The best I can come up with for now is this monstrosity:
>>> datetime.utcnow() \
... .replace(tzinfo=pytz.UTC) \
... .astimezone(pytz.timezone("Australia/Melbourne")) \
... .replace(hour=0,minute=0,second=0,microsecond=0) \
... .astimezone(pytz.UTC) \
... .replace(tzinfo=None)
datetime.datetime(2008, 12, 16, 13, 0)
I.e., in ...
Like an idiot, I completely overlooked the timezone setting when I first built an application that collects datetime data.
It wasn't an issue then because all I was doing was "time-since" style comparisons and ordering. Now I need to do full reports that show the actual datetime and of course, they're all stored at America/Chicago (the ...
I have data that is the utc offset and the utc time, given that is it possible to get the users local timezone (mainly to figure if it is dst etc. probably using pytz), similar to the function in php timezone_name_from_abbr ?
For example:
If my epoch time is: 1238720309
I can get the utc time as:
d = datetime.utcfromtim...
Hi,
What is the right way to convert a naive time and a tzinfo into an utc time?
Say I have:
d = datetime(2009, 8, 31, 22, 30, 30)
tz = timezone('US/Pacific')
First way, pytz inspired:
d_tz = tz.normalize(tz.localize(d))
utc = pytz.timezone('UTC')
d_utc = d_tz.astimezone(utc)
Second way, from UTCDateTimeField
def utc_from_localt...
I'm having some weird issues with pytz's .localize() function. Sometimes it wouldn't make adjustments to the localized datetime:
.localize behaviour:
>>> tz
<DstTzInfo 'Africa/Abidjan' LMT-1 day, 23:44:00 STD>
>>> d
datetime.datetime(2009, 9, 2, 14, 45, 42, 91421)
>>> tz.localize(d)
datetime.datetime(2009, 9, 2, 14, 45, 42, 91421,
...
Hi
I'm reading the not so complete pytz documentation and I'm stuck on understand one part of it.
Converting between timezones also needs special attention. This also needs to use the normalize method to ensure the conversion is correct.
>>> utc_dt = utc.localize(datetime.utcfromtimestamp(1143408899))
>>> utc_dt.strftime(fmt)
'200...
Is there a cross-platform function in python (or pytz) that returns a tzinfo object corresponding to the timezone currently set on the computer?
environment variables cannot be counted on as they are not cross-platform
...
This is a little embarrassing, but I have not been able to find good resources on this topic.
I'm working on a Google App Engine application that requires sophisticated time zone conversions. Since I am nowhere near the imposed quotas, I have opted to go with PyTZ. However, I must be doing something wrong. What I've done so far is:
Do...
Hi, I wan't to manage the different timezones of my users in my web application, but I have no idea where to start. I have to save the local time of each user in my database?, or maybe make the conversion to a UTC time, save it, and then make the conversion again to show it?, or there is another way? For example, if one of my users make ...
How can I convert a date time string of the form Feb 25 2010, 16:19:20 CET to the unix epoch?
Currently my best approach is to use time.strptime() is this:
def to_unixepoch(s):
# ignore the time zone in strptime
a = s.split()
b = time.strptime(" ".join(a[:-1]) + " UTC", "%b %d %Y, %H:%M:%S %Z")
# this puts the time_tupl...
If I convert a UTC datetime to swedish format, summertime is included (CEST). However, while creating a datetime with sweden as the timezone, it gets CET instead of CEST. Why is this?
>>> # Modified for readability
>>> import pytz
>>> import datetime
>>> sweden = pytz.timezone('Europe/Stockholm')
>>>
>>> datetime.datetime(2010, 4, 20, 1...
So I have datetime objects in UTC time and I want to convert them to UTC timestamps. The problem is, time.mktime makes adjustments for localtime.
So here is some code:
import os
import pytz
import time
import datetime
epoch = pytz.utc.localize(datetime.datetime(1970, 1, 1))
print time.mktime(epoch.timetuple())
os.environ['TZ'] = '...
I understand that repr()'s purpose is to return a string, that can be used to be evaluated as a python command and return the same object. Unfortunately, pytz does not seem to be very friendly with this function, although it should be quite easy, since pytz instances are created with a single call:
import datetime, pytz
now = datetime...
I'm having trouble understanding the conversion between the "Etc/GMT-5" timezone and UTC in pytz.
>>> dt = datetime(2009, 9, 9, 10, 0) # September 9 2009, 10:00
>>> gmt_5 = pytz.timezone("Etc/GMT-5")
>>> gmt_5.localize(dt)
datetime.datetime(2009, 9, 9, 10, 0, tzinfo=<StaticTzInfo 'Etc/GMT-5'>)
Everything is fine so far, but then I try...