DateTime.SetTimeZone should affect the conversions of dates to ticks and viceversa:
Is it correct? If so, where is my error in this code?
Is it correct? If so, where is my error in this code?
B4X:
Dim AbsTicks As Long = AbsDateTicks(DateTime.Now)
'------------------------------------------------
Sub AbsDateTicks(DateTicks As Long) As Long
Dim Result As Long
' Stores the current date format.
Dim CurrentDateFormat As String = DateTime.DateFormat
' Stores the device time zone offset.
Dim DeviceTimeZoneOffset As Int = GetTimeZoneOffset
' Changes the date format.
DateTime.DateFormat = "MM/dd/yyyy hh:mm:ss"
LogColor("date : " & DateTime.Date(DateTicks), Colors.Blue)
LogColor("ticks : " & DateTicks, Colors.Blue)
' Sets the time zone offset to 0.
' It does not change the device setting.
DateTime.SetTimeZone(0)
Dim AbsoluteDate As String = DateTime.Date(DateTicks)
LogColor("'absolute' date : " & AbsoluteDate, Colors.Blue)
Result = DateTime.DateParse(DateTime.Date(DateTicks))
LogColor("'absolute' ticks: " & DateTicks, Colors.Blue)
' Resets the previous date format.
DateTime.DateFormat = CurrentDateFormat
' Resets the time zone offset.
DateTime.SetTimeZone(DeviceTimeZoneOffset)
Return Result
End Sub
Sub GetTimeZoneOffset As Int
Dim s, d As String
Dim l As Long
s = DateTime.DateFormat
DateTime.DateFormat = "MM/dd/yyyy HH:mm:ss"
l = DateTime.Now
d = DateTime.Date(l) & " GMT"
DateTime.DateFormat = "MM/dd/yyyy HH:mm:ss z"
Dim res As Int
res = -Round((l - DateTime.DateParse(d))/3600000)
DateTime.DateFormat = s
Return res
End Sub
Last edited: