Android Question Daylight Saving Time question

Alex_197

Well-Known Member
Licensed User
Longtime User
Hi all.

I have a problem with checking if DST has started.

Here is my code - this is just an example
Code: Copy
private Sub CheckIsDST
  
    Try
      
        Dim Offset1 As Double,Offset2 As Double
        Dim Date1 As String,Date2 As String
      
        Date1="03/12/2021"
        Date2="03/13/2021"
      
        Offset1=DateTime.GetTimeZoneOffsetAt(DateTime.DateParse(Date1))
        Offset2=DateTime.GetTimeZoneOffsetAt(DateTime.DateParse(Date2))
      
        Log("Date1=" & Date1 &  TAB &  "Date2=" & Date2 & TAB &  "Offset1=" & Offset1 & TAB & "Offset2=" & Offset2)
      
        'DST started on 03/14/2021 at 1:30 AM so I expected that offset 1 and offset 2 will be different             
        Date1="03/13/2021"
        Date2="03/14/2021"
      
        Offset1=DateTime.GetTimeZoneOffsetAt(DateTime.DateParse(Date1))
        Offset2=DateTime.GetTimeZoneOffsetAt(DateTime.DateParse(Date2))
      
        Log("Date1=" & Date1 &  TAB &  "Date2=" & Date2 & TAB &  "Offset1=" & Offset1 & TAB & "Offset2=" & Offset2 & TAB & "DST started")
      
        Date1="03/14/2021"
        Date2="03/15/2021"
        'Why it happens here - 24 hours later?
        Offset1=DateTime.GetTimeZoneOffsetAt(DateTime.DateParse(Date1))
        Offset2=DateTime.GetTimeZoneOffsetAt(DateTime.DateParse(Date2))
      
        Log("Date1=" & Date1 &  TAB &  "Date2=" & Date2 & TAB &  "Offset1=" & Offset1 & TAB & "Offset2=" & Offset2)
      
        Date1="03/15/2021"
        Date2="03/16/2021"
      
        Offset1=DateTime.GetTimeZoneOffsetAt(DateTime.DateParse(Date1))
        Offset2=DateTime.GetTimeZoneOffsetAt(DateTime.DateParse(Date2))
      
        Log("Date1=" & Date1 &  TAB &  "Date2=" & Date2 & TAB &  "Offset1=" & Offset1 & TAB & "Offset2=" & Offset2)
      
    Catch
        Log("CheckIsDST " & LastException)     
    End Try
  
End Sub

My app checks if DST has started and shows a message to the user that he needs to wait for 1 hour until the device will switch to either summer or winter time. I've got a call from one of the user that he got this message on the night from 3/14/2021 to 3/15/2021. It was weird because DST night in the USA was 24 hours earlier (at 1:30 AM 03/14/2021).

I checked my code and I found that offsets the night before the DST are the same (Offset1=-5 and Offset2=-5) for dates 03/12/2021 and 03/13/2021. It's correct

I expected that offsets will be different for dates 03/13/2021 and 03/14/2021. But they are the same (Offset1=-5 and Offset2=-5). It's weird - we switched to the DST on 03/14/2021.

And finally offsets are different for 03/14/2021 and 03/15/2021 - Offset1=-5 and Offset2=-4.

Here is a log

Code: Copy
--------- beginning of system
** Activity (main) Pause, UserClosed = false **
** Activity (main) Create, isFirst = false **
** Activity (main) Resume **
** Activity (main) Create, isFirst = true **
Date1=03/12/2021    Date2=03/13/2021    Offset1=-5    Offset2=-5
Date1=03/13/2021    Date2=03/14/2021    Offset1=-5    Offset2=-5    DST started
Date1=03/14/2021    Date2=03/15/2021    Offset1=-5    Offset2=-4
Date1=03/15/2021    Date2=03/16/2021    Offset1=-4    Offset2=-4
** Activity (main) Resume **

So my question is - what am I doing wrong?

I attached my small project.
 

Attachments

I expected that offsets will be different for dates 03/13/2021 and 03/14/2021. But they are the same (Offset1=-5 and Offset2=-5). It's weird - we switched to the DST on 03/14/2021.
The answer to your first post: it is not weird. DST didn't change on 00:00 so the offsets are the same. As DST changes at 01:00 or 02:00 you can check it on 13:00 to be sure that you check after the DST change.
 
Upvote 0
So how do I know that in 2021 it will be 03/14?

DST in the USA Today. Daylight Saving Time (DST) in the USA starts on the 2nd Sunday in March and ends on the 1st Sunday in November.
In Europe it is last Sunday in March till last Sunday in October.
 
Upvote 0
Is it possible to implement in B4x

B4X: Copy
Public Function IsDST() As Boolean

        Dim b As Boolean = False, b1 As Boolean = False

        b = Now.IsDaylightSavingTime
        b1 = Today.IsDaylightSavingTime

        Return b1

    End Function
 
Upvote 0
You must be more accurate. Do you want to find the two days where DST changes?
All I need to know that current date is when DST started or ended to show a message to the user that he needs to wait for 1 hour between 1:30 AM and 2:30 AM.
 
Upvote 0
All I need to know that current date is when DST started

Something like this will work, until Erel provides you a more elegant way.
Code: Copy
DateTime.DateFormat="MM/dd/yyyy HH:mm"
    Try        
        Dim Offset1 As Double,Offset2 As Double
        Dim Date1 As String,Date2 As String
        
        Date1="03/12/2021 3:00"
        Date2="03/13/2021 3:00"
        Offset1=DateTime.GetTimeZoneOffsetAt(DateTime.DateParse(Date1))
        Offset2=DateTime.GetTimeZoneOffsetAt(DateTime.DateParse(Date2))        
        Log("Date1=" & Date1 &  TAB &  "Date2=" & Date2 & TAB &  "Offset1=" & Offset1 & TAB & "Offset2=" & Offset2)
        
        'DST started on 03/14/2021 14:00 at 1:30 AM so I expected that offset 1 and offset 2 will be different                
        Date1="03/13/2021 3:00"
        Date2="03/14/2021 3:00"
        Offset1=DateTime.GetTimeZoneOffsetAt(DateTime.DateParse(Date1))
        Offset2=DateTime.GetTimeZoneOffsetAt(DateTime.DateParse(Date2))        
        Log("Date1=" & Date1 &  TAB &  "Date2=" & Date2 & TAB &  "Offset1=" & Offset1 & TAB & "Offset2=" & Offset2 & TAB & "DST started")
        
        Date1="03/14/2021 3:00"
        Date2="03/15/2021 3:00"
        'Why it happens here - 24 hours later?
        Offset1=DateTime.GetTimeZoneOffsetAt(DateTime.DateParse(Date1))
        Offset2=DateTime.GetTimeZoneOffsetAt(DateTime.DateParse(Date2))        
        Log("Date1=" & Date1 &  TAB &  "Date2=" & Date2 & TAB &  "Offset1=" & Offset1 & TAB & "Offset2=" & Offset2)
        
        Date1="03/15/2021 3:00"
        Date2="03/16/2021 3:00"
        Offset1=DateTime.GetTimeZoneOffsetAt(DateTime.DateParse(Date1))
        Offset2=DateTime.GetTimeZoneOffsetAt(DateTime.DateParse(Date2))        
        Log("Date1=" & Date1 &  TAB &  "Date2=" & Date2 & TAB &  "Offset1=" & Offset1 & TAB & "Offset2=" & Offset2)
        
    Catch
        Log("CheckIsDST " & LastException)        
    End Try
 
Upvote 0
Code: Copy
Sub AppStart (Args() As String)
    Log(DidTimeZoneChange(DateTime.Now)) 'False
    Log(DidTimeZoneChange(DateTime.DateParse("03/26/2021"))) 'True in my time zone
    Log(DidTimeZoneChange(DateTime.DateParse("10/31/2021"))) 'True
    Log(DidTimeZoneChange(DateTime.DateParse("11/21/2021"))) 'False
End Sub

Private Sub DidTimeZoneChange (date As Long) As Boolean
    Dim Start As Long = DateUtils.SetDateAndTime(DateTime.GetYear(date), DateTime.GetMonth(date), DateTime.GetDayOfMonth(date), 0, 0, 0)
    Dim EndDate As Long = DateUtils.SetDateAndTime(DateTime.GetYear(date), DateTime.GetMonth(date), DateTime.GetDayOfMonth(date), 23, 0, 0)
    Return DateTime.GetTimeZoneOffsetAt(Start) <> DateTime.GetTimeZoneOffsetAt(EndDate)
End Sub
 
Upvote 0

Similar Threads

Back
Top