Android Question Find if a given date & GMT should have DST applied?

malcfreem

Member
Licensed User
Longtime User
Help please...
I am getting the date and GMT time from the atomic online pool. That works fine every time.

What I then want to do is check if that date and time should have DST applied so I can get the ACTUAL time if you were in London looking at your watch.

Can someone please show me the code which can take my GMT time and date and check if that date and time is or isnt in DST in the UK?

Thanks for any help you can give
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
You can use this code:
B4X:
Sub Activity_Create(FirstTime As Boolean)
   Dim currentOffset As Double = DateTime.TimeZoneOffset
   SetTimeZone("Europe/London")
   Log(DateTime.TimeZoneOffset) '1 -> DST
   DateTime.SetTimeZone(currentOffset)
End Sub

Sub SetTimeZone(id As String)
  Dim r As Reflector
  Dim idTz As Object = r.RunStaticMethod("java.util.TimeZone", "getTimeZone", Array As String(id), _
  Array As String("java.lang.String"))
  r.RunStaticMethod("anywheresoftware.b4a.keywords.DateTime", "setTimeZoneInternal", _
  Array As Object(idTz), Array As String("java.util.TimeZone"))
End Sub
 
Upvote 0

malcfreem

Member
Licensed User
Longtime User
Thank you very much both. Erel I'm afraid I don't fully understand what your code is doing. Could users not affect the result by advancing the time on their device?

I need a device independent solution, so that users cannot affect the time returned by altering date, time, timezone or locale on their device, which is why I go to the atomic pool online, forgive me if I have misunderstood your code.
 
Upvote 0

nwhitfield

Active Member
Licensed User
Longtime User
If it's just London (or other European cities) you are worried about, you could probably code fairly simply using the standard rules, which are fixed for all European countries (unlike some places where they change the rules from year to year, to accomodate sporting events, etc!)

Summer time starts on the last Sunday in March
Summer time ends on the last Sunday in October

Changeover is at 0100 GMT/UTC

So, use the DateUtils module; find out what day of the week the 31st of March and October are. If they're a Sunday, then that's the date of the change; if not, work backwards to the preceding Sunday

Something like

B4X:
Dim MarchEnd As Long
Dim DSTstart As Int
MarchEnd = DateUtils.SetDate( 2014, 3, 31 )
If DateTime.GetDayOfWeek(MarchEnd) = 1 Then
  DSTstart = 31
Else
  DSTstart = 32 - DateTime.GetDayOfWeek(MarchEnd)
End if

I've assumed without checking that the day is Sunday when GetDayOfWeek returns 1; if not, you just have to change the maths; check for 7 in the if and subtract from 31 in the Else.
 
Upvote 0

RjCl

Member
Licensed User
Longtime User

You need to get date and time from a place that provides that data, such as this free api :-

http://timezonedb.com/api

gives Yes/No if DST is on or not.

Why not get it from the above link? Its free, it gives you the DST info and users can't mess about by changing time/date ?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
The time zone information of all time zones is available as part of the OS. The code I posted will work on all devices and will not be affected by the user settings.

It sets the process time zone (not device tome zone) to the london time zone and then check the current offset. Then it returns the time zone to the previous one.
 
Upvote 0

nwhitfield

Active Member
Licensed User
Longtime User
As a matter of interest, Erel, how often is that data in the OS updated? Do you know if it's fetched dynamically, or baked in?
 
Upvote 0

Laurent95

Active Member
Licensed User
Longtime User

Hello,
Even this post is very old, in case some read it, and use it, be careful.
That's not a bad solution, but like as many servers on web it's limited, and their limits are really low.
1 request by second where Google give the same with 100 requests by second and 2500 requests by day.
Then if you want use it for a deployed application, that's probably not work.

Regards.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…