UNIX Timestamp and Timezone

Started by Krischan, July 16, 2018, 21:34:58

Previous topic - Next topic

Krischan

Hi there,

I have a small problem with Blitzmax, Timestamps and Windows. The POSIX standard has the "z/Z" format code which should return the local time zone offset compared to the UTC time. But Microsoft, well, makes it dependent of the registry, see https://msdn.microsoft.com/en-us/library/fe06s4ak.aspx

So executing this small test programm doesn't return the local time with the offset as a number, I'm getting the time zone name as text only

Expected Result: 2018-07-16 22:26:20 +0200
My Result: 2018-07-16 22:26:20 Mitteleuropäische Sommerzeit

Any idea how to get ALWAYS the local UTC offset as a number in Blitzmax on all systems?

Code (Blitzmax) Select
SuperStrict

Print FTimestamp(GetTimeStamp())

Function FTimestamp:String(timestamp:Int, format:String = "%Y-%m-%d %H:%M:%S %z")

Local time:Int Ptr, buff:Byte[256]

time = VarPtr(timestamp)
strftime_(buff, 256, format, localtime_(time))

Return String.FromCString(buff)

End Function


Function GetTimeStamp:Int()

Local time:Int[256]
time_(time)
Return time[0]

End Function
Kind regards
Krischan

Windows 10 Pro | i7 9700K@ 3.6GHz | RTX 2080 8GB]
Metaverse | Blitzbasic Archive | My Github projects

Henri

Hi,

there is an environment variable called TZ (timezone) which normally is not set, but if set, then it would effect all time functions. My idea is to set it to UCT/GMT timezone, in order to always get the same timestamp.

You can experiment with this:
Code (blitzmax) Select

putenv_("TZ=UCT0")


Other idea is to use bah.datetime module. If you are only after time difference, then acquire both local time and UCT time and calculate the difference.

i'm sure that there is some API to get this info directly from control panel, but that would require some effort to get working on all platforms.

Ps. I'm not on my machine right now, but killing mosquitoes  8)

-Henri
- Got 01100011 problems, but the bit ain't 00000001

Krischan

#2
Great. Works. Thanks! And good hunting! Notice: to get bah.datetime running, the bah.boost module must be compiled first. If there is a problem, there is always a Blitzmax module (ancient wisdom) ;D

Could somebody in a different timezone than MESZ please check if it is working properly (I've attached a compiled exe for commandline for that)

Local:      2018-Jul-18 18:30:20
UTC:        2018-Jul-18 16:30:20
Difference: 2 Hours

Unfixed output:
2018-07-18 18:30:20 Mitteleuropäische Sommerzeit


Code (Blitzmax) Select
SuperStrict

Import bah.datetime

Local l:TTime = TTime.CreateLocal()
Local localtime:String = l.toString()

Local u:TTime = TTime.CreateUniversal()
Local utctime:String = u.toString()

Local diff:String = l.Subtract(u).toString()

Print "Local:      " + localtime
Print "UTC:        " + utctime
Print "Difference: " + Int(diff) + " Hours"
Print
Print "Unfixed output:"
Print FTimestamp(GetTimeStamp())

Function FTimestamp:String(timestamp:Int, format:String = "%Y-%m-%d %H:%M:%S %z")

        Local time:Int Ptr, buff:Byte[256]
       
        time = VarPtr(timestamp)
        strftime_(buff, 256, format, localtime_(time))
       
        Return String.FromCString(buff)
       
End Function

Function GetTimeStamp:Int()

        Local time:Int[256]
        time_(time)
        Return time[0]

End Function
Kind regards
Krischan

Windows 10 Pro | i7 9700K@ 3.6GHz | RTX 2080 8GB]
Metaverse | Blitzbasic Archive | My Github projects

Henri

My output from Finland is:

Local:      2018-Jul-19 16:53:50
UTC:        2018-Jul-19 13:53:50
Difference: 3 Hours

Unfixed output:
2018-07-19 16:53:50 FLE Daylight Time


-Henri
- Got 01100011 problems, but the bit ain't 00000001