Android Question Knowing if DST is active for a prior or future date

andyp

Member
Licensed User
Hi

It is simple to know if DST is active

B4X:
Dim daylightsavings As Int 
        If IsDaylightInEffect = True Then
            daylightsavings = 1
            Else
                daylightsavings = 0
        End If

However, what if I wanted to know if DST would be active on some past or future date?

If there any way to find this out?

Thank you
Andrew
 

andyp

Member
Licensed User
Sorry....

B4X:
Sub IsDaylightInEffect As Boolean
    Dim currentTimezone As Double = DateTime.TimeZoneOffset
    Dim t As Long = DateTime.Now
    For i = 0 To 11 Step 2
        If DateTime.GetTimeZoneOffsetAt(t) < currentTimezone Then Return True
        t = t + i * DateTime.TicksPerDay * 30
    Next
    Return False
End Sub

My issue is: I want to display the time of sunrise at the users location. I calculate the sunrise time based on GMT. Using the code above, I know if daylight savings is active or not, and add an hour if necessary (DST).

But what if I want to calculate the time of sunrise on some arbitrary date (at the users location). For example, how do I know if DST is active on the 1st March?

Thank you :)
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
B4X:
Sub Process_Globals
   Private MinTimeZone As Double
End Sub

Sub Activity_Create(FirstTime As Boolean)
   If FirstTime Then
       FindMinTimeZone
   End If
   Log(IsDST(DateTime.Now))
   Log(IsDST(DateTime.DateParse("08/01/2000")))
End Sub

Private Sub FindMinTimeZone
   Dim t As Long = DateTime.Now
   MinTimeZone = DateTime.GetTimeZoneOffsetAt(t)
   For i = 1 To 400
       Dim m As Double = DateTime.GetTimeZoneOffsetAt(t)
       If m <> MinTimeZone Then
           MinTimeZone = Min(m, MinTimeZone)
           Exit
       End If
       t = t + DateTime.TicksPerDay
   Next
End Sub

Public Sub IsDST(t As Long) As Boolean
   Return DateTime.GetTimeZoneOffsetAt(t) > MinTimeZone
End Sub
 
Upvote 0

Peter Simpson

Expert
Licensed User
Longtime User
Another solution is this library that does everything for you. You just need to enter your current location via GPS location or FusedLocationProvider, enter the time, and read your gettimezoneoffset from you phone and you are sorted no matter where you are located in the world. Enter one one into the library and it gives you all the results that you need.

https://www.b4x.com/android/forum/threads/astro-library.15975/#content

https://www.b4x.com/android/forum/attachments/watch_time_2-png.62779/
 
Last edited:
Upvote 0

Peter Simpson

Expert
Licensed User
Longtime User
I've updated my last post, you should read it again :)

Yes I've been using that library for years with weather apps that I've created. The library is really easy to use, you will have your app up and running perfect within 10 minutes with basically a hand full of your own lines of code :cool:

Good luck with your app...
 
Last edited:
Upvote 0

andyp

Member
Licensed User
Hi Erel

Just wanted to thank you for showing me this code. Works perfectly, and I can understand the solution.

Thanks again!
Andrew
 
Upvote 0

Abid Hussain

New Member
Licensed User
Hi,

I'm implementing the code, and think that it needs a proviso / note of caution for anyone using it.

When you enter the time, to IsDST make sure it's not midnight of the day, as DST usually switches over about 2am (certainly it does in the UK)

So I've stuck in the following code, which fixed my issues

B4X:
IsDST( t + DateTime.TicksPerHour*12)

oh, and thank you Erel for the code :)
 
Upvote 0
Top