Android Question DateTime Ticks and Time Zones?

daveinhull

Active Member
Licensed User
Longtime User
Hi,

Could someone give me a quick bit of help on how time zones affect (or not) the datetime tick counter, or point me to where I can read up on this.

Basically, does the tick counter include time zone changes, i.e. if I get a tick count for position A and then move to a time zone which is ahead by 1 hour and get another tick count, does it include the 1 hour change or is the tick count fixed, e.g. to UTC?

Thanks in advance
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Ticks = Number of milliseconds since 1.1.1970 UTC. So the ticks value already has a time zone.

When you use DateTime.Date or DateTime.Time the device current time zone will be applied.

The following code should show the correct time string based on the device time zone. Note that if you want to support time zones changes while the app is running then you need to call DateTeim.ListenToExternalTimeChanges.
B4X:
Log(DateTime.Date(DateTime.Now))
Log(DateTime.Time(DateTime.Now))
 
Upvote 0

daveinhull

Active Member
Licensed User
Longtime User
Thanks Erel,

So if I just want to keep an absolute time I can just use ticks.

If it's got a date and time and a time zone offset and I want to get back to the absolute time in ticks, I'd need to convert the time into ticks using the built in function and then subtract or add the time zone offset times ticks per hour?

Thanks
Ticks = Number of milliseconds since 1.1.1970 UTC. So the ticks value already has a time zone.

When you use DateTime.Date or DateTime.Time the device current time zone will be applied.

The following code should show the correct time string based on the device time zone. Note that if you want to support time zones changes while the app is running then you need to call DateTeim.ListenToExternalTimeChanges.
B4X:
Log(DateTime.Date(DateTime.Now))
Log(DateTime.Time(DateTime.Now))
 
Upvote 0

daveinhull

Active Member
Licensed User
Longtime User
Hi Erel,

What I'm doing is storing the date and time of an event in ticks in a DB. The user can list these events and their dates and time which is shown as a string. They can edit the string date and time. So I want to convert their edited date and time back into ticks, which is easy. But I also need to handle time zones as an event can start in one time zone and finish in another like taking a flight. So I also store the time zone of the event and also allow them to change this. So I have a string date and time and a floating time zone value.

I need the absolute time value so that I can calculate durations of the events. So I just want to get clear how time zones affect or should be used to correct tick values when recreating the tick from a string date and time in different time zones.

How I've explained what I'm trying to do.

Thanks
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
As I see it, you have two options:

1. Use DateUtils.SetDateAndTime2. You will need to parse the date and time string yourself. It should be simple.
2. Use a DateFormat that includes the time and the timezone. See this example: https://www.b4x.com/android/forum/threads/date-time-to-utc.51569/#post-323244

Don't try to calculate the time zone offset. It is very difficult to calculate it correctly when DST changes.

Make sure to use DateUtils.PeriodBetween to calculate durations.
 
Upvote 0

daveinhull

Active Member
Licensed User
Longtime User
Many thanks Erel, I think I get it. So just work with a time format that includes the time zone and use built in functions to do all the work. So I don't need to store ticks, just the formated time.

Thanks
 
Upvote 0

daveinhull

Active Member
Licensed User
Longtime User
HI Erel,

Been thinking about this a little further, and out of pure understanding, what would be wrong with:
1. Get Ticks from the built in function for a given string date and time - this should give UTC time
2. Calculate the number of ticks caused by the time zone - Time Zone * TicksPerHour
3. Subtracting (given that it might be a negative time zone offset) the calculated ticks in 2 to the ticks given in 1

So if it is 5:45 in Europe (time zone 2) 1. gives me ticks for 5:45 in UTC
The time zone (2) times TicksPerHour gives me the number of ticks for the 2 hour time zone
and then subtracting gives me the ticks for 3:45 which would be the real UTC time?

I wouldn't need to worry about daylight saving as this would be accommodated by the universal time zone and the fact that I'm always going back to UTC.

as I said I get what you have sad, but just wondering where I'm missing something with the above - useful learning.

Many thanks
Dave
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
1. Get Ticks from the built in function for a given string date and time - this should give UTC time
This is only correct if you call DateTime.SetTimeZone(0), otherwise it will parse the time string in the current device time zone.

The important point is that any date or time string should include the timezone.
 
Upvote 0
Top