[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 |