Time zone issue

malcfreem

Member
Licensed User
Longtime User
Hi all. In testing my app, which now works fine for me here in the UK, I stumbled upon a problem when my app is used in a different timezone...

My app stores dates and times in the users calendar. These dates and times are all in UK local time. If I set my tablet and my calendar to for example Eastern time (5 hours behind the UK) and then add an event which takes place at 15:00 UK time, the calendar still shows it at 15:00 (but it should be 5 hours earlier in Eastern time).

What I think I have to do is find the devices timezone ( inc DST) and adjust the start time in my app. Any ideas how I could do this, please?

Thanks very much
 

mc73

Well-Known Member
Licensed User
Longtime User
You can check this. Still, before altering your code, I was thinking that if I am supposed to be present somewhere at a specific time, I would choose to schedule based on the event's local time. Just a thought.
 
Upvote 0

malcfreem

Member
Licensed User
Longtime User
timezones

Thanks for your replies...
1) some of the events are on sale dates and times, which u would do from home
2) Erel, the user doesn't enter a date of time. These are football fixtures / sale dates which take place in the UK on fixed dates at fixed times eg London, 01/01/2015, 15:00. But if u were in the US (on EST), the start time would be 10:00 in your local time (if u wanted to watch it live on TV u would do so at10:00, not 15:00)

Sorry I hadn't explained that
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Are you using DateTime module?

You should add this code to the module:
B4X:
'Returns the ticks value of the given date and time with the specified time zone offset.
'The last parameter is the time zone offset measured in hours.
Public Sub SetDateAndTime2(Years As Int, Months As Int, Days As Int, Hours As Int, Minutes As Int, Seconds As Int, _
      TimeZone As Double) As Long
   Dim df As String = DateTime.DateFormat
   DateTime.DateFormat = "GGyyyyMMddHHmmssz"
   Dim d As String = Format(Abs(Years), 4) & Format(Months, 2) & Format(Days, 2)
   d = GetEra(Years < 0) & d
   Dim tzMinutes As Int = Abs(TimeZone * 60)
   Dim tzHours As Int = tzMinutes / 60
   Dim tzSign As String
   If TimeZone >= 0 Then tzSign = "+" Else tzSign = "-"
   d = d & Format(Hours, 2) & Format(Minutes, 2) & Format(Seconds, 2) & _
      "GMT" & tzSign & Format(tzHours, 2) & ":" & Format(tzMinutes Mod 60, 2)
   Try
      Dim ticks As Long = DateTime.DateParse(d)
   Catch
      DateTime.DateFormat = df
      Log("Error: Invalid value: " & d)
      Return "invalid date" + 1 'hack to throw an error
   End Try
   DateTime.DateFormat = df
   Return ticks
End Sub
This will allow you to explicitly set the timezone of the specified date / time.
 
Upvote 0

malcfreem

Member
Licensed User
Longtime User
too good for me!

Thanks Erel!

That is really great of you to give me the code.

You are so much better at this than me and I am a bit confused with what the code is doing.

I am currently simply finding the time in millisecs since the epoch and using this.

I could easily incorporate datetime2 - I assume would just call it with the date and time of my event? How (and in what format) would I get my result?

I find it easier to quote an example, so say my event is on 23/08/2013 in London at 15:00 best (GMT +1). Assume the device is in New York (EST = GMT -4). I call the code with my London date and time. What is returned and where?

Thanks again for all your brilliant support
 
Upvote 0

malcfreem

Member
Licensed User
Longtime User
nearly there

Hi. I inserted your code into my app, but the "format' and 'Getera' keywords are highlighted in red. Am I missing a necessary Date time or strings library?

M
 
Upvote 0

malcfreem

Member
Licensed User
Longtime User
Working! One final question tho.

Brilliant! Thanks so much Erel-that works perfectly and I am delighted.

Can I please ask one final question?

For my app, I have hard coded the timezone which I pass to SetDateTime2to be 1. Will this need to change with DST? (ie become zero)?

Thanks very much for everything.

Malc:sign0060::sign0060:
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
For my app, I have hard coded the timezone which I pass to SetDateTime2to be 1. Will this need to change with DST? (ie become zero)?
The time zone of the local device is handled automatically. However this is not the case with the time instance set with SetDateAndTime2. You are responsible for setting the correct time zone of the event.
 
Upvote 0

malcfreem

Member
Licensed User
Longtime User
Sorry. Poorly explained question

Erel, apologies - on re-reading that last post I worded my question badly.

It is this:-

Does DST, when it changes, alter the timezone itself, or does it just alter the time associated with the timezone?

i.e. If I am in timezone one now (GMT+1), will I be in the same timezone on Christmas Day, or will I then be in timezone zero (GMT).

Thanks
 
Upvote 0

malcfreem

Member
Licensed User
Longtime User
Me being thick?

Erel, I think I have misunderstood you. I thought you were telling me all I had to do is call DateUtils.SetDateAndTime(Yr,Mo,Da,Ho,Mi,Se,Tz) where timezone is the timezoneoffset for that date. It doesn't work for me.

Here is my code:-

UTC1 = DateUtils.SetDateAndTime(Yr,Mo,Da,Ho,Mi,Se)
Tz=DateTime.GetTimeZoneOffsetAt(UTC1)
UTC2=DateUtils.SetDateAndTime2(Yr,Mo,Da,Ho,Mi,Se,Tz)

This work fine for all dates in the UK.
First I ran the app using todays date (GMT+1) It worked for all dates thru to May 2014 (including winter dates where the fixtures are played on GMT+0).

Next I set the tablet date to 25/12/2012 (in the GMT+0 timezone) and ran again. Again it worked for all dates.

Next I set my tablet's date back to today and set the tablet's timezone to EST (GMT-4). I reloaded the calendar and it shows all the fixtures at the correct LONDON time, ie 15:00, not 10:00 when the game is played in New York.

I have fiddled with the calendar settings (General settings, Home time zone set to either EST or BST and 'use home time zone'). EST shows the time as 15:00, not 10:00. BST makes it worse. Fixures show at 20:00, not 10:00.

Is there a way of making SetDateandtime2 work as I need, ie when a fixture starts at 15:00 in London (either BST or GMT, depending on the date), then the calendar date will show as 15:00 in the UK, 10:00 in the US, 17:00 in Germany, etc?

Its driving me nuts!

Malc
 
Upvote 0

malcfreem

Member
Licensed User
Longtime User
what if

Hi. OK, I see that. But dont I need to know, for each of 40 fixtures, whether that fixture is in timezone 1 (BST) in say September, or timezone 0 (GMT) in say January?

That is what I was trying to achieve with my sample code, above

Thanks for all your help Erel
 
Upvote 0

malcfreem

Member
Licensed User
Longtime User
Well I must be being obtuse then

Erel, I thought I was actually doing that with this code...

(where Yr,Mo,Da,etc is the event date and time)

UTC1 = DateUtils.SetDateAndTime(Yr,Mo,Da,Ho,Mi,Se)
Tz=DateTime.GetTimeZoneOffsetAt(UTC1)
UTC2=DateUtils.SetDateAndTime2(Yr,Mo,Da,Ho,Mi,Se,Tz)

The code doesnt work-what am I doing wrong?

Cheers

Malc
 
Upvote 0

malcfreem

Member
Licensed User
Longtime User
yes but

Hi again Erel.

OK, I understand that. I guess my question is

How do I tell the timezone in the UK for any date, from anywhere in the world? (Is the UK on DST for the event date or not?).

Malc
 
Upvote 0
Top