Re: [Gambas-user] UTC conversion Tip for Date class doesn't work as expected
Brought to you by:
gambas
|
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 |