I'm working with a dataset with timestamps from two different time zones. Below is what I am trying to do:
1.Create a time object t1 from a string;
2.Set the timezone for t1;
3.Infer the time t2 at a different timezone.
import time
s = "2014-05-22 17:16:15"
t1 = time.strptime(s, "%Y-%m-%d %H:%M:%S")
#Set timezone for t1 as US/Pacific
#Based on t1, calculate the time t2 in a different time zone
#(e.g, Central European Time(CET))
Any answers/comments will be appreciated..!
Use datetime and pytz
import datetime
import pytz
pac=pytz.timezone('US/Pacific')
cet=pytz.timezone('CET')
s = "2014-05-22 17:16:15"
t1 = datetime.datetime.strptime(s, "%Y-%m-%d %H:%M:%S")
pacific = pac.localize(t1)
cet_eur = pacific.astimezone(cet)
print pacific
print cet_eur
2014-05-22 17:16:15-07:00
2014-05-23 02:16:15+02:00
I think you want datetime.timetuple
Return a time.struct_time such as returned by time.localtime(). The hours, minutes and seconds are 0, and the DST flag is -1. d.timetuple() is equivalent to time.struct_time((d.year, d.month, d.day, 0, 0, 0, d.weekday(), yday, -1)), where yday = d.toordinal() - date(d.year, 1, 1).toordinal() + 1 is the day number within the current year starting with 1 for January 1st.
print datetime.date.timetuple(t1)
time.struct_time(tm_year=2014, tm_mon=5, tm_mday=22, tm_hour=17, tm_min=16, tm_sec=15, tm_wday=3, tm_yday=142, tm_isdst=-1)
That is a quick draft but the pytz docs have lots of good and clear examples
Related
I'm modifying our pacific time zone filter to include a time option. I don't want the time component to be shown if midnight. The only import thus far we are using is dateutil.parser. Any pointers on best solution would be appreciated! Thanks.
def to_pacific_date_str(timestamp, format='%Y-%m-%d', time=False):
pacific_timestamp = timestamp
if time:
format='%Y-%m-%d %H:%M' # 2016-10-03 00:00
if timestamp.tzname() is None:
# setting timezone lost when pulled from DB
utc_timestamp = timestamp.replace(tzinfo=pytz.utc)
# always converting to pacific timezone
pacific_timestamp = utc_timestamp.astimezone(pytz.timezone('US/Pacific'))
return pacific_timestamp.strftime(format)
I believe the best thing to do would be to just take the time() from the datetime before passing it, then compare that to datetime.time(0, 0).
import pytz
import datetime
def to_pacific_date_str(timestamp, date_fmt='%Y-%m-%d', time=False):
pacific_timestamp = timestamp
if timestamp.tzinfo is None:
# setting timezone lost when pulled from DB
utc_timestamp = timestamp.replace(tzinfo=pytz.utc)
# always converting to pacific timezone
pacific_timestamp = utc_timestamp.astimezone(pytz.timezone('US/Pacific'))
if time and pacific_timestamp.time() != datetime.time(0, 0):
date_fmt = '%Y-%m-%d %H:%M' # 2016-10-03 00:00
return pacific_timestamp.strftime(date_fmt)
Note that I've changed format to date_fmt, because format() is already a builtin. Also, from a design standpoint, it's probably not a great idea to have time override the specified format string, so maybe change the "add time" portion to be date_fmt = date_fmt + ' %H:%M'.
Demonstration:
>>> PST = pytz.timezone('US/Pacific')
>>> to_pacific_date_str(PST.localize(datetime.datetime(2015, 4, 1, 0, 0)), time=True)
'2015-04-01'
>>> PST = pytz.timezone('US/Pacific')
>>> to_pacific_date_str(PST.localize(datetime.datetime(2015, 4, 1, 2, 0)), time=True)
'2015-04-01 02:00'
Try this for UTC:
def checkIfMidnight():
return (time.time() % 86400) == 0
To check if the time is midnight:
from datetime import datetime
def checkIfMidnight():
now = datetime.now()
seconds_since_midnight = (now - now.replace(hour=0, minute=0, second=0, microsecond=0)).total_seconds()
return seconds_since_midnight == 0
Alternatively you can use the .hour, .minute and .second attributes of the datetime object. Like this:
from datetime import datetime as dt
from pytz import timezone
now = dt.now(timezone('US/Pacific'))
midnight = now.hour == 0 and now.minute == 0 and now.second == 0 and now.microsecond == 0
midnight is a boolean indicating if it is midnight in the US/Pacific timezone.
I am not sure if this is the solution you are/were looking for but personally I use simple comparison:
import datetime
...
time == datetime.time(hour=0, minute=0, second=0, microsecond=0)
where time is TimeObject (datetime.time)
I am trying to investigate how to find these numbers, but honestly the answers I have been finding around online have been confusing me. I'm trying to figure out how to get the time of the start of today, and of the most recent Monday.
I read about time.time(), but that gives me the time right now.
I prefer to work with datetime, then you can easily convert that to a timestamp: How to convert a Python datetime object to seconds.
To get today's date, with the time set to zero (midnight) in local time:
today = datetime.datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
If you do this you will probably want to convert the time zone to UTC before converting to a timestamp. If you'd rather take midnight in UTC:
today_utc = datetime.datetime.utcnow().replace(hour=0, minute=0, second=0, microsecond=0)
Now to adjust to Monday, you need to know which day of the week this represents. Handily there's a weekday method that uses 0 for Monday, so you can just subtract:
monday = today - datetime.timedelta(days=today.weekday())
Something like this should get you the unix timestamp for midnight today in your local timezone:
today = datetime.date.today()
time.mktime((today.year, today.month, today.day, 0, 0, 0, 0, 0, 0))
And this should get you midnight on Monday, since Monday is considered the 0th day of the week:
monday = today + datetime.timedelta(days=-today.weekday())
time.mktime((monday.year, monday.month, monday.day, 0, 0, 0, 0, 0, 0))
Get UNIX timestamp of the start of today in Python
See How do I get the UTC time of “midnight” for a given timezone? to get an aware datetime object that represents midnight. To get Unix timestamp from that object, call its .timestamp() method.
Get UNIX timestamp of this week's monday
The code almost the same for the monday's timestamp:
from datetime import datetime, time, timedelta
import tzlocal # $ pip install tzlocal
local_timezone = tzlocal.get_localzone()
today = datetime.now(local_timezone).date()
monday = today - timedelta(today.weekday())
dt = local_timezone.localize(datetime.combine(today, time.min), is_dst=None)
unix_time = dt.timestamp()
If you need .timestamp() implementation for <3.3 Python version, see Converting datetime.date to UTC timestamp in Python.
I have a timestamp that represents milliseconds since 1970 1432202088224 which translates to Thursday, May 21, 2015 5:54:48 AM EDT. I'd like to write a python function that converts that timestamp to milliseconds in GMT. I can't naively add four hours (3600000 milliseconds) to the existing timestamp because half the year i'll be off by one hour.
I've tried writing a function using datetime and pytz
def convert_mills_GMT(milliseconds):
converted_raw = datetime.datetime.fromtimestamp(milliseconds/1000.0)
date_eastern = eastern.localize(converted_raw, is_dst=True)
date_utc = date_eastern.astimezone(utc)
return int(date_utc.strftime("%s")) * 1000
using the input of 1432202088224 this function returns 1432220088000 which is Thursday, May 21, 2015 10:54:48 AM EDT when what I want is 9:54 AM. what am I missing?
There is no such thing as "EST timestamp". If you need "GMT timestamp" then you already have it.
To get UTC time from a POSIX timestamp given as number of milliseconds:
>>> from datetime import datetime, timedelta
>>> timestamp = 1432202088224
>>> utc_time = datetime(1970, 1, 1) + timedelta(milliseconds=timestamp)
>>> utc_time.strftime('%A, %B %d, %Y %H:%M:%S %p UTC')
'Thursday, May 21, 2015 09:54:48 AM UTC'
We can check that the result is correct by converting the UTC time back to "EST" timezone:
>>> import pytz # $ pip install pytz
>>> est = utc_time.replace(tzinfo=pytz.utc).astimezone(pytz.timezone('US/Eastern'))
>>> est.strftime('%A, %B %d, %Y %H:%M:%S %p %Z')
'Thursday, May 21, 2015 05:54:48 AM EDT'
Don't use .strftime("%s"). It is not supported, and may silently fail. Instead, to convert a UTC datetime to a timestamp use one of the methods shown here depending on your version of Python:
Python 3.3+:
timestamp = dt.timestamp()
Python3 (< 3.3):
epoch = datetime(1970, 1, 1, tzinfo=timezone.utc)
timestamp = (dt - epoch) / timedelta(seconds=1)
Python 2.7+:
timestamp = (dt.replace(tzinfo=None) - datetime(1970, 1, 1)).total_seconds()
Python2 (< 2.7):
def totimestamp(dt, epoch=datetime(1970,1,1)):
td = dt - epoch
# return td.total_seconds()
return (td.microseconds + (td.seconds + td.days * 86400) * 10**6) / 10**6
timestamp = totimestamp(dt.replace(tzinfo=None))
Therefore, your convert_mills_GMT should look like
def convert_mills_GMT(milliseconds,
utc=pytz.utc,
eastern=pytz.timezone('US/Eastern')
):
converted_raw = DT.datetime.fromtimestamp(milliseconds/1000.0)
date_eastern = eastern.localize(converted_raw, is_dst=True)
date_utc = date_eastern.astimezone(utc)
timestamp = ...
return int(timestamp) * 1000
For example, with Python2.7,
import datetime as DT
import pytz
def convert_mills_GMT(milliseconds,
utc=pytz.utc,
eastern=pytz.timezone('US/Eastern')
):
converted_raw = DT.datetime.fromtimestamp(milliseconds/1000.0)
date_eastern = eastern.localize(converted_raw, is_dst=True)
date_utc = date_eastern.astimezone(utc)
timestamp = ((date_utc.replace(tzinfo=None) - DT.datetime(1970, 1, 1))
.total_seconds())
return int(timestamp) * 1000
print(DT.datetime.utcfromtimestamp(convert_mills_GMT(1432202088224)/1000.0))
prints
2015-05-21 09:54:48
In the code below, I am calculating now epoch and beginning of current day epoch.
import time
import pytz
from datetime import datetime
tz1 = pytz.timezone('CST6CDT')
utc = pytz.timezone('UTC')
now = pytz.UTC.localize(datetime.utcnow())
now_tz = now.astimezone(tz1)
print now_tz
print now_tz.strftime('%s')
begin_day = now_tz.replace(hour=0, minute=0, second=0)
print begin_day
print begin_day.strftime('%s')
print statements:
2012-08-28 13:52:21.595718-05:00
1346187141
2012-08-28 00:00:00.595718-05:00
1346137200
Converting epochs to timestamp with CDT timezone:
1346187141 - Aug 28 2012 15:52:21,
1346137200 - Aug 28 2012 02:00:00
I'd like the second epoch to be beginning of the day but it's 2 am. It looks like it is still using local timezone PST when converting to epoch.
What am I doing wrong ? or can this be done a different way?
Thanks!
To convert a datetime with timezone to epoch (POSIX timestamp):
from datetime import datetime
import pytz
tz = pytz.timezone('CST6CDT')
# a datetime with timezone
dt_with_tz = tz.localize(datetime(2012, 8, 28, 19, 33, 50), is_dst=None)
# get timestamp
ts = (dt_with_tz - datetime(1970, 1, 1, tzinfo=pytz.utc)).total_seconds()
# -> 1346200430.0
It is how datetime.timestamp method is implemented for timezone-aware datetime objects in Python 3.
To get "now epoch":
from datetime import datetime
now_epoch = (datetime.utcnow() - datetime(1970, 1, 1)).total_seconds()
Or (assuming time uses POSIX epoch):
import time
now_epoch = time.time()
Getting "beginning of current day epoch" is more complex because current day may be different in different timezones:
from datetime import datetime, time
import pytz
tz = pytz.timezone('CST6CDT')
# get current date in given timezone
today = datetime.now(tz).date()
# -> datetime.date(2013, 6, 22)
# get beginning of current day in given timezone as a datetime with timezone
midnight = tz.localize(datetime.combine(today, time(0, 0)), is_dst=None)
# -> datetime.datetime(2013, 6, 22, 0, 0, tzinfo=<DstTzInfo 'CST6CDT'...>)
# get timestamp
ts = (midnight - datetime(1970, 1, 1, tzinfo=pytz.utc)).total_seconds()
# -> 1371877200.0
See How do I get the UTC time of “midnight” for a given timezone?.
To get "beginning of current day epoch" assuming UTC date:
from datetime import datetime, date
# get current date in UTC
utc_date = datetime.utcnow().date()
# -> datetime.date(2013, 6, 23)
# get timestamp
ts = (utc_date - date(1970, 1, 1)).days * 86400
# -> 1371945600
See Converting datetime.date/datetime.datetime to UTC timestamp in Python.
NOTE: My answer is flat-out wrong. (I'd like to delete it, but am unable to do so until the accept flag is removed.)
Please see J.F.Sebastian's answer.
Here is code demonstrating a value of now_tz for which our two methods produce different results.
import calendar
import pytz
import datetime as dt
tz1 = pytz.timezone('US/Eastern')
utc = pytz.timezone('UTC')
now = utc.localize(dt.datetime(2002, 10, 28), is_dst=None)
now_tz = now.astimezone(tz1)
now_epoch = calendar.timegm(now_tz.utctimetuple())
begin_day = tz1.normalize(now_tz.replace(hour=0, minute=0, second=0))
midnight = tz1.localize(dt.datetime.combine(now_tz, dt.time(0, 0)), is_dst=None)
if begin_day != midnight:
print(begin_day)
# 2002-10-27 01:00:00-04:00 # my result -- is not midnight
print(midnight)
# 2002-10-27 00:00:00-04:00 # J.F.Sebastian's result is correct
(Original answer redacted)
the latest release of simple-date (version 0.2 on pypi) will manage the details for you:
>>> from simpledate import *
>>> now_utc = SimpleDate(tz='UTC')
>>> now_tz = now_utc.convert(tz='CST6CDT')
>>> begin_day = now_tz.replace(hour=0, minute=0, second=0, microsecond=0)
>>> now_utc.timestamp
1371950295.777453
>>> now_tz.timestamp
1371950295.777453
>>> begin_day.timestamp
1371877200.0
we can go backwards to check the timestamps (although it's clear above that switching timezone didn't change the epoch, while moving to start of day did):
>>> SimpleDate(1371877200.0, tz='CST6CDT')
SimpleDate('2013-06-22 00:00:00.000000 CDT', tz='CST6CDT')
>>> SimpleDate(1371877200.0, tz='UTC')
SimpleDate('2013-06-22 05:00:00.000000 UTC')
I have a time difference
import time
import datetime
time1 = datetime.datetime.fromtimestamp(time.mktime(time.gmtime()))
...
time2 = datetime.datetime.fromtimestamp(time.mktime(time.gmtime()))
diff = time2 - time1
Now, how do I find the total number of seconds that passed? diff.seconds doesn't count days. I could do:
diff.seconds + diff.days * 24 * 3600
Is there a built-in method for this?
Use timedelta.total_seconds().
>>> import datetime
>>> datetime.timedelta(seconds=24*60*60).total_seconds()
86400.0
You have a problem one way or the other with your datetime.datetime.fromtimestamp(time.mktime(time.gmtime())) expression.
(1) If all you need is the difference between two instants in seconds, the very simple time.time() does the job.
(2) If you are using those timestamps for other purposes, you need to consider what you are doing, because the result has a big smell all over it:
gmtime() returns a time tuple in UTC but mktime() expects a time tuple in local time.
I'm in Melbourne, Australia where the standard TZ is UTC+10, but daylight saving is still in force until tomorrow morning so it's UTC+11. When I executed the following, it was 2011-04-02T20:31 local time here ... UTC was 2011-04-02T09:31
>>> import time, datetime
>>> t1 = time.gmtime()
>>> t2 = time.mktime(t1)
>>> t3 = datetime.datetime.fromtimestamp(t2)
>>> print t0
1301735358.78
>>> print t1
time.struct_time(tm_year=2011, tm_mon=4, tm_mday=2, tm_hour=9, tm_min=31, tm_sec=3, tm_wday=5, tm_yday=92, tm_isdst=0) ### this is UTC
>>> print t2
1301700663.0
>>> print t3
2011-04-02 10:31:03 ### this is UTC+1
>>> tt = time.time(); print tt
1301736663.88
>>> print datetime.datetime.now()
2011-04-02 20:31:03.882000 ### UTC+11, my local time
>>> print datetime.datetime(1970,1,1) + datetime.timedelta(seconds=tt)
2011-04-02 09:31:03.880000 ### UTC
>>> print time.localtime()
time.struct_time(tm_year=2011, tm_mon=4, tm_mday=2, tm_hour=20, tm_min=31, tm_sec=3, tm_wday=5, tm_yday=92, tm_isdst=1) ### UTC+11, my local time
You'll notice that t3, the result of your expression is UTC+1, which appears to be UTC + (my local DST difference) ... not very meaningful. You should consider using datetime.datetime.utcnow() which won't jump by an hour when DST goes on/off and may give you more precision than time.time()
More compact way to get the difference between two datetime objects and then convert the difference into seconds is shown below (Python 3x):
from datetime import datetime
time1 = datetime.strftime('18 01 2021', '%d %m %Y')
time2 = datetime.strftime('19 01 2021', '%d %m %Y')
difference = time2 - time1
difference_in_seconds = difference.total_seconds()
You can use mx.DateTime module
import mx.DateTime as mt
t1 = mt.now()
t2 = mt.now()
print int((t2-t1).seconds)