Android Question DateTime.SetTimeZone

LucaMs

Expert
Licensed User
Longtime User
DateTime.SetTimeZone should affect the conversions of dates to ticks and viceversa:
upload_2017-4-18_13-17-13.png


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:

LucaMs

Expert
Licensed User
Longtime User
Is this a B4J server?
SURE, what else? ;)

Just send the ticks value and compare it to the device current ticks value (DateTime.Value)
I can compare ticks, of course, but where should I take them if not using DateTime.Now? And if the server is in China and the client in RomA...!
For this reason I tried to develop that function, to get the number of (absolute - GMT since...) ticks.

Pseudo code:

Server:
SendToClient(AbsDateTicks(DateTime.Now))


Client:
ServerDateTicks = ReceiveFromServer(DateTicks As Long)
ClientDateTicks = AbsDateTicks(DateTime.Now)

Diff = ClientDateTicks - ServerDateTicks
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Very nice!

1) The server sends its current time (ticks - DateTime.Now) to all clients;
2) the client stores its current time (tick - DateTime.Now) WHEN IT RECEIVES the server time;
3) the client calculates the latency = ClientNow - ServerNow.

Well, latency results... a negative number (one test: -1,078)

I think that this is possible because DateTime.Now simply takes the system time and server and client system times may not coincide perfectly.


Maybe one day I will understand how many people handle these things :D:(
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
The measurements will only be correct if the client and the server clocks are synchronized.
Yes, then... never, unless both devices are at your disposal.

I could make a sort of ping; the client sends a request and receives an answer, calculates the time (using only its own clock), divides it for two and get something similar to the latency (it could do it 10 times and divide by 10. Moreover, the client should do this job in a different thread, not in the main).

This, however, does not ensure that when you need to use this average latency you will have just that delay.

In short, users of my servers will still have problems and then... they will send me to hell, this is the logical consequence :D:(


Thank you.
 
Upvote 0
Top