Time Zones

jscoulter

Member
Licensed User
Longtime User
Hi All.
I have some data that has dates in UTC (GMT) but my time zone is +12 on GMT.
I could just add 12hrs to the date and time to get the right date, but when daylight saving kicks in again....in 6 odd months, I will need to change it to +13....but the sensible thing o do is use the timezone.
Are there any examples on how to use the timezone of the phone to get the right time?

Jeremy
 

awama

Active Member
Licensed User
Longtime User
Hi Erel,

Dim timezone, uhrzeit As Long
timezone = -DateTime.DateParse("01/01/1970")

uhrzeit = DateTime.Time(0) ' uhrzeit = 3600000
uhrzeit = uhrzeit - timezone ' uhrzeit = 0

Ist das so korrekt?
------------
Muß ich bei jeder Uhrzeit-Berechnung eine Timezone-Korrektur machen?

Dim z1,z2,zdiff As Long
Dim sz, zz As String
Dim timezone As Long

timezone = -DateTime.DateParse("01/01/1970")

sz="00:00:00"
zz="02:00:00"

z1 = DateTime.DateParse(sz)
z2 = DateTime.DateParse(zz)
zdiff = z2 - z1 - timezone 'so ist das Ergebnis bei mir richtig, = "02:00:00"

Wenn ich das richtig verstanden habe muß bei jeder Uhrzeitberechnung z.B. wieviel Zeit zwischen 13:00 und 15:30 vergangen ist noch eine Timezone berücksichtigt werden sonst würde das Ergebnis in Europa und USA um einige Stunden nicht stimmen. Oder gibt es noch eine andere Möglichkeit wie ich die Differenz zwischen zwei Uhrzeiten ohne Timezone-Korrektur berechnen kann?
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
Wie Erel es schon angedeutet hat, es kommt darauf an was man machen will.
Der Code sollte so aussehen:
B4X:
sz="00:00:00"
zz="02:00:00"
 
z1 = DateTime.TimeParse(sz)
z2 = DateTime.TimeParse(zz)
zdiff_Ticks = z2 - z1 
zdiff_String = DateTime.Time(zdiff_Ticks - timezone)
Die DateTime.Time Funktion wandelt die Ticks in eine Uhrzeit um und addiert deshalb die Timezone dazu.
Da Du aber eine Zeitdifferenz als String willst musst Du bei der Umwandlung die Timezone abziehen.

Beste Grüsse.

As Erel already mentioned, it depends on what you want to do.
The code should look like above.
The DateTime.Time function converts the Ticks in a clock time and therefore adds the Timezone.
But as you want a time difference as a string, you must subtract the Timezone for the conversion.

Best regards.
 
Upvote 0

awama

Active Member
Licensed User
Longtime User
Ok, das habe ich jetzt entgültig verstanden. Sobald DateTime im Spiel ist auch die jeweilige Zeitzone zu berücksichtigen. Will ich unabhängig von der Zeitzone die Differenz von 2 Uhrzeiten berechnen dann muß ich das mathematisch lösen.

Dim z1,z2,z3 As Long
Dim sz, zz, dz As String
sz="00:00:00"
zz="02:00:00"

z1 = DateTime.timeparse(sz)
z2 = DateTime.timeparse(zz)
z3 = z2 - z1

dz = Floor(z3 / DateTime.TicksPerDay)
dz = dz & CRLF & (Floor(z3 / DateTime.TicksPerHour) Mod 24)
dz = dz & ": " & (Floor(z3 / DateTime.TicksPerMinute) Mod 60)
dz = dz & ": " & (Floor(z3 / DateTime.TicksPerSecond) Mod 60)

Besten Dank
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
Will ich unabhängig von der Zeitzone die Differenz von 2 Uhrzeiten berechnen dann muß ich das mathematisch lösen.
Nein, denn sobald du die Zeiten in Ticks umgewandelt hast und die Differenz rechnest ist das Resultat die richtige Zeitdifferenz, die zwei Timezones gleichen sich hier aus. Erst wenn du die Ticks wieder in eine Uhrzeit zurückwandeln willst dann musst Du wieder eine Timezone abziehen.
Der Code den Ich gepostet habe funktioniert, Du brauchst nicht mit den TicksPerxxx arbeiten da Deine Zeiten am gleichen Tag vorkommen.
Falls deine Zeiten an verschiedenen Tagen vorkommen musst Du mit DateParse, mit dem Datum und Zeit, anstatt mit TimeParse arbeiten und mit den TicksPerxxx Funktionen.

Beste Grüsse.
 
Last edited:
Upvote 0

alfcen

Well-Known Member
Licensed User
Longtime User
While the time zone can be determined by:

timezone = -DateTime.DateParse("01/01/1970")

it seems that daylight saving time is not being compensated for.

Is there any means of confirming DST? Yet I doubt that the
Android o/s knows when DST begins and ends in every country.
 
Upvote 0

alfcen

Well-Known Member
Licensed User
Longtime User
Thanks for your thoughts, Erel. Japan is without DST, so I can't check it myself.
When your provider sets your phone an hour ahead will

timezone = -DateTime.DateParse("01/01/1970") / DateTime.TicksPerHour

return your time zone as 3 (standard) or 4?
 
Upvote 0

alfcen

Well-Known Member
Licensed User
Longtime User
Get DST Routine

When a phone is on-air then the provider will automatically adjust the clock for DST, say, from GMT+01:00 to GMT+02:00. This can be ascertained by

DateTime.TimeFormat = "z"

while

-DateTime.DateParse("01/01/1970") / DateTime.TicksPerHour

will still return "1"

This sub will get DST provided all devices return the same format, such as "GMT+01:00" or "GMT-03:30", after

DateTime.TimeFormat = "z"


B4X:
Sub get_dst
   Dim saveDateFormat As String, saveTimeFormat As String, systime_string As String, dst_hour As Int, dst_min As Int
   saveDateFormat = DateTime.DateFormat: saveTimeFormat = DateTime.TimeFormat
   DateTime.TimeFormat = "z"
   systime_string = DateTime.Time(DateTime.Now) '"GMT-10:00"
   DateTime.DateFormat = "dd/MM/yyyy"
   zt = -DateTime.DateParse("01/01/1970") / DateTime.TicksPerHour
   dst_hour = Abs(systime_string.SubString2(3,6))
   dst_min = systime_string.SubString2(7,9)
   DateTime.DateFormat = saveDateFormat: DateTime.TimeFormat = saveTimeFormat
   Return Abs(Abs(zt) - (dst_hour + dst_min / 60))
End Sub

I would be grateful if someone in a country currently observing DST, could check this on her/his device.

Thanks a lot,

Robert
 
Upvote 0

Creaky

Member
Licensed User
Longtime User
Somehow this doesn's work for me.
When I use this sub it gives me a time difference between dutch time and GMT of 1 hour, while it is actually 2 hours (normal timezone + DST).
Why is this?

I'm desperatly looking for a way to get/calculate the true GMT without resorting to GPS. Can anybody help me out?
 
Upvote 0

alfcen

Well-Known Member
Licensed User
Longtime User
Hi, as long as DST is observed in The Netherlands, what do you get back from this:

DateTime.TimeFormat = "z"
Log(DateTime.Time(DateTime.Now))

Please also compare the result with the system Time Zone settings of your phone.

Actually, I am confused with DST, too (we have no DST here, though); based on user feedbacks I assume that not all phones handle DST in the same fashion.
 
Upvote 0

Creaky

Member
Licensed User
Longtime User
@alfcen

It says "GMT+02:00" which is correct considering DST over here.
Hmmm perhaps I should just parse this string, capture the "+02:00" part and try and add/substract that to/from the local time, that should give me GMT.

The code is:
B4X:
Dim timezone As Long
timezone = -DateTime.DateParse("01/01/1970")
DateTime.TimeFormat = "HH:mm:ss"
Msgbox(DateTime.Time(DateTime.Now - timezone),"") '==> (Returns local time minus just 1 hours instead of two.)
Msgbox(get_dst,"") '==> Returns 1 instead of 2
   
DateTime.TimeFormat = "z"
Msgbox(DateTime.Time(DateTime.Now),"") '==> (Returns GMT+02:00, which is correct)
Log(DateTime.Time(DateTime.Now))
 
Upvote 0

alfcen

Well-Known Member
Licensed User
Longtime User
Hi Creaky,

Provided your TZ is GMT +02:00 and your clock ist set to local time, incl DST, then,
DateTime.Time(DateTime.Now - timezone)
should return GMT.

What is the result of:
Log(-DateTime.DateParse("01/01/1970") / DateTime.TicksPerHour),
"2" or "1"?

Cheers
Robert
 
Upvote 0

alfcen

Well-Known Member
Licensed User
Longtime User
Erel's code returns "2" because he wisely included Time in the date format strings. Anyway, the cause seems to be evident.
 
Upvote 0
Top