How to calculate/get GMT

Creaky

Member
Licensed User
Longtime User
I've been looking at all the DateTime related posts and tutorials but I somehow can't figure out a way to calculate/get the true GMT time.

Most examples use:

B4X:
Dim timezone As Long
timezone = -DateTime.DateParse("01/01/1970")
Msgbox(DateTime.Time(DateTime.Now - timezone),"")

But this does not take DST into account. I live in Holland and our timezone is GMT+1, but not during the somertime then it is GMT+2.

When I set the timezone in my emulator, it tells me "Amsterdam, Berlin" is now GMT+2:00, which is correct (Timezone + DST). But the code above reports 10:00 when the local time here is 11:00. It should be 09:00 as that is the local time minus 2 hours. What am I missing here?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Please try the following code:
B4X:
Sub Activity_Create(FirstTime As Boolean)
   Log(GetTimeZoneOffset)
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub
Sub GetTimeZoneOffset
   Dim s, d As String
   Dim l1 As Long
   s = DateTime.DateFormat
   DateTime.DateFormat = "MM/dd/yyyy HH:mm:ss"
   l = DateTime.Now
   d = DateTime.Date(l) & " GMT"
   DateTime.DateFormat = "MM/dd/yyyy HH:mm:ss z"
   Dim res As Int
   res = -Round((l - DateTime.DateParse(d))/3600000)
   DateTime.DateFormat = s
   Return res
End Sub
 
Upvote 0

Creaky

Member
Licensed User
Longtime User
The response is "2" :)

I presume that it is +2 and that when I use a negative timezone I get -2?
 
Upvote 0

Creaky

Member
Licensed User
Longtime User
Just tried your new code Erel, although it does give me the number of hours offset to GMT, it doesn't tell me whether it is a positive offset or a negative one.

When I change the timezone in my emu to +02:00 or -02:00, it returns "2" in both cases. Now I've been trying to grasp what you are doing in that sub, but I can't figure it out. Are you able to change the sub so that it does give a negative offset when a GMT-02:00 timezone is used?

Thanx in advance for the effort!
 
Upvote 0
Top