I'm collecting some data from sensors and I get the timestamp from it like this:
"time": {
"seconds": 40,
"year": 115,
"month": 5,
"hours": 7,
"time": 1434549820776,
"date": 17,
"minutes": 3,
"day": 3,
"timezoneOffset": 420
},
I have a python script that processes the data coming from the sensors (incoming data is json format), I take the value of time
and converts into readable time format.
I used datetime.fromtimestamp(1434549820776/1000).strftime('%Y-%m-%d %H:%M:%S')
and that returned '2015-06-17 15:03:40'
Where as the datetime.utcfromtimestamp(1434549820776/1000).strftime('%Y-%m-%d %H:%M:%S')
Returned: '2015-06-17 14:03:40'
As you can there is an hour Difference, so my question is which one is better to use?
Looking at your json, you can see that the time stamp corresponds to 2015-06-17 07:03:40 locally.
The timezoneOffset tells you that there are 7 hours difference between local time and UTC time => the UTC time corresponding to your json is 2015-06-17 14:03:40.
Since this is what you get when using
datetime.utcfromtimestamp(1434549820776/1000).strftime('%Y-%m-%d %H:%M:%S')
(=> '2015-06-17 14:03:40'), this means that your time stamp is written in UTC time and you should therefore useutcfromtimestamp
if you want to be exact.