Problem in converting local unix time to UTC unix time
NickName:Raghav Ask DateTime:2019-07-25T00:40:53

Problem in converting local unix time to UTC unix time

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

Answers
Lev Rubel 2019-07-24T17:15:31

I guess you're incorrectly transform datetimes with TZ information when converting the timestamp to datetime instance using time.strftime and after that datetime.strptime or when using mktime.\n\nAnyway, there is much easier way to achieve what you want:\n\nfrom datetime import datetime, timezone\n\n# Get current timestamp\n\nnow_timestamp = time.time()\n>> 1563987839.054703\n\n# Get datetime of that timestamp but already in UTC.\n# This preserves TZ information and allows \n# to correctly do transition to the timestamp again.\n\nutc_time = datetime.utcfromtimestamp(now_timestamp)\n>> datetime.datetime(2019, 7, 24, 17, 3, 59, 54703)\n\n# Convert UTC datetime back to timestamp\n\nutc_timestamp = utc_time.timestamp()\n>> 1563977039.054703\n\n# Verify that this timestamp is indeed UTC\n# I am in UTC+3 timezone now\n\ndatetime.now()\n>> datetime.datetime(2019, 7, 24, 20, 4, 10, 500229)\n\ndatetime.fromtimestamp(utc_timestamp)\n>> datetime.datetime(2019, 7, 24, 17, 3, 59, 54703)\n\n",


More about “Problem in converting local unix time to UTC unix time” related questions

Problem in converting local unix time to UTC unix time

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

Show Detail

Converting a UTC time string to Unix time

In my database, I'm storing time as a BIGINT UTC value. In my test data SQL dump I'd like to see the time in UTC for readability. However, the MySQL unix_timestamp function expects time in the lo...

Show Detail

converting unix timestamp to local time

I'm attempting to convert time.time() to the local time. Reading python datetime to unix timestamp, I understand there's some limitations given that the timestamp is in UTC. However, in lieu of just

Show Detail

GMT/UTC (not local) time in Unix format

I'm using JavaScript and trying to get GMT+0/UTC+0 time (otherwhise called Zulu time) in Unix format. I've managed to get my local time in Unix format like this: var datetime = new Date() var uni...

Show Detail

Moment.js trouble converting unix to local time to utc

Hi I am having trouble converting a timestamp from unix to local to ultimately utc. let convertToUTC = (unixTimestamp)=> { let convertedUnixToLocal = moment.unix(unixTimestamp).format("MM/DD/

Show Detail

Converting UTC time to Unix without changing default timezone

I'm having a problem where I'm dealing with another server in UTC time. Our procedure here is to always convert timestamps to Unix before sending times to our company's app to use. The problem in t...

Show Detail

Converting Moment utc time into unix time

Hello guys I was wondering how do I convert a date into Unix time stamp using the library moment.js so I can compare the oldDate with another date. This is what I tried: var oldDate = (moment...

Show Detail

change Unix(Epoch) time to local time in pyspark

I have a dataframe in Spark which contains Unix(Epoch) time and also timezone name. I hope to convert the epochtime to local time according to different tz name. Here is how my data looks like: ...

Show Detail

android convert unix time to utc date

I am a little confused with the unix time stamp conversion to java.util.Date involving the time zones. I have a unix time stamp that is "1367832568". it is a UTC date (Mon May 06 17:29:28 GMT+00:0...

Show Detail

Best way to get unix timestamp for current UTC time in MySQL

I've been trying to use the following in mySQL to create a unix stamp for current UTC time UNIX_TIMESTAMP(UTC_TIMESTAMP()) When I execute the query and get the result, it seems like mySQL is doin...

Show Detail