Android Question Covert server time & date to local time & date

aaronk

Well-Known Member
Licensed User
Hi,

I am connecting to a server and it's storing events on it. It's storing the servers date and time to each event.

When I connect to the server I request event 1 and it returns the time & date as well as the event, but the time and date is the servers time & date.

Rather than displaying the servers date & time that event occurred in the app I want to display the users local time & date.

For Example:
The server's current date and time is:

1:30AM Friday, 5 May 2017 (GMT-4)

The server stores an event like:
DDMMYYYYHHMMEVENT
eg. 050520170130Bob logged in

In the example above, it stored the event on 5th of May 2017 at 1:30AM (since this is the servers date and time), and the event is 'Bob logged in'.

Let's say 3 users logged in to get event 1 from the server..

User A current time is: 6:30 am Friday, 5 May 2017 (GMT+1)
User B current time is: 1:30 PM Friday, 5 May 2017 (GMT+8)
User C current time is: 10:30 PM Thursday, 4 May 2017 (GMT-7)

Rather than showing that the event happened at 1:30AM on the 5th of May (the servers time & date it stored the event) I want it to show it happened at:

User A sees the event happened at 6:30 AM Friday, 5 May 2017 (GMT+1)
User B sees the event happened at 1:30 PM Friday, 5 May 2017 (GMT+8)
User C sees the event happened at 10:30 PM Thursday, 4 May 2017 (GMT-7)

Which is the users local time & date.

Any ideas on how to do this ?
(also need it to work during DLS time)
 

udg

Expert
Licensed User
One way could be to record on server UTC time (set timezone=0); once you read it back you can convert it to local time thanks to local TZ settings.

udg
 
Upvote 0

aaronk

Well-Known Member
Licensed User
Is this a B4J server?
Yes it is a B4J server.

Would the following be the correct way in doing this..

On my server set the timezone to:

B4X:
DateTime.SetTimeZone(0)

Now my B4J server will be UTC +0 and all events are stored using that timezone. (or don't I need to set the timezone ?)

Then store the ticks on the server (rather than DDMMYYYYHHM like what I was going to use):

B4X:
DateTime.SetTimeZone(0)
Dim serverTicks As String = DateTime.Now
' store serverTicks as the event time/date on the server

Then from my B4A (and B4i) app convert the event ticks like:

B4X:
' DateTime.SetTimeZone is not required since it gets the devices timezone
Dim ServerTicks As String = "1494135595957"  ' ServerTicks value is set/sent from the B4J server
Dim AppTimeDate As String
AppTimeDate = DateTime.date(ServerTicks) & " " & DateTime.time(ServerTicks)
' AppTimeDate will now show the users local date and time from the Ticks that the server sent

Would the above be the correct or is there a better way in doing this ?
 
Upvote 0
Top