Time Zone Offset

99toLife

Member
Licensed User
Longtime User
I'm trying to get the device's time zone offset.

I'm using the code posted here by Erel and (I think) it was working in Basic4android 1.7 but not now in 1.8. The GetTimeZoneOffset method always returns -8.

Also, DateTime.TimeZoneOffset always returns -8.

I have Basic4android running in VMWare with Windows set to Central time zone (offset -6) and Central time zone set in the Android emulator too. I'm thinking this might be an emulator issue.

Does anyone have any suggestions as to what's wrong? Why is the time zone offset always returning -8 instead of -6?
 
Last edited:

99toLife

Member
Licensed User
Longtime User
The emulator uses its own date / time settings. You can change it in the settings menu (Home screen - Menu - Settings).

I did, but the following code always returns -8. I can verify the setting is correct in the emulator because the correct time is shown.

B4X:
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
 
Last edited:
Upvote 0

99toLife

Member
Licensed User
Longtime User
Even if the displayed time is correct, it is possible that the offset is not correct.
You should check the settings.

You should use DateTime.TimeZoneOffset instead.

Thanks for your help, Erel.

DateTime.TimeZoneOffset returns -8 although the emulator is set to GMT-6:00 Central Standard Time. :BangHead:

The following images can maybe better describe my issue.

-6 is set...
Screen%20Shot%202012-01-28%20at%2011.29.28%20AM.png


...but -8 is returned.
Screen%20Shot%202012-01-28%20at%2011.32.30%20AM.png
 
Last edited:
Upvote 0

99toLife

Member
Licensed User
Longtime User
Ok, this is interesting.

It was also returning -8 on my Kindle Fire, so maybe it's not just the emulator...or maybe it's just my code? :mad:

But, now after changing my code around, DateTime.TimeZoneOffset returns the correct offset if called from the Process_Globals and Activity_Create methods but not in other methods I tried (such as a button_click method).

Anyways, I set a global variable in Process_Globals (and called DateTime.TimeZoneOffset there) so I always have the correct offset. :sign0060:
 
Last edited:
Upvote 0
Top