I am trying to convert unix time on my local system to UTC time in unix format, but after converting it the final time is going off by +1 hour.
To do so, I wrote the following code.
from dateutil import tz
import time
from time
import mktime
now_time = time.time()
print('current local unix time', now_time)
print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(now_time)))
to_zone = tz.tzutc()
from_zone = tz.tzlocal()
t = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(now_time))
utc = datetime.strptime(str(t), '%Y-%m-%d %H:%M:%S')
utc = utc.replace(tzinfo=from_zone)
central = utc.astimezone(to_zone)
print('Converted to UTC ',central)
unix_secs = mktime(central.timetuple())
print('Central unix time ',unix_secs)
print('central unix time to dattime ', time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(unix_secs)))
The output is as follows
current local unix time 1563985835.3707478
2019-07-24 12:30:35
Converted to UTC 2019-07-24 16:30:35+00:00
Central unix time 1564003835.0
central unix time to dattime 2019-07-24 17:30:35
can someone please tell me what am I doing wrong here and how can I fix this?
Copyright Notice:Content Author:「Raghav」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/57187679/problem-in-converting-local-unix-time-to-utc-unix-time