Odd results with converting dates

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
alanspurgeon
Posts: 2
Joined: Sun Jan 18, 2015 11:03 am

Odd results with converting dates

Post by alanspurgeon » Sun Jan 18, 2015 12:05 pm

I ran across some odd behavior while writing some unit tests against a stack. My test cycles through the calendar and everything goes fine until I reach November. For some reason it appears convert to long date is not giving me what I expect and the date this happens is different depending upon the year. Maybe there is a better way to increment the date by a day that I am unaware of.

Here is sample code and example output that shows what I am seeing..

put "1/1/2015" into theDate
put "" into card field “testOutput”

repeat for 700 times
convert theDate to long date
put theDate after field “testOutput”
convert theDate to seconds
put " = " & theDate & "(sec) next day: " after field “testOutput”
add 86400 to theDate
convert theDate to long date
convert theDate to seconds
put theDate &"(sec)" & CRLF after field “testOutput”
end repeat

[sample output]…
Friday, October 23, 2015 = 1445583600(sec) next day: 1445670000(sec)
Saturday, October 24, 2015 = 1445670000(sec) next day: 1445756400(sec)
Sunday, October 25, 2015 = 1445756400(sec) next day: 1445842800(sec)
Monday, October 26, 2015 = 1445842800(sec) next day: 1445929200(sec)
Tuesday, October 27, 2015 = 1445929200(sec) next day: 1446015600(sec)
Wednesday, October 28, 2015 = 1446015600(sec) next day: 1446102000(sec)
Thursday, October 29, 2015 = 1446102000(sec) next day: 1446188400(sec)
Friday, October 30, 2015 = 1446188400(sec) next day: 1446274800(sec)
Saturday, October 31, 2015 = 1446274800(sec) next day: 1446361200(sec)
Sunday, November 1, 2015 = 1446361200(sec) next day: 1446361200(sec)
Sunday, November 1, 2015 = 1446361200(sec) next day: 1446361200(sec)
Sunday, November 1, 2015 = 1446361200(sec) next day: 1446361200(sec)
Sunday, November 1, 2015 = 1446361200(sec) next day: 1446361200(sec)
Sunday, November 1, 2015 = 1446361200(sec) next day: 1446361200(sec)


If I change the year that the test starts from it hangs at a different date..
In 2016 it gets hung up on Sunday, November 6, 2016
In 2017 : Sunday, November 5, 2017
In 2018 : Sunday, November 4, 2018

Also if I have the test start with Nov 2, 2015 it runs fine until it reaches Nov 6, 2016.

I’m running LiveCode 7 Community Edition 7.0.1 | Build 10023 on a Mac with OS/X 10.10.1

Thanks,
Alan

Dixie
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1336
Joined: Sun Jul 12, 2009 10:53 am

Re: Odd results with converting dates

Post by Dixie » Sun Jan 18, 2015 12:45 pm

Mmm... it looks like you have found a bug... I have just run more or less the same script as you and am seeing the same as you...

Code: Select all

on mouseUp
   lock screen
   put empty into fld 1
   put the short date into tempdate
   convert tempdate to seconds
   
   repeat with count = 1 to 365
      convert tempdate to long date
      put tempdate & cr after fld 1
      convert tempdate to seconds
      add 86400 to tempdate
      wait 0 seconds with messages
   end repeat
   beep
end mouseUp

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4172
Joined: Sun Jan 07, 2007 9:12 pm

Re: Odd results with converting dates

Post by bn » Sun Jan 18, 2015 1:17 pm

Hi Alan,

Welcome to the forum.

when does Daylight Saving Time start and end in your part of the world?
Hint, hint.

Kind regards
Bernd

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4172
Joined: Sun Jan 07, 2007 9:12 pm

Re: Odd results with converting dates

Post by bn » Sun Jan 18, 2015 1:26 pm

in case you have not seen this:

Simon posted this to the use-list

https://www.youtube.com/watch?v=-5wpm-gesOY

Kind regards

Bernd

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4172
Joined: Sun Jan 07, 2007 9:12 pm

Re: Odd results with converting dates

Post by bn » Sun Jan 18, 2015 1:49 pm

Hi Alan,

try this, only a slight modification of your script
It starts at Noon, thanks to Sarah Reichelt for this trick.

Code: Select all

on mouseUp
   lock screen
   put "1/1/2015" into theDate
   convert theDate to dateItems
   put 12 into item 4 of theDate -- noon
   convert theDate to seconds
   
   put empty into field "fTest"
   
   repeat for 700 times
      put theDate into tLongDate
      convert tLongDate to long date
      put tLongDate after field "fTest"
      put " = " & theDate & "(sec) next day: " after field "fTest"
      add 86400 to theDate
      put theDate into tLongDate
      convert tLongDate to long date
      put theDate &"(sec)" & cr after field "fTest"
   end repeat
   unlock screen
end mouseUp
Kind regards
Bernd

PS no need to say «card field "xyz"» «field "xyz"» is enough. And no need for CRLF, CR or return is enough. Livecode uses LF internally anyways and translates the line-endings on export and import of text files depending on the operating system. CRLF Windows, CR Mac, LF Linux.

Dixie
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1336
Joined: Sun Jul 12, 2009 10:53 am

Re: Odd results with converting dates

Post by Dixie » Sun Jan 18, 2015 5:18 pm

Bernd...

mmm...:-)

Dixie

alanspurgeon
Posts: 2
Joined: Sun Jan 18, 2015 11:03 am

Re: Odd results with converting dates

Post by alanspurgeon » Mon Jan 19, 2015 1:01 am

You guys rock. Yep its got to do with daylight savings time.

It looks like I'm not using convert on the date correctly as well. It needs "and long time" or else I will loose any time setting I make.

put "11/1/2015" into theDate
convert theDate to dateItems
put "13" into item 4 of theDate <-- set time to 1pm
convert theDate to long date <-- loose the time settings, oops!

put "13" into item 4 of theDate
convert theDate to long date and long time <-- keeps the time settings, yea!

To do date arithmetic I think I'm going to use dateItems instead of seconds..

put "43" into numDays
convert theDate to dateItems <-- get theDate into dateItems format
add numDays to item 3 of theDate <-- add a number of days
convert theDate to dateItems <-- now let convert do the appropriate date arithmetic

Thanks everyone for your suggestions!

Alan

Post Reply