How to get UTC epoch value from a local timestamp - python

I need to convert my timestamp into an epoch value. I found that my system timezone is set to CET, which is also used by my my-sql database.
I tried this:
os.environ['TZ']='UTC'
epoch = int(time.mktime(time.strptime('2017-02-22 17:04:06', '%Y-%m-%d %H:%M:%S')))
print(epoch)
#Output: 1487779446 -> not the same compared to 1487779446000 (= '2017-02-22 17:04:06')
print(time.strftime('%m/%d/%Y %H:%M:%S', time.gmtime(epoch/1000.)))
#Output: 01/18/1970 05:16:19 -> Not Correct!
# Timestamp: '2017-02-22 17:04:06'
print(time.strftime('%m/%d/%Y %H:%M:%S', time.gmtime(1487779446000/1000.)))
#Output: 02/22/2017 16:04:06 -> Correct!
I tried this with CET too, same result. I don't understand why I am getting different values.

A good way to get seconds since epoch is to do it explicitly. This function converts the timestring to a naive datetime, makes the datetime timezone aware, and then subtracts a datetime which is epoch at UTC.
Code:
import datetime as dt
from pytz import timezone
def convert_timestamp_to_utc_epoch(ts, tz_info):
# convert timestamp string to naive datetime
naive = dt.datetime.strptime(ts, '%Y-%m-%d %H:%M:%S')
# assign proper timezone to datetime
aware = tz_info.localize(naive)
# get a datetime that is equal to epoch in UTC
utc_at_epoch = timezone('UTC').localize(dt.datetime(1970,1,1))
# return the number of seconds since epoch
return (aware - utc_at_epoch).total_seconds()
Test Code:
Using the example from question:
print('CET:',
convert_timestamp_to_utc_epoch('2017-02-22 17:04:06', timezone('CET')))
Results:
CET: 1487779446.0
Get an arbitrary local timezone:
If you are not sure what timezone the local machine is using, the tzlocal library can be used like:
from tzlocal import get_localzone
local_tz = get_localzone()
print('Local:',
convert_timestamp_to_utc_epoch('2017-02-22 17:04:06', local_tz))
print('Pacific:',
convert_timestamp_to_utc_epoch('2017-02-22 17:04:06', timezone('US/Pacific')))
Results:
Local: 1487811846.0
Pacific: 1487811846.0

Related

Convert python aware datetime to local timetuple

I have an aware datetime object:
dt = datetime.datetime.now(pytz.timezone('Asia/Ho_Chi_Minh'))
I'm using this to convert the object to timestamp and it currently runs fine:
int(time.mktime(dt.utctimetuple()))
But according to time docs, time.mktime requires local timetuple, not UTC timetuple. How do I get local timetuple from an aware datetime? Or is any other way to make timestamp instead of time.mktime?
I have read this question and it seems that I should use calendar.timegm(dt.utctimetuple()).
Converting datetime to unix timestamp
If you have an aware datetime you can convert it to a unix epoch timestamp by subtracting a datetime at UTC epoch like:
Code:
import datetime as dt
import pytz
def aware_to_epoch(aware):
# get a datetime that is equal to epoch in UTC
utc_at_epoch = pytz.timezone('UTC').localize(dt.datetime(1970, 1, 1))
# return the number of seconds since epoch
return (aware - utc_at_epoch).total_seconds()
aware = dt.datetime.now(pytz.timezone('Asia/Ho_Chi_Minh'))
print(aware_to_epoch(aware))
Results:
1521612302.341014
It seems that your are confused on what do you want.
Based on your comment, the answer is in python datetime documentation:
There is no method to obtain the POSIX timestamp directly from a naive datetime instance representing UTC time. If your application uses this convention and your system timezone is not set to UTC, you can obtain the POSIX timestamp by supplying tzinfo=timezone.utc:
timestamp = dt.replace(tzinfo=timezone.utc).timestamp()
or by calculating the timestamp directly:
timestamp = (dt - datetime(1970, 1, 1)) / timedelta(seconds=1)

Epoch Time and Time Zones

Here's my problem, I have 2 times I'm feeding into Python, one in EST and other in GMT. I need to convert both to epoch and compare them. From what it's looking like when I convert the EST to epoch, it's should convert to the exact equivalent of GMT I thought. It doesn't look like it is:
from datetime import datetime as dt,datetime,timedelta
import time
# EST
date_time = '09.03.1999' + " " + "08:44:17"
pattern = '%d.%m.%Y %H:%M:%S'
epoch = int(time.mktime(time.strptime(date_time, pattern)))
print epoch
# GMT
date_time2 = '09.03.1999' + " " + "13:44:17.000000"
pattern2 = '%d.%m.%Y %H:%M:%S.%f'
epoch2 = int(time.mktime(time.strptime(date_time2, pattern2)))
print epoch2
So, I think you're confusing what epoch means here.
Epoch is a representation of time which counts the numbers of seconds from 1970/01/01 00:00:00 to a given a date.
Epoch conversion doesn't care about timezones and you can actually have negative epoch times with timezone conversion (play around at http://www.epochconverter.com/).
A real example: I live in Japan, so local time epoch 0 for me is actually -32400 in GMT epoch.
What you need to do is do something like in this question to first convert between timezones and then do the date to epoch conversion.
Here's some code from the accepted answer:
from datetime import datetime
from dateutil import tz
from_zone = tz.gettz('UTC')
to_zone = tz.gettz('America/New_York')
utc = datetime.strptime('2011-01-21 02:37:21', '%Y-%m-%d %H:%M:%S')
# Tell the datetime object that it's in UTC time zone since
# datetime objects are 'naive' by default
utc = utc.replace(tzinfo=from_zone)
# Convert time zone
central = utc.astimezone(to_zone)

Python: Converting string to timestamp with microseconds

I would like to convert string date format to timestamp with microseconds
I try the following but not giving expected result:
"""input string date -> 2014-08-01 04:41:52,117
expected result -> 1410748201.117"""
import time
import datetime
myDate = "2014-08-01 04:41:52,117"
timestamp = time.mktime(datetime.datetime.strptime(myDate, "%Y-%m-%d %H:%M:%S,%f").timetuple())
print timestamp
> 1410748201.0
Where did the milliseconds go?
There is no slot for the microseconds component in a time tuple:
>>> import time
>>> import datetime
>>> myDate = "2014-08-01 04:41:52,117"
>>> datetime.datetime.strptime(myDate, "%Y-%m-%d %H:%M:%S,%f").timetuple()
time.struct_time(tm_year=2014, tm_mon=8, tm_mday=1, tm_hour=4, tm_min=41, tm_sec=52, tm_wday=4, tm_yday=213, tm_isdst=-1)
You'll have to add those manually:
>>> dt = datetime.datetime.strptime(myDate, "%Y-%m-%d %H:%M:%S,%f")
>>> time.mktime(dt.timetuple()) + (dt.microsecond / 1000000.0)
1406864512.117
The other method you could follow is to produce a timedelta() object relative to the epoch, then get the timestamp with the timedelta.total_seconds() method:
epoch = datetime.datetime.fromtimestamp(0)
(dt - epoch).total_seconds()
The use of a local time epoch is quite deliberate since you have a naive (not timezone-aware) datetime value. This method can be inaccurate based on the history of your local timezone however, see J.F. Sebastian's comment. You'd have to convert the naive datetime value to a timezone-aware datetime value first using your local timezone before subtracting a timezone-aware epoch.
As such, it is easier to stick to the timetuple() + microseconds approach.
Demo:
>>> dt = datetime.datetime.strptime(myDate, "%Y-%m-%d %H:%M:%S,%f")
>>> epoch = datetime.datetime.fromtimestamp(0)
>>> (dt - epoch).total_seconds()
1406864512.117
In Python 3.4 and later you can use
timestamp = datetime.datetime.strptime(myDate, "%Y-%m-%d %H:%M:%S,%f").timestamp()
This doesn't require importing the time module. It also uses less steps so it should be faster. For older versions of python the other provided answers are probably your best option.
However, the resulting timestamp will interpret myDate in local time, rather than UTC, which may cause issues if myDate was given in UTC
Where did the milliseconds go?
It is the easy part. .timetuple() call drops them. You could add them back using .microsecond attribute. The datetime.timestamp() method from the standard library works that way for naive datetime objects:
def timestamp(self):
"Return POSIX timestamp as float"
if self._tzinfo is None:
return _time.mktime((self.year, self.month, self.day,
self.hour, self.minute, self.second,
-1, -1, -1)) + self.microsecond / 1e6
else:
return (self - _EPOCH).total_seconds()
It is enough if possible ~1 hour errors could be ignored in your case. I assume that you want microseconds and therefore you can't ignore ~1 hour time errors silently.
To convert the local time given as a string to the POSIX timestamp correctly is a complex task in general. You could convert the local time to UTC and then get the timestamp from UTC time.
There are two main issues:
local time may be non-existent or ambiguous e.g. during DST transitions the same time may occur twice
UTC offset for the local timezone may be different in the past and therefore a naive: local time minus epoch in local time formula may fail
Both can be solved using the tz database (pytz module in Python):
from datetime import datetime
import pytz # $ pip install pytz
from tzlocal import get_localzone # $ pip install tzlocal
tz = get_localzone() # get pytz timezone corresponding to the local timezone
naive_d = datetime.strptime(myDate, "%Y-%m-%d %H:%M:%S,%f")
# a) raise exception for non-existent or ambiguous times
d = tz.localize(naive_d, is_dst=None)
## b) assume standard time, adjust non-existent times
#d = tz.normalize(tz.localize(naive_d, is_dst=False))
## c) assume DST is in effect, adjust non-existent times
#d = tz.normalize(tz.localize(naive_d, is_dst=True))
timestamp = d - datetime(1970, 1, 1, tzinfo=pytz.utc)
The result is timestamp -- a timedelta object, you can convert it to seconds, milliseconds, etc.
Also different systems may behave differently around/during leap seconds. Most application can ignore that they exist.
In general, it might be simpler to store POSIX timestamps in addition to the local time instead of trying to guess it from the local time.

Convert datetime object in a particular timezone to epoch seconds in that timezone

eg:
>>> print dt
2012-12-04 19:00:00-05:00
As you can see, I have this datetime object
How can I convert this datetime object to epoch seconds in GMT -5.
How do I do this?
Your datetime is not a naive datetime, it knows about the timezone it's in (your print states that is -5). So you just need to set it as utc before you convert it to epoch
>>> import time, pytz
>>> utc = pytz.timezone('UTC')
>>> utc_dt = utc.normalize(dt.astimezone(utc))
>>> time.mktime(utc_dt.timetuple())
1355270789.0 # This is just to show the format it outputs
If the dt object was a naive datetime object, you'd need to work with time zones to comply to daylight saving time while finding the correct hours between GMT 0. For example, Romania in the winter, it has +2 and in the summer +3.
For your -5 example, New-York will do:
>>> import time,pytz
>>> timezone = pytz.timezone('America/New_York')
>>> local_dt = timezone.localize(dt)
Now you have a non-naive datetime and you can get the epoch time like I first explained.
Have fun

Localizing Epoch Time with pytz in Python

Im working on converting epoch timestamps to dates in different timezones with pytz. What I am trying to do is create a DateTime object that accepts an Olson database timezone and an epoch time and returns a localized datetime object. Eventually I need to answer questions like "What hour was it in New York at epoch time 1350663248?"
Something is not working correctly here:
import datetime, pytz, time
class DateTime:
def __init__(self, timezone, epoch):
self.timezone = timezone
self.epoch = epoch
timezoneobject = pytz.timezone(timezone)
datetimeobject = datetime.datetime.fromtimestamp( self.epoch )
self.datetime = timezoneobject.localize(datetimeobject)
def hour(self):
return self.datetime.hour
if __name__=='__main__':
epoch = time.time()
dt = DateTime('America/Los_Angeles',epoch)
print dt.datetime.hour
dt = DateTime('America/New_York',epoch)
print dt.datetime.hour
This prints the same hour, whereas one should be 3 or so hours ahead. Whats going wrong here? I'm a total Python beginner, any help is appreciated!
datetime.fromtimestamp(self.epoch) returns localtime that shouldn't be used with an arbitrary timezone.localize(); you need utcfromtimestamp() to get datetime in UTC and then convert it to a desired timezone:
from datetime import datetime
import pytz
# get time in UTC
utc_dt = datetime.utcfromtimestamp(posix_timestamp).replace(tzinfo=pytz.utc)
# convert it to tz
tz = pytz.timezone('America/New_York')
dt = utc_dt.astimezone(tz)
# print it
print(dt.strftime('%Y-%m-%d %H:%M:%S %Z%z'))
Or a simpler alternative is to construct from the timestamp directly:
from datetime import datetime
import pytz
# get time in tz
tz = pytz.timezone('America/New_York')
dt = datetime.fromtimestamp(posix_timestamp, tz)
# print it
print(dt.strftime('%Y-%m-%d %H:%M:%S %Z%z'))
It converts from UTC implicitly in this case.
For creating the datetime object belonging to particular timezone from a unix timestamp, you may pass the pytz object as a tz parameter while creating your datetime. For example:
>>> from datetime import datetime
>>> import pytz
>>> datetime.fromtimestamp(1350663248, tz= pytz.timezone('America/New_York'))
datetime.datetime(2012, 10, 19, 12, 14, 8, tzinfo=<DstTzInfo 'America/New_York' EDT-1 day, 20:00:00 DST>)
You can get the list of all timezones using pytz.all_timezones which returns exhaustive list of the timezone names that can be used.
Also take a look at List of tz database time zones wiki.
epochdt = datetime.datetime.fromtimestamp(epoch)
timezone1 = timezone("Timezone/String")
adjusted_datetime = timezone1.localize(epochdt)
Working from memory, so excuse any syntax errors, but that should get you on the right track.
EDIT: Missed the part about knowing the hour,etc. Python has great Time/Date Formatting. At pretty much the bottom of that link is the table showing how to pull different attributes from the datetime object.

Categories