Alex, I was too far down the rabbit hole to turn around ? on the bright side, this code is coming up with the answer your users are expecting ? :
[CODE lang="b4x" title="Log Output with annotations"]Waiting for debugger to connect...
Program started.
Hello world!!!
(ArrayList) [3, 13, 2022, 1, 0] 'DST start date 1:00 AM
(ArrayList) [0, 0, 0, 1, 0] 'plus 1 hour (01:00)'
(ArrayList) [3, 13, 2022, 2, 0, 0] '= 2:00:00 AM
(ArrayList) [3, 13, 2022, 20, 35] '08:35 PM (20:35)'
(ArrayList) [0, 0, 0, 10, 47, 16] 'plus 10 hours 47 minutes 16 seconds'
(ArrayList) [3, 14, 2022, 7, 22, 16] '= next day 07:22:16'
Program terminated (StartMessageLoop was not called).[/CODE]
B4X:
Log("Hello world!!!")
Dim S As String = "03/13/2022 01:00"
Log(DateComponents(S))
Dim I As String = "00/00/0000 01:00"
Log(DateComponents(I))
Log(AddDates(S, I))
Dim S As String = "03/13/2022 20:35"
Log(DateComponents(S))
Dim I As String = "00/00/0000 10:47:16"
Log(DateComponents(I))
Log(AddDates(S, I))
Sub DateComponents(D As String) As List 'returns list of day, month, year and (if sepecified) hour, minute, second
'''Dim ComponentMin() As Int = Array As Int( 1, 1, 1800, 0, 0, 0) 'spewin! date increment components mostly 0
Dim ComponentMin() As Int = Array As Int( 0, 0, 0, 0, 0, 0)
Dim ComponentMax() As Int = Array As Int(12,31, 2222, 59, 59, 60) 'leap second is 60
Dim Components As List
Components.Initialize
Dim StringComponents() As String = Regex.Split("[^0-9]", D.Trim)
Dim BadDateFlag As Boolean = (StringComponents.Length < 3 Or StringComponents.Length > 6)
If BadDateFlag = False Then
For I = 0 To StringComponents.Length - 1
If IsNumber(StringComponents(I)) = False Then
BadDateFlag = True
Exit
Else
Dim Temp As Int = StringComponents(I)
If Temp < ComponentMin(I) Or Temp > ComponentMax(I) Then
BadDateFlag = True
Exit
End If
Components.Add(Temp)
End If
Next
End If
If BadDateFlag Then
Log("Bad date: """ & D & """")
Components.Initialize 'return empty list if bad
End If
Return Components
End Sub
Sub AddDates(StartDate As String, Increment As String) As List
Dim StartDateComponents As List = DateComponents(StartDate)
Dim IncrementComponents As List = DateComponents(Increment)
If StartDateComponents.Size = 0 Or IncrementComponents.Size = 0 Then
Dim EmptyList As List
EmptyList.Initialize
Return EmptyList 'return empty list if bad
End If
Do While StartDateComponents.Size < 6
StartDateComponents.Add(0)
Loop
Do While IncrementComponents.Size < 6
IncrementComponents.Add(0)
Loop
Dim Sum As Int = 0
Dim Carry As Int = 0
Sum = StartDateComponents.Get(5) + IncrementComponents.Get(5) 'seconds
Carry = 0
Do While Sum >= 60
Sum = Sum - 60
Carry = Carry + 1
Loop
Dim Second As Int = Sum
Sum = StartDateComponents.Get(4) + IncrementComponents.Get(4) + Carry 'minutes
Carry = 0
Do While Sum >= 60
Sum = Sum - 60
Carry = Carry + 1
Loop
Dim Minute As Int = Sum
Sum = StartDateComponents.Get(3) + IncrementComponents.Get(3) + Carry 'hours
Carry = 0
Do While Sum >= 24
Sum = Sum - 24
Carry = Carry + 1
Loop
Dim Hour As Int = Sum
Dim DaysInMonth As Int = 31
Select StartDateComponents.Get(0) 'month
Case 9, 4, 6, 11:
DaysInMonth = 30
Case 2:
If (StartDateComponents.Get(2) Mod 400) = 0 Then 'year
DaysInMonth = 29
else if (StartDateComponents.Get(2) Mod 100) = 0 Then
DaysInMonth = 28
else if (StartDateComponents.Get(2) Mod 4) = 0 Then
DaysInMonth = 29
Else
DaysInMonth = 28
End If
End Select
Sum = StartDateComponents.Get(1) + IncrementComponents.Get(1) + Carry 'days
Carry = 0
If Sum > DaysInMonth Then
Sum = Sum - DaysInMonth
Carry = Carry + 1
End If
Dim Day As Int = Sum
Sum = StartDateComponents.Get(0) + IncrementComponents.Get(0) + Carry 'months
Carry = 0
Do While Sum > 12
Sum = Sum - 12
Carry = Carry + 1
Loop
Dim Month As Int = Sum
Dim Year As Int = StartDateComponents.Get(2) + IncrementComponents.Get(2) + Carry 'years
Dim ReturnValue As List
ReturnValue.Initialize
ReturnValue.Add(Month)
ReturnValue.Add(Day)
ReturnValue.Add(Year)
ReturnValue.Add(Hour)
ReturnValue.Add(Minute)
ReturnValue.Add(Second)
Return ReturnValue
End Sub