DateTime.Time

derez

Expert
Licensed User
Longtime User
DateTime.time(something) returns the time of the value of "something" + the time offset which is stored in the device settings !
So if I want to get the time to go from here to there at a certain speed, the datetime.time(datetime.ticksperhour*distance/speed) gives two hours extra (for a two hours offset).
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
DateTime.Time takes the ticks value and converts it to a string representation. The ticks value is the number of milliseconds since January 01, 1970 (GMT).
So it is correct to add the current time zone.

You can use the following code to get rid of the time zone effect:
B4X:
Sub Globals
    Dim timezone As Long
    timezone = -DateTime.DateParse("01/01/1970")
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Msgbox(DateTime.Time(5 * DateTime.TicksPerHour - timezone), "") 'Will show 05:00
End Sub
 

derez

Expert
Licensed User
Longtime User
Erel

The effect of additional timezone is only in the device, while in the emulator it seems not to care what is the timeshift and provides the correct answer without any subtractions.

In the device the addition of the time shift does not make sense because the value of the ticks should be exactly what I input by ticksperhour or any other parameter. by subtracting the start value of the ticks (=0) you should not change anything because you are subtracting zero, but actually you subtract zero+time shift added by the OS, so you get the required result.

I think it worth a note in the documentation because it is not something you would expect, especially the difference between the emulator and the device (or is it only in my A81E device?)
 
Last edited:

agraham

Expert
Licensed User
Longtime User
while in the emulator it seems not to care what is the timeshift
If you untick Automatic in Settings -> Date & time and set a timezone it does it in the emulator as well. I can't tell if it also does it with Automatic selected as we are currently on GMT so it will make no difference!
 

derez

Expert
Licensed User
Longtime User
I use manual settings of the time shift in both, and they are not providing the same result.

EDit: checked again, and after changing the time settings few times it is the same.
I think that the time settings does not catch immediately and that what probably happened in the first try.
 
Last edited:
Top