Derek Johnson Active Member Licensed User Longtime User Nov 4, 2017 #1 I've was trying to display the time between 2 events in hours, minutes and seconds using this type of code Code: Copy Sub TestSub Dim t1, t2 As Long t1=DateTime.now Sleep(5000) t2=DateTime.now Log("Elapsed time: " & DateTime.time(t2-t1)) End Sub I expected this to display: Elapsed time: 00:00:05 but instead it displayed: Elapsed time: 01:00:05So the duration displayed an extra hour. My Local time is currently GMT (CST) and DateTime.TimeZoneOffset =0 Time(0) shows as Thu 01 Jan 1970 01:00:00 Is this the expected result? Derek Last edited: Nov 4, 2017
I've was trying to display the time between 2 events in hours, minutes and seconds using this type of code Code: Copy Sub TestSub Dim t1, t2 As Long t1=DateTime.now Sleep(5000) t2=DateTime.now Log("Elapsed time: " & DateTime.time(t2-t1)) End Sub I expected this to display: Elapsed time: 00:00:05 but instead it displayed: Elapsed time: 01:00:05So the duration displayed an extra hour. My Local time is currently GMT (CST) and DateTime.TimeZoneOffset =0 Time(0) shows as Thu 01 Jan 1970 01:00:00 Is this the expected result? Derek
M Mahares Expert Licensed User Longtime User Nov 4, 2017 #2 Derek Johnson said: I expected this to display: Elapsed time: 00:00:05 Click to expand... Your code should be like this: Code: Copy t1=DateTime.now Sleep(5000) t2=DateTime.now Dim p As Period p=DateUtils.PeriodBetweenInDays(t1,t2) Log($"${p.Days} days ${p.Hours} hours ${p.Minutes} min ${p.Seconds} sec"$) '0 days 0 hours 0 min 5 sec and if you want more precise formatting: Code: Copy Log($"${p.Days}:$2.0{p.Hours}:$2.0{p.Minutes}:$2.0{p.Seconds}"$) '0:00:00:05 Log($"$2.0{p.Hours}:$2.0{p.Minutes}:$2.0{p.Seconds}"$) '00:00:05 Last edited: Nov 4, 2017 Upvote 0
Derek Johnson said: I expected this to display: Elapsed time: 00:00:05 Click to expand... Your code should be like this: Code: Copy t1=DateTime.now Sleep(5000) t2=DateTime.now Dim p As Period p=DateUtils.PeriodBetweenInDays(t1,t2) Log($"${p.Days} days ${p.Hours} hours ${p.Minutes} min ${p.Seconds} sec"$) '0 days 0 hours 0 min 5 sec and if you want more precise formatting: Code: Copy Log($"${p.Days}:$2.0{p.Hours}:$2.0{p.Minutes}:$2.0{p.Seconds}"$) '0:00:00:05 Log($"$2.0{p.Hours}:$2.0{p.Minutes}:$2.0{p.Seconds}"$) '00:00:05
Erel B4X founder Staff member Licensed User Longtime User Nov 5, 2017 #3 Derek Johnson said: DateTime.TimeZoneOffset =0 Click to expand... Whenever you need to set the time zone to 0 you can be sure that you are doing something wrong. Upvote 0
Derek Johnson said: DateTime.TimeZoneOffset =0 Click to expand... Whenever you need to set the time zone to 0 you can be sure that you are doing something wrong.
Derek Johnson Active Member Licensed User Longtime User Nov 5, 2017 #4 Erel said: Whenever you need to set the time zone to 0 you can be sure that you are doing something wrong. Click to expand... Yes that's probably true, but I wasn't setting the Time Zone, my Nexus 5X was reporting that the DateTime.TimeZoneOffset was 0. What seems wrong is that Code: Copy DateTime.DateFormat="EEE dd MMM YYYY HH:mm:ss" Log("Time Zero " & DateTime.Date(0)) on my Nexus 5X produces Time Zero Thu 01 Jan 1970 01:00:00 whereas my understanding is that Unix Time starts at 01 Jan 1970 00:00:00 CST I tried this on another device and it produced Time Zero Thu 01 Jan 1970 00:00:00 so this may be a problem specific to my Nexus 5X. Could anyone else try this on a Nexus 5X please? Derek Last edited: Nov 5, 2017 Upvote 0
Erel said: Whenever you need to set the time zone to 0 you can be sure that you are doing something wrong. Click to expand... Yes that's probably true, but I wasn't setting the Time Zone, my Nexus 5X was reporting that the DateTime.TimeZoneOffset was 0. What seems wrong is that Code: Copy DateTime.DateFormat="EEE dd MMM YYYY HH:mm:ss" Log("Time Zero " & DateTime.Date(0)) on my Nexus 5X produces Time Zero Thu 01 Jan 1970 01:00:00 whereas my understanding is that Unix Time starts at 01 Jan 1970 00:00:00 CST I tried this on another device and it produced Time Zero Thu 01 Jan 1970 00:00:00 so this may be a problem specific to my Nexus 5X. Could anyone else try this on a Nexus 5X please? Derek
Erel B4X founder Staff member Licensed User Longtime User Nov 5, 2017 #5 Ticks = Number of milliseconds since 1/1/1970 00:00 UTC The short and correct answer is: never use DateTime.Date or DateTime.Time with a value that represents anything other than ticks. Milliseconds <> Ticks. Code to convert milliseconds to string: https://www.b4x.com/android/forum/threads/summing-times.85302/#post-540422 Long answer: Source: https://en.wikipedia.org/wiki/British_Summer_Time "An inquiry during the winter of 1959–60, in which 180 national organisations were consulted, revealed a slight preference for a change to all-year GMT+1, but instead the length of summer time was extended as a trial.[9] A further inquiry during 1966–67 led the government of Harold Wilson to introduce the British Standard Time experiment, with Britain remaining on GMT+1 throughout the year. This took place between 27 October 1968 and 31 October 1971, when there was a reversion to the previous arrangement." This means that the time in UK at 1/1/1970 00:00 UTC was 01:00. Upvote 0
Ticks = Number of milliseconds since 1/1/1970 00:00 UTC The short and correct answer is: never use DateTime.Date or DateTime.Time with a value that represents anything other than ticks. Milliseconds <> Ticks. Code to convert milliseconds to string: https://www.b4x.com/android/forum/threads/summing-times.85302/#post-540422 Long answer: Source: https://en.wikipedia.org/wiki/British_Summer_Time "An inquiry during the winter of 1959–60, in which 180 national organisations were consulted, revealed a slight preference for a change to all-year GMT+1, but instead the length of summer time was extended as a trial.[9] A further inquiry during 1966–67 led the government of Harold Wilson to introduce the British Standard Time experiment, with Britain remaining on GMT+1 throughout the year. This took place between 27 October 1968 and 31 October 1971, when there was a reversion to the previous arrangement." This means that the time in UK at 1/1/1970 00:00 UTC was 01:00.
Derek Johnson Active Member Licensed User Longtime User Nov 5, 2017 #6 Erel, Thanks for taking the time to reply to that. That definitely explains it! I'm constantly surprised by the complexity of time related issues. Weird! Derek Upvote 0
Erel, Thanks for taking the time to reply to that. That definitely explains it! I'm constantly surprised by the complexity of time related issues. Weird! Derek