new date conversion bug?
Devin Asay
devin_asay at byu.edu
Thu Jan 31 15:52:10 EST 2008
I am working on a scheduling program that includes the ability to
schedule an event every other week over a specific time period. To
determine whether the event should occur during any given week during
the time period I do some simple date math to figure out whether a
multiple of 14 days have passed since the first occurrence of the
event. Let's say I've scheduled an event for every other week on
Friday, beginning January 11, 2008 and ending April 11, 2008. For any
given week on my calendar I can calculate whether the event should
show up using this algorithm (pseudocode):
get the date of the first occurrence of the event and convert it to
seconds
get the date of that the event should fall on if this week falls in
the every-other-week pattern, convert it to seconds
subtract date 2 from date 1
divide the difference by (60*60*24*14) --the number of seconds in 14
days
if the difference divides evenly (modulo division = 0) then the event
should occur in the week in question
This works fine for a few weeks, but then inexplicably fails. For
example, Jan. 11, Jan. 25, Feb. 8, Feb. 22, and March 7 all show the
event properly. But anything after that fails; i.e., March 21, April 4.
Here is a simulation I did of my code in the multiline message box:
convert "1/11/08" to seconds; put it into tD1; put tD1
convert " 3/7/08" to seconds; put it into tD2; put cr & tD2 after msg
put tD2 - tD1 into diff; put cr & diff after msg
put cr & (diff div (24*60*60*14)) after msg
put cr & (diff mod (24*60*60*14)) after msg
Results:
1200034800 --D1 seconds
1204873200 --D2 seconds
4838400 --difference
0 --diff mod 14 days worth of seconds --OK!
BUT:
convert "1/11/08" to seconds; put it into tD1; put tD1
convert " 3/21/08" to seconds; put it into tD2; put cr & tD2 after msg
put tD2 - tD1 into diff; put cr & diff after msg
put cr & (diff mod (24*60*60*14)) after msg
Results:
1200034800 --D1 seconds
1206079200 --D2 seconds
6044400 --difference
1206000 --diff mod 14 days worth of seconds --BZZZZZT! Wrong!
Now if you do some quick math it turns out that the second example is
3600 seconds--1 hour-- short of a full 14 days! This reminds me of a
date conversion bug we were dealing with a year ago or so. So am I
looking at some weird convert bug? Or is the bug in my brain? (I'm
prepared to accept either possibility.) ;-)
BTW, this is Rev v. 2.8.1 on Intel Mac OS X 10.4.11. The same thing
happens in 2.9.0-dp-3. No Windows box to check on.
Regards,
Devin
Devin Asay
Humanities Technology and Research Support Center
Brigham Young University
More information about the use-livecode
mailing list