#186438 - 2008-03-22 06:51 PM
Re: DateDiff, DateAdd
[Re: Skatterbrainz]
|
Glenn Barnas
KiX Supporter
Registered: 2003-01-28
Posts: 4396
Loc: New Jersey
|
The TimeDiff and TimeConvert (which use similar logic) provide a high degree of accuracy. From the header: "Validated by generating sequential date values from 1/1/1883 to 12/31/2105, a range of just over 8.5 Billion seconds."
The 1883 date was chosed pretty much at random for the sequential testing. Random testing for dates between 1 and the year 3000 was also done.
The logic used by these UDFs is widely regarded as the standard method of date calculation.
TimeDiff returns the difference in time (in seconds) between two dates. Alternate values can be specified, such as minutes, hours, or days. For short duration calculations, it can calculate and return millisecond accuracy. I say "short durations" due to limits of double-precision. Generally, for anything over a few days, milliseconds won't matter anyway.
TimeConvert uses the same algorithm, but converts a date/time string to cTime format - the number of seconds that elapsed since an epoch date. It defaults to a standard computer epoch date of 1/1/1970, but any date can be used and it will return the seconds from midnight of that date.
Also, note that the calendar as we know it changed in Sept, 1752. 11 days were removed (Sept 3-13) to "catch up" for bad leap year calculations (among other discrepencies). None of the date calculations take this into account, so calculations that span those dates (unlikely) will probably need to make specific adjustments.
September 1752
Su Mo Tu We Th Fr Sa
1 2 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
Glenn
_________________________
Actually I am a Rocket Scientist!
|
Top
|
|
|
|
#187420 - 2008-05-01 09:40 AM
Re: DateDiff, DateAdd
[Re: Glenn Barnas]
|
cj
MM club member
Registered: 2000-04-06
Posts: 1102
Loc: Brisbane, Australia
|
Just to add another method to the mix The TDateTime class in Delphi (Pascal) uses a float for dates and times counting from midnight on the 30th of December 1899 as 0.0.
The whole number is the number of days since that date and the fractional component is the time since midnight. so .25 is 0600 and .5 is noon etc.
As I type this, it's 01/05/2008 17:37:03 which is 39569.7340687384. This is a cool system as you get as much accuracy as the computer can count. Typically you would format it down to milliseconds at the most.
Getting the difference in dates is just one number minus the other with or without the fractions if you care for the times. I was born at 26961.3958395718, so I am now 12608.3382291666 days old.
In fact, here's a little app to show those numbers in action...
cj
Edited by cj (2008-05-01 10:17 AM) Edit Reason: Added URL to app
|
Top
|
|
|
|
#187474 - 2008-05-06 01:49 AM
Re: DateDiff, DateAdd
[Re: Sealeopard]
|
cj
MM club member
Registered: 2000-04-06
Posts: 1102
Loc: Brisbane, Australia
|
I tend to agree and have had all manner of trouble dealing with DateTimes. You can't just trust = and <> to compare times, you have to say Abs(T1-T2)<00:00:00.001 means =.
What century is this, where's my quantum computer!?
cj
|
Top
|
|
|
|
Moderator: Lonkero, ShaneEP, Jochen, Radimus, Glenn Barnas, Allen, Ruud van Velsen, Mart
|
2 registered
(morganw, mole)
and 414 anonymous users online.
|
|
|