Android Question DST problem

Alex_197

Well-Known Member
Licensed User
Longtime User
Hi all.

I have a problem with DateUtils.AddPeriod and DST which is In the USA today November, 7 2021

Here is my code
Add 1 hour:
Private Sub GetStrTo(strFrom As String, AddHour As Int) As String
    
    Try
        
        Dim Arr() As String
        Dim NotTimeDate As String,NoteTimeStr As String
        Dim P As Period
        Dim Tick As Long ,NewTick As Long
        Dim strTo As String
            
        P.Hours=AddHour
        
        Arr=Regex.Split(" ", strFrom)
        
        NotTimeDate=Arr(0)
        NoteTimeStr=Arr(1) & " " & Arr(2)
        strFrom=NoteTimeStr
            
        Tick=DateTime.DateTimeParse(NotTimeDate,NoteTimeStr)
        NewTick=DateUtils.AddPeriod(Tick,P)
        strTo=DateUtils.TicksToString(NewTick)
                
        Return strTo
        
        
    Catch
        Log("GetStrTo " & LastException.Message)
        
        Return "Error"
    End Try
    
End Sub
    
End Sub

I attached a small project so you can try yourself.

My question is - why if I add 1 hour to 11/7/2021 12:00 AM - my code returns 11/7/2021 1:00 AM. If I add 1 hour to 11/7/2021 1:00 AM - my code returns 11/7/2021 1:00 AM and if I add 1 hour to 11/7/2021 2:00 AM - my code returns 11/7/2021 3:00 AM.

Is it because DST changes on 11/7/2021 at 2:00 AM?

But if I add not 1 but 2 hours

11/7/2021 12:00 AM returns 11/7/2021 1:00 AM

11/7/2021 1:00 AM returns 11/7/2021 2:00 AM

11/7/2021 2:00 AM returns 11/7/2021 4:00 AM

Thanks.
 

DonManfred

Expert
Licensed User
Longtime User
B4X:
Sub Button1_Click
    Log(GetStrTo("11/7/2021 12:00:00",1))
End Sub

Private Sub GetStrTo(strFrom As String, AddHour As Int) As String
    
    Try
        
        Dim Arr() As String
        'Dim NotTimeDate As String,NoteTimeStr As String
        Dim DateStr As String, TimeStr As String
        Dim P As Period
        Dim Tick As Long ,NewTick As Long
        Dim strTo As String
            
        P.Hours=AddHour
        
        Arr=Regex.Split(" ", strFrom)
        
        DateStr=Arr(0)
        TimeStr=Arr(1)
        'strFrom=NoteTimeStr
       'Log($"Date ${DateStr}"$)
       'Log($"Time ${TimeStr}"$)
       'Log($"Timeformat ${DateTime.TimeFormat}"$)
                
        Tick=DateTime.DateTimeParse(DateStr,TimeStr)
        NewTick=DateUtils.AddPeriod(Tick,P)
        strTo=DateUtils.TicksToString(NewTick)
                
        Return strTo
        
        
    Catch
        Log("GetStrTo " & LastException.Message)
  '     
        Return "Error"
    End Try
    
End Sub

Outputs 11/07/2021 13:00:00
 
Upvote 0

Alex_197

Well-Known Member
Licensed User
Longtime User
Now try
B4X:
Log(GetStrTo("11/7/2021 1:00:00 AM",1))
 
Upvote 0

Alex_197

Well-Known Member
Licensed User
Longtime User
In your case you sent "11/7/2021 12:00:00" which is 12:00 PM in the USA format and the output is "11/7/2021 13:00:00" which is 1 PM in USA format.



But I need to send 1:00 AM which is 01:00:00. For time after 2 PM my example works fine. The problem is only with hours between 1:00 AM and 1:59 AM.

After DST happens which is 2:00 AM everything works fine.
 
Upvote 0

Alex_197

Well-Known Member
Licensed User
Longtime User
What are you actually trying to do? As I said above it is all to do with clock times, not duration. One hour after 1:30 AM on the clock will be 1:30 AM again clock time as the hour goes back during that interval.
In my app user opens a screen for a 1st time today and we record a timestamp. He needs to type some note in order this not will be counted.

Let's say the first time it was at 12:00 AM 11/07/2021. He returns at 6 AM 11/7/2021 so we need to add 5 records to the table for each hour.

Each of these records has NoteTimeFromn and NoteTimeTo fields so the user will see a list of 5 buttons each with NoteTimeFrom and NoteTimeTo like

12 AM - 1 AM
1 AM - 2 AM
2 AM - 3 AM
4 AM - 5 AM
5 AM - 6 AM

yesterday it was ok but today we have this

12 AM - 1 AM
1 AM - 1 AM
1 AM - 1 AM
1 AM - 1 AM
1 AM - 1 AM
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
I don't understand why your second table looks like repeated 1AM - 1AM periods, but I reiterate that you are dealing with clock times not durations. Somehow somewhere your app will need to deal with that extra or missing subjective hour. Perhaps you should do all your calcuclations in UTC.
 
Upvote 0

Alex_197

Well-Known Member
Licensed User
Longtime User
Thanks for your help. My second table looks like this because my code is tryibng to add 1 hour to 1 AM and gets the same 1 AM due to UTC. I know already how to fix it.
 
Upvote 0

Alex_197

Well-Known Member
Licensed User
Longtime User
Just one more question.

Today is DST date when clock is set to 1 hours back so if I try to add 1 hour to 11/07/2021 01:00 AM I'm getting the same date and time 11/07/2021 01:00 AM.

But ket's assume that today is 03/13/2022 which is also DST date when clock will be set to 1 hour forward. What would happen if I will try to add 1 hour to 03/13/2022 1:00 AM? Will it be 2 AM or 3 AM? Is there any way to test it before the actuald DST date?

Thanks.
 
Upvote 0

Alex_197

Well-Known Member
Licensed User
Longtime User
I assume it will go forward at 02:00 so adding one hour at, say, 01:30 should give 03:30. Why can't you just test it the code above?
Thanks for your reply. Why I can't test the code? Because the same code excuted on DST date (either in Nov, 8 2021 or Mar, 13 2022) will work dufferently than in any regular days.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…