Android Question changing date timezone with DateUtils

ALBRECHT

Active Member
Licensed User
hello,

to check the DateUtils.SetDateAndTime2 timestamp,

I had trying that code :
B4X:
'for the datetime = 08/05/2019 14:43:00        
Dim mats As Long = DateUtils.SetDateAndTime2(2019, 8, 5, 14, 43, 0, 1) 'with UTC/GMT + 1
Log(mats) 'return : 1565012580000
Dim MyDate As Long = DateUtils.UnixTimeToTicks(mats)
Log(DateTime.Date(MyDate) & " " & DateTime.time(MyDate))
'return : 11/4/51563 12:40:00
so, wrong result.

but if i delete the last 3 digit of the timestamp long number:
B4X:
'for the datetime = 08/05/2019 14:43:00        
Dim mats As Long = DateUtils.SetDateAndTime2(2019, 8, 5, 14, 43, 0, 1)
Log(mats) 'return : 1565012580000
mats = mats / 1000 ' for : 1565012580 instead 1565012580000
Dim MyDate As Long = DateUtils.UnixTimeToTicks(mats)
Log(DateTime.Date(MyDate) & " " & DateTime.time(MyDate))
'return : 08/05/2019 13:43:00
the result is ok

please where im wrong ?

Regards, Michel
 

ALBRECHT

Active Member
Licensed User
but I dont see how to get the good date in other Timezone , into the TUTO with using DateUtils.SetDateAndTime2

so, to record in my server (Paris) the good API date, for an event generate by an user anywhere in the world,
i have change by :

B4X:
Dim DT As Long
    DT = DateTime.now
    Log(DT) ' **** the User datetime *****
    DateTime.DateFormat = "dd/MM/yyyy"
    DateTime.TimeFormat = "HH:mm:ss"
    Log(DateTime.Date(DT) & "-" & DateTime.Time(DT))
   
Dim my As Int = DateTime.GetYear(DT)
    Dim mm As Int = DateTime.GetMonth(DT)
    Dim md As Int = DateTime.GetDayOfMonth(DT)
    Dim mh As Int = DateTime.GetHour(DT)
    Dim mn As Int = DateTime.GetMinute(DT)
    Dim ms As Int = DateTime.GetSecond(DT)
    Dim mgmt As Int = 1
   
Dim mats As Long = DateUtils.SetDateAndTime2(my, mm, md, mh, mn, ms, mgmt)
    Log(mats) ' **** my server in Paris = UTC+1*****
    Log(DateTime.Date(mats ) & "-" & DateTime.time(mats ))

Am I on the right track and is this useful ?

because it is always more correct to take the conversion of the date / time of the user (has tel-aviv at 10h45 am for example) in hour gmt of Paris (+1) than to take the date / time of my server ...

Michel
 
Upvote 0

emexes

Expert
Licensed User
but I dont see how to get the good date in other Timezone
Am I on the right track and is this useful ?
Good question! I don't know. TBH I'm not even sure where we're headed, let alone if we're heading in the right direction :)

What was it we're trying to do, again?

In the meantime, maybe this is something: might be able to get the timezone of the current device by using Z or similar in the time format:
B4X:
z Time zone   General time zone    Pacific Standard Time; PST; GMT-08:00
Z Time zone   RFC 822 time zone    -0800
X Time zone   ISO 8601 time zone   -08; -0800; -08:00
https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
 
Upvote 0

ALBRECHT

Active Member
Licensed User
Emexes, y m trying to :
1 get the date time of the user when he click "ok" (for example) in tel-aviv : gmt +3
2 send it to my server
3 but record it in my gmt format (Paris : gmt +1)

So how to convert the user datetime in my datetime (Paris),
i simply thought it was necessary to use DateUtils.SetDateAndTime2 to convert correctly?
 
Upvote 0

emexes

Expert
Licensed User
1 get the date time of the user when he click "ok" (for example) in tel-aviv : gmt +3
3 but record it in my gmt format (Paris : gmt +1)
(GMT +1) minus (GMT +3) = -2 hours
thus subtract 2 hours from the time sent by the (Tel Aviv) user.

But I suspect that DateTime.Now is UTC, and so if you just transfer and save that (Long) number, then it will be automatically adjusted to your server's timezone upon conversion to readable text format.
 
Last edited:
Upvote 0

emexes

Expert
Licensed User
Righto. This code:
B4X:
DateTime.DateFormat = "yyyy-MM-dd"
DateTime.TimeFormat = "HH:MM:ss Z"
  
Dim T As Long = DateTime.Now
Log(T)
Log(DateTime.Date(T))
Log(DateTime.Time(T))
  
Dim T As Long = 0
Log(T)
Log(DateTime.Date(T))
Log(DateTime.Time(T))
gives this:
B4X:
1565080535551
2019-08-06
18:08:35 +1000

0
1970-01-01
10:01:00 +1000
which suggests that the ticks time is UTC time, and that DateTime displays that time but adjusted for the device's timezone.

I have two minor quirks going on, though:

Firstly, date/time 0 is out by 1 minute (10:01:00 rather than 10:00:00) but I suspect that might be due to leap seconds. Leap seconds are now 37 since 1970, so rounded off to nearest minute, that'd match the 10:01 time offset.

Secondly, the B4A shows 18:38 not 18:08 ie off by half an hour. The phone itself shows the correct time, ie half an hour ahead. Changed to another phone, works fine. Odd. I assume that if I put a SIM in, then it will right itself.
 
Last edited:
Upvote 0

ALBRECHT

Active Member
Licensed User
Emexes,

yes, but in addition to that, i think the subtract method of GMT hrs differences do not take into account the difference with the summer and winter hours of each country,

so the simple -2hrs between Tel-Aviv and Marseille would be not correct at any time of the year?

So, in my server, it will be simpler to have two DateTime field :
- Server DateTime
- User Device DateTime
 
Upvote 0

emexes

Expert
Licensed User
If your aim is:
- an event happens on an Android device in place A (say Tel Aviv)
- the device sends a record of that event to your server in place B (say Paris)
- when you look at the event record, you wish to know what time it was at place B when the event happened in place A

then the solution is to have the Android device in place A send the DateTime.Now Long value to be recorded on the server at place B (or wherever). When you look at the event record, the Android time system will offset that during conversion to readable form so that it shows what time it was at place B when the event happened in place A.
 
Upvote 0

ALBRECHT

Active Member
Licensed User
Ok, i will check that. Thanks for all emexes
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
thus subtract 2 hours from the time sent by the (Tel Aviv) user.
Date and time arithmetics do not work.

If you haven't watched the date and time video tutorial then start with it.

1 get the date time of the user when he click "ok" (for example) in tel-aviv : gmt +3
2 send it to my server
3 but record it in my gmt format (Paris : gmt +1)
1. Use DateUtils.SetDateAndTime2 for this.
3. Save the ticks values. Ticks = number of milliseconds since 1/1/1970 00:00 UTC. Your server time zone is not relevant.
 
Upvote 0

ALBRECHT

Active Member
Licensed User
Thats what i tried to have (see my post above 9:30am ) with setdatetime2 ...

Probably du to the summer daylight lag of the 2 countries and other things,
I still can not determine exactly what time it was in Marseille France, when my sister clicked at 12:43 in Tel Aviv!
 
Upvote 0

ALBRECHT

Active Member
Licensed User
So, anyway I think I will opt to implement my own sub upon arrival on the server, using the recept of : Ticks + Timezone + Daylite of the sender position
 
Upvote 0

emexes

Expert
Licensed User
I still can not determine exactly what time it was in Marseille France, when my sister clicked at 12:43 in Tel Aviv!
I think you're already on to it, but just in case... this site is great for double-checking time and date calculations:

https://www.timeanddate.com/worldclock/meetingtime.html?&p1=195&p2=676&p3=152

eg on 24th March 2019 (randomly-chosen example date)

https://www.timeanddate.com/worldcl...=24&hour=10&min=43&sec=0&p1=195&p2=676&p3=152

12:43pm in Tel Aviv was 11:43am in Paris.
 
Upvote 0

emexes

Expert
Licensed User
So, anyway I think I will opt to implement my own sub upon arrival on the server, using the receipt of : Ticks + Timezone + Daylite of the sender position
JUST USE THE TICKS VALUE SENT BY THE SENDER aka their DateTime.Now

This indicates a specific global moment in time, irrespective of location and any local daylight savings / summer time adjustments.

You can divide it by 1000 if you are more comfortable with it being in seconds. My understanding was that this was equivalent to Unix time, or at least very close to it.

If you do reduce it down, remember to keep storing it as a 64-bit integer (B4A Long) - that 32-bit year-2038 issue is getting closer and closer...
 
Last edited:
Upvote 0

ALBRECHT

Active Member
Licensed User
thank you for everything, this forum is hyper responsive ... i love it
 
Upvote 0

emexes

Expert
Licensed User
Upvote 0

ALBRECHT

Active Member
Licensed User
hello, yes you have my complete attention. :rolleyes:

I would like your opinion on this:

With regard to the IANA database (https://www.iana.org/time- zones) which provides complex data
(which often evolves according to the political atmosphere of countries, which will soon be the case for some countries of Europe):
time zone limits, UTC offsets, summer time shift rules and other country adjustments.

Do you consider this in your displays and customer information, and if so how do you handle this in your date / time conversion?

This must be of great importance for financial apps with time difference calculation!

Michel
 
Upvote 0
Top