Confused by DateTime

david ashkenazi

New Member
Licensed User
Longtime User
I am trying to keep track of countdown to future events.
Basically I substract time ticks (in long).

I get the correct countdowns, but when I try to display that in string (hour, minute, seconds) I get an anwser biased by two hours.

Here is the code:

B4X:
Sub UpdateCountDowns(rightnow As Long)
Dim i As Int 


DateTime.TimeFormat = "dd/MM/yyy HH:mm:ss"

For  i = 0 To 2

   Log("Event " & i & " at: " & Evt(i).EventTimeA & ", " & DateTime.Time(Evt(i).EventTimeA))
   Log("Now is: " & rightnow & ", " & DateTime.Time(rightnow))
   Evt(i).CountDownA = Evt(i).EventTimeA - rightnow
   Log("set count down to: " & Evt(i).CountDownA & ", " & DateTime.Time(Evt(i).CountDownA))
Next   

   Log ("GetTimeZoneOffsetAt: " & DateTime.GetTimeZoneOffsetAt(rightnow))
    Log ("TimeZoneOffset: " & DateTime.TimeZoneOffset)
End Sub

And log file
B4X:
Event 0 at: 1335919936936, 02/05/2012 03:52:16
Now is: 1335918136936, 02/05/2012 03:22:16
set count down to: 1800000, 01/01/1970 02:30:00
Event 1 at: 1335924028482, 02/05/2012 05:00:28
Now is: 1335918136936, 02/05/2012 03:22:16
set count down to: 5891546, 01/01/1970 03:38:11
Event 2 at: 1335940578412, 02/05/2012 09:36:18
Now is: 1335918136936, 02/05/2012 03:22:16
set count down to: 22441476, 01/01/1970 08:14:01
Event 3 at: 1335940897920, 02/05/2012 09:41:37
Now is: 1335918136936, 02/05/2012 03:22:16
set count down to: 22760984, 01/01/1970 08:19:20
Event 4 at: 1335953797749, 02/05/2012 13:16:37
Now is: 1335918136936, 02/05/2012 03:22:16
set count down to: 35660813, 01/01/1970 11:54:20
Event 5 at: 1335968292327, 02/05/2012 17:18:12
Now is: 1335918136936, 02/05/2012 03:22:16
set count down to: 50155391, 01/01/1970 15:55:55
Event 6 at: 1335976269580, 02/05/2012 19:31:09
Now is: 1335918136936, 02/05/2012 03:22:16
set count down to: 58132644, 01/01/1970 18:08:52
Event 7 at: 1335975619889, 02/05/2012 19:20:19
Now is: 1335918136936, 02/05/2012 03:22:16
set count down to: 57482953, 01/01/1970 17:58:02
Event 8 at: 1335982819889, 02/05/2012 21:20:19
Now is: 1335918136936, 02/05/2012 03:22:16
set count down to: 64682953, 01/01/1970 19:58:02
GetTimeZoneOffsetAt: 3
TimeZoneOffset: 3

For example the first event gives correctly 1800000 ticks, but this is displayed as 2:30:00 instead of 30 minutes.

What did I wrong?
 

lagore

Active Member
Licensed User
Longtime User
You are doing a date.time on the difference between timenow and event time which is the elapsed time but date.time converts this to a date and a time from the epoch Jan1 1970

Sent from my HTC Desire using Tapatalk 2

Sent from my HTC Desire using Tapatalk 2
 
Upvote 0

david ashkenazi

New Member
Licensed User
Longtime User
You are doing a date.time on the difference between timenow and event time which is the elapsed time but date.time converts this to a date and a time from the epoch Jan1 1970

Sent from my HTC Desire using Tapatalk 2

Sent from my HTC Desire using Tapatalk 2

That is correct, this is why the result shows the date part as Jan 1 1970, but when I extract the time (or convert to string with the appropriate timeformat), I still get an offset of 2 hours.

My problem is not regarding the 'date' part in the log, but the 'time part'
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
The time zone at Jan 1 1970 can be different due to daylight saving. It is better to use a method such as this one:
B4X:
Sub ConvertTicksToTimeString(t As Long) As String
   Dim hours, minutes, seconds As Int
   hours = t / DateTime.TicksPerHour
   minutes = (t Mod DateTime.TicksPerHour) / DateTime.TicksPerMinute
   seconds = (t Mod DateTime.TicksPerMinute) / DateTime.TicksPerSecond
   Return NumberFormat(hours, 2, 0) & ":" & NumberFormat(minutes, 2, 0) & ":" & NumberFormat(seconds, 2, 0)
End Sub
 
Upvote 0
Top