Calculating number of days between two dates.

Started by
39 comments, last by owl 19 years, 2 months ago
Quote:Original post by owl
Should I add 1 to the years passed or am I doing something wrong?

Just a shot in the dark, but you could try a nearest rounding instead of truncation when converting years to days.
(int)(year * 365.256 + 0.5)

instead of
(int)(year * 365.256)

I'm not entirely sure how my code does it (I'm converting dates to UNIX time), but I think it's using nearest rounding, and my times are off by just a second or so (just a rough implementation so far, so it works quite good in my oppinion) so it seems to handle leap years fine.
Advertisement
Shouldn't the 'daysinmonth' be cumulative? o_O
Quote:Original post by Zahlman
Shouldn't the 'daysinmonth' be cumulative? o_O


What you mean? daysinmont is an array[12] that holds how many days each month of a year has according to Gregory Peck. ;)

@Bob: If i round the calculation, the whole leap year thing will be gone. It will always return 365. If I substract 1 to each year passed then it works perfect.
[size="2"]I like the Walrus best.
No.

Daysinmonth is an array, which holds the amount of days there are in the month (non-leap-year).

From,
Nice coder
Click here to patch the mozilla IDN exploit, or click Here then type in Network.enableidn and set its value to false. Restart the browser for the patches to work.
All I have to figure now is how to calculate the correct date that is the result of substracting n days from some other date taking leap years into account too.

01/01/0001 - n(days) = ??/??/????

I'm considering handling BC dates as negative years...

Advice on this will be appreciated too :D
[size="2"]I like the Walrus best.
I don't mean to rain on your parade, but I'm curious as to why you're duplicating the efforts in the Boost library?
Quote:Original post by Kylotan
I don't mean to rain on your parade, but I'm curious as to why you're duplicating the efforts in the Boost library?


I dunno, for fun I guess. I don't want to make a huge complex library as Date_Time, just a simple/intuitive class (for dummies) that can perform the most common operations on any *Gregorian* kind of date (BC/AD).
[size="2"]I like the Walrus best.
Surely there's no reason to do time/date math by hand (a sure way to lose one's mind in the long run) when there are standard facilities in the <time.h> / <ctime> header just for that purpose!

I *strongly* recommend representing your dates as the standard type time_t (UNIX timestamp, number of seconds since 00:00:00 1970/01/01; a simple integer type on most systems). It's a *lot* easier, shorter, faster, less bug-prone and takes less memory than storing the parts separately as integers.

EDIT: Oh well, this approach won't do much good if you want to have dates outside the size_t's range...
Quote:Original post by Sharlin
(UNIX timestamp, number of seconds since 00:00:00 1970/01/01; a simple integer type on most systems). It's a *lot* easier, shorter, faster, less bug-prone and takes less memory than storing the parts separately as integers.


But what would be of the poor historian-programmers then? :(

Or old programmers that want to remember the good ol' 50-60's?
[size="2"]I like the Walrus best.
Yeah, I realised that almost as I hit "reply", re-reading your original message and seeing the dates you used as an example... Edited my message to reflect that. Still, I'd say that a third-party library is better than doing all the dirty stuff yourself. If not boost, then something else.

This topic is closed to new replies.

Advertisement