Bug? DateTime error

jccraig

Member
Licensed User
Longtime User
Sub Activity_Create(FirstTime As Boolean)
Dim tim,utc As Long
Dim tz As Double
tim = DateTime.Now 'Get current local time
tz = DateTime.TimeZoneOffset 'Get hours offset from Greenwich
DateTime.SetTimeZone(0) 'Set clock to Greenwich time
utc = DateTime.Now 'Grab the time in Greenwich
'DateTime.SetTimeZone(tz) 'Set the clock back to local time
Msgbox(DateTime.Time(tim) & CRLF & DateTime.Time(utc),"")
End Sub

I believe this code should store current time in variable tim, and Greenwich time in utc. However, both times display as UTC time. Also, if you uncomment the line that sets the time zone back, both variables display as local time. My goal is to get local time in ticks into one long variable, and UTC time into another long variable, so I can work with both in some astronomical calculations. No matter what I try, I'm unable to figure a workaround at this point.

Suggestion... It would be ideal if there was simply DateTime.UTC as a function that would return UTC time without having to mess with the timezone offset, or to have to adjust the local time clock.
 

jccraig

Member
Licensed User
Longtime User
Okay, thanks Erel. I believe this is the right way to accomplish what I wanted...
B4X:
Dim tim,utc As Long

'Get local and Greenwich times
tim = DateTime.now 
utc = DateTime.now - DateTime.TimeZoneOffset * DateTime.ticksperhour

I still think there must be some bug about how two variables are set to two different values yet end up the same, but I'll stay away from that approach with DateTime.
 

jccraig

Member
Licensed User
Longtime User
One failed attempt at a workaround was to store the tim and utc values into other variables, even doing a speck of math on them. My theory was that the tim and utc variables were getting set as references to the time in DateTime, instead of to the actual ticks values. So here's the code I tried. It has the same bizarre result, where output times are the same, either both local time or both Greenwich time...
B4X:
Sub Activity_Create(FirstTime As Boolean)
Dim tim,utc As Long
Dim t0, u0 As Long
Dim tz As Double
tim = DateTime.Now 'Get current local time
t0 = (tim)+1
tz = DateTime.TimeZoneOffset 'Get hours offset from Greenwich
DateTime.SetTimeZone(0) 'Set clock to Greenwich time
utc = DateTime.Now 'Grab the time in Greenwich
u0 = (utc) - 1
'DateTime.SetTimeZone(tz) 'Set the clock back to local time
Msgbox(DateTime.Time(t0) & CRLF & DateTime.Time(u0),"") 
End Sub
 
Top