Solved Get datetime in my timezone
-
I have an app that will update its last seen status to firebase realtime database.
The problem is that the app will get system datetime and update it to the DB and that will vary depending on the country from where the app is being run, so that info will be practically useless as I will not know the country where app is being used.I need to always get the datetime in a specific timezone, say the USA.
How can I do it? -
The use QDateTime::toTimeZone() - it's even mentioned in toTimeSpec()
-
Hi,
Use UTC as reference and then translate the value in the local timezone when needed.
-
@SGaist Could you give a code example, I saw a comment with someone mentioning but couldn't find a working example of this.
-
QDateTime::toTimeSpec has an example.
-
@SGaist But still can't see where I would set the time zone I want in order to do the conversion. For example if someone who is using my app is in China, I need to convert his local time to my local time because China is several hours ahead of my local time. China is 7 hours, if I am not mistaken, ahead, so if a user is using my app there and it is 20:21 there I need the app to update to database the time 11:21.
For the time being I am using www.timeapi.io api to get time I need. -
The use QDateTime::toTimeZone() - it's even mentioned in toTimeSpec()
-
@Christian-Ehrlicher Thanks, exactly what I needed, my eyes went only for the code, didn't even see
QDateTime::toTimeZone()
.Is there any chance, if you know, that this might not work correctly or it being unreliable when compared to the timeapi.io api?
-
What kind of unreliability are you expecting ?
-
@SGaist like for example if the user has its system time wrong, wouldn't that convert to a wrong time also.
-
@hbatalha said in Get datetime in my timezone:
like for example if the user has its system time wrong, wouldn't that convert to a wrong time also.
Nice joke... how is any program supposed to do a correct conversion then?
-
If you are worried about that kind of issue you should do the work server side. Provided that your server time is accurate.
-
@Christian-Ehrlicher hahaha I don't know, I always find myself going to the worst case scenarios.
And also, from time to time my Windows 10 system time changes by itself(always one hour ahead) whether it is set automatically or not and that is an issue many users complain about. So that one thing I am worried about.
-
@SGaist I am thinking about using an api - http://www.timeapi.io/