I'm receiving a time HH:mm:ss from some hardware.
When received, I'm parsing it with:
Then using longTime for various other operations.
The DateTime.TimeParse hint states 'Note that the returned value date will be today'.
The problem is that strTime may not be exactly correct, and around midnight the day it relates to might be different from the current date on the PC.
I'm getting around this by doing:
This obviously won't work if the hardware time and PC time are more than an hour different (although this is pretty unlikely in the real world).
Is there a better way to handle this?
Many thanks.
When received, I'm parsing it with:
B4X:
DateTime.TimeFormat = "HH:mm:ss"
Dim longTime As Long = DateTime.TimeParse(strTime) 'strTime comes from the hardware
The DateTime.TimeParse hint states 'Note that the returned value date will be today'.
The problem is that strTime may not be exactly correct, and around midnight the day it relates to might be different from the current date on the PC.
I'm getting around this by doing:
B4X:
DateTime.TimeFormat = "HH:mm:ss"
Dim longTime As Long = DateTime.TimeParse(strTime) 'strTime comes from the hardware
Dim pcHour As Long = DateTime.GetHour(DateTime.Now)
Dim hwHour As Long = DateTime.GetHour(longTime)
If pcHour=23 And hwHour=0 Then 'hw clock is ahead of PC so add a day to longTime
Dim p As Period
p.Initialize
p.Days=1
longTime=DateUtils.AddPeriod(longTime, p)
Else If pcHour=0 And hwHour=23 Then 'hw clock is behind PC so subtract a day from longTime
Dim p As Period
p.Initialize
p.Days=-1
longTime=DateUtils.AddPeriod(longTime, p)
Else
longTime=longTime
End If
This obviously won't work if the hardware time and PC time are more than an hour different (although this is pretty unlikely in the real world).
Is there a better way to handle this?
Many thanks.