Thread: [Gambas-user] UTC conversion Tip for Date class doesn't work as expected
Brought to you by:
gambas
|
From: T L. D. <t.l...@gm...> - 2014-11-28 23:58:06
|
http://gambaswiki.org/wiki/lang/type/date says, "Dates can be converted to numbers. Then the number returned is the number of days stored internally and the fraction of day represented by the number of microseconds." And ,there is a tip box that says: [[ tip As date are internally stored in UTC, the time offset between local time and UTC is represented by the fractional part of the floating point number returned by the date to float conversion. So, to convert a date into UTC, you do the following: UTCDate = LocalDate - Frac(Date(Now)) ]] 1. It is unclear to me how the fractional part can represent both "the fraction of day represented by the number of microseconds" *and* "the time offset between local time and UTC", especially since it would always be changing. 2. A simple command-line app is apparently also confused: Public Sub Main() Dim dDate1, dDate2 As Date Print "System.TimeZone: " & System.TimeZone Print "Number of hours to add: " & Str(System.TimeZone / 60 / 60) Print "Frac(Date(Now)): " & Frac(Date(Now)) & "\n" dDate1 = Now Print "Date now : " & Format(dDate1, "yyyy/mm/dd hh:nn") dDate2 = dDate1 - Frac(Date(Now)) 'To follow example Print "UTC date now: " & Format(dDate2, "yyyy/mm/dd hh:nn") & "\n" Print "Difference: " & DateDiff(dDate1, dDate2, gb.Hour) End On my system, this produced: System.TimeZone: 18000 Number of hours to add: 5 Frac(Date(Now)): 0.79166666651145 Date now : 2014/11/28 18:49 UTC date now: 2014/11/27 23:49 Difference: -19 Instead of adding 5 hours, it subtracted 19. -- Lee __________ "Artificial Intelligence is no match for natural stupidity." P.S. [System] Gambas=3.6.0 OperatingSystem=Linux Kernel=3.10.60-desktop-1.mga3 Architecture=x86 Distribution=Mageia 3 Desktop=KDE4 Theme=Oxygen Language=en_US.UTF-8 Memory=1005M [Libraries] Cairo=libcairo.so.2.11200.12 Curl=libcurl.so.4.3.0 DBus=libdbus-1.so.3.7.2 GStreamer=libgstreamer-0.10.so.0.30.0 GStreamer=libgstreamer-1.0.so.0.5.0 GTK+3=libgtk-3.so.0.600.4 GTK+=libgtk-x11-2.0.so.0.2400.17 OpenGL=libGL.so.1.2.0 Poppler=libpoppler.so.34.0.0 Qt4=libQtCore.so.4.8.6 SDL=libSDL-1.2.so.0.11.4 |
|
From: Benoît M. <ga...@us...> - 2014-11-29 00:24:25
|
Le 29/11/2014 00:57, T Lee Davidson a écrit : > http://gambaswiki.org/wiki/lang/type/date says, "Dates can be converted > to numbers. Then the number returned is the number of days stored > internally and the fraction of day represented by the number of > microseconds." > > And ,there is a tip box that says: > [[ tip > As date are internally stored in UTC, the time offset between local time > and UTC is represented by the fractional part of the floating point > number returned by the date to float conversion. > > So, to convert a date into UTC, you do the following: > UTCDate = LocalDate - Frac(Date(Now)) > ]] > > 1. It is unclear to me how the fractional part can represent both "the > fraction of day represented by the number of microseconds" *and* "the > time offset between local time and UTC", especially since it would > always be changing. > > 2. A simple command-line app is apparently also confused: > Public Sub Main() > > Dim dDate1, dDate2 As Date > > Print "System.TimeZone: " & System.TimeZone > Print "Number of hours to add: " & Str(System.TimeZone / 60 / 60) > Print "Frac(Date(Now)): " & Frac(Date(Now)) & "\n" > > dDate1 = Now > Print "Date now : " & Format(dDate1, "yyyy/mm/dd hh:nn") > dDate2 = dDate1 - Frac(Date(Now)) 'To follow example > Print "UTC date now: " & Format(dDate2, "yyyy/mm/dd hh:nn") & "\n" > > Print "Difference: " & DateDiff(dDate1, dDate2, gb.Hour) > > End > > On my system, this produced: > System.TimeZone: 18000 > Number of hours to add: 5 > Frac(Date(Now)): 0.79166666651145 > > Date now : 2014/11/28 18:49 > UTC date now: 2014/11/27 23:49 > > Difference: -19 > > > Instead of adding 5 hours, it subtracted 19. > > You're right, the tip is false, i.e. it works only when System.TimeZone < 0, otherwise you have a 24h error. It was written before System.TimeZone exist. So you must do that instead: UTCDate = DateAdd(LocalDate, gb.Second, System.TimeZone) And the wiki must be fixed... Regards, -- Benoît Minisini |
|
From: T L. D. <t.l...@gm...> - 2014-11-29 00:32:10
|
On 11/28/2014 07:24 PM, Benoît Minisini wrote: > > You're right, the tip is false, i.e. it works only when System.TimeZone > < 0, otherwise you have a 24h error. It was written before > System.TimeZone exist. > > So you must do that instead: > > UTCDate = DateAdd(LocalDate, gb.Second, System.TimeZone) > > And the wiki must be fixed... I'd be glad to do that. But, would you like the Tip simply removed or modified re: "UTCDate = DateAdd(LocalDate, gb.Second, System.TimeZone)"? -- Lee __________ "Artificial Intelligence is no match for natural stupidity." |
|
From: Benoît M. <ga...@us...> - 2014-11-29 00:33:49
|
Le 29/11/2014 01:32, T Lee Davidson a écrit : > On 11/28/2014 07:24 PM, Benoît Minisini wrote: >> >> You're right, the tip is false, i.e. it works only when System.TimeZone >> < 0, otherwise you have a 24h error. It was written before >> System.TimeZone exist. >> >> So you must do that instead: >> >> UTCDate = DateAdd(LocalDate, gb.Second, System.TimeZone) >> >> And the wiki must be fixed... > > I'd be glad to do that. But, would you like the Tip simply removed or > modified re: "UTCDate = DateAdd(LocalDate, gb.Second, System.TimeZone)"? > Remove the tip, as it is false. -- Benoît Minisini |
|
From: T L. D. <t.l...@gm...> - 2014-11-29 00:35:55
|
On 11/28/2014 07:33 PM, Benoît Minisini wrote: > Le 29/11/2014 01:32, T Lee Davidson a écrit : >> On 11/28/2014 07:24 PM, Benoît Minisini wrote: >>> >>> You're right, the tip is false, i.e. it works only when System.TimeZone >>> < 0, otherwise you have a 24h error. It was written before >>> System.TimeZone exist. >>> >>> So you must do that instead: >>> >>> UTCDate = DateAdd(LocalDate, gb.Second, System.TimeZone) >>> >>> And the wiki must be fixed... >> >> I'd be glad to do that. But, would you like the Tip simply removed or >> modified re: "UTCDate = DateAdd(LocalDate, gb.Second, System.TimeZone)"? >> > > Remove the tip, as it is false. > Done. :-) -- Lee __________ "Artificial Intelligence is no match for natural stupidity." |