Page 1 of 1 1
Topic Options
#186432 - 2008-03-22 04:19 AM DateDiff, DateAdd
Skatterbrainz Offline
Starting to like KiXtart

Registered: 2002-10-17
Posts: 172
Loc: Virginia, USA
I know there's plenty of UDF's for this, but everytime I turn around it seems there's a bug being fixed or someone suggesting an alternate method. Would it be possible to build in DateDiff and DateAdd functions to make it easier to compare and calculate date and time results? WSH makes it pretty simple. I really like Kix more, but having to add things like this seem unnecessary and obvious enough to have built-in. Maybe I'm the only one that feels this way, but this is the suggestion forum, so hopefully this doesn't cause anyone too much grief.
_________________________
silence is golden, but duct tape is silver

Top
#186437 - 2008-03-22 06:44 PM Re: DateDiff, DateAdd [Re: Skatterbrainz]
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
Not sure what bugs and/or fixes there are/have been. The beauty of alternate methods is you get to choose what's best for your need.

I'm not familiar with what in WSH would simplify things. Date and time can take different formats so various methods are needed. IMHO there are enough UDFs around to address the various needs.

I think intrinsic code to handle all the diverse needs will bloat KiX for the many and benefit the few. My two cents anyway.
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#186438 - 2008-03-22 06:51 PM Re: DateDiff, DateAdd [Re: Skatterbrainz]
Glenn Barnas Administrator Offline
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.
 Code:
   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! \:D

Top
#187420 - 2008-05-01 09:40 AM Re: DateDiff, DateAdd [Re: Glenn Barnas]
cj Offline
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
#187423 - 2008-05-01 11:35 AM Re: DateDiff, DateAdd [Re: cj]
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
 Quote:
This is a cool system as you get as much accuracy as the computer can count

Using floats is actually not a great idea as it is not very portable and can lead to errors, the accuracy of the computer is somewhat less than you think \:\)

Specifically, floating point math / storage uses approximation which causes rounding errors. Google for "floating point approximation" if you want more information.

Top
#187433 - 2008-05-02 04:33 AM Re: DateDiff, DateAdd [Re: Richard H.]
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11164
Loc: Boston, MA, USA
Don't even get me started on the fallacies of using FLOATs. I personally think one should not use FLOATS unless one fully understands the (dis)advantages.

I once had an Oracle developer use FLOATS in an interim step to translate monetary values from a text source to a decimal destination. He was then wondering why his numebrs were always off of $0.01. took him a while to get the concept that the computer introduced imprecision into his process.
_________________________
There are two types of vessels, submarines and targets.

Top
#187474 - 2008-05-06 01:49 AM Re: DateDiff, DateAdd [Re: Sealeopard]
cj Offline
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!? \:D

cj

Top
#191569 - 2009-01-03 11:15 PM Re: DateDiff, DateAdd [Re: cj]
Skatterbrainz Offline
Starting to like KiXtart

Registered: 2002-10-17
Posts: 172
Loc: Virginia, USA
Getting back to the original question I posted: I was looking at how other languages have reached a point where certain "standard" functions have been incorporated into the base scripting engine because they had become so widely used. Things like string manipulation, date and time functions, math and statistical operations, and in some cases database interface tools. It seems to me that date and time functions are pretty common and that maybe it's a good time to consider rolling at least two into the "core": DateDiff() and DateAdd(). I understand some would disagree, but this is the "suggestion" forum, so I'm just making a suggestion. \:\)
_________________________
silence is golden, but duct tape is silver

Top
Page 1 of 1 1


Moderator:  Lonkero, ShaneEP, Jochen, Radimus, Glenn Barnas, Allen, Ruud van Velsen, Mart 
Hop to:
Shout Box

Who's Online
2 registered (morganw, mole) and 414 anonymous users online.
Newest Members
gespanntleuchten, DaveatAdvanced, Paulo_Alves, UsTaaa, xxJJxx
17864 Registered Users

Generated in 0.056 seconds in which 0.022 seconds were spent on a total of 13 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org