pytz

Comparing a time in UTC with a time in Eastern time using Python

穿精又带淫゛_ 提交于 2019-11-28 13:47:48
I'm trying to compare two times using the Python datetime module, but I can't seem to create a timezone-aware time object in UTC. >>> import pytz, datetime >>> UTC_TZ = pytz.utc >>> EASTERN_TZ = pytz.timezone('America/New_York') >>> d1 = datetime.time(10, tzinfo = UTC_TZ) >>> d1 datetime.time(10, 0, tzinfo=<UTC>) >>> d2 = datetime.time(10, tzinfo = EASTERN_TZ) >>> d2 datetime.time(10, 0, tzinfo=<DstTzInfo 'America/New_York' EST-1 day, 19:00:00 STD>) >>> d1 < d2 Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: can't compare offset-naive and offset-aware times Is

From a timezone and a UTC time, get the difference in seconds vs local time at that point in time

China☆狼群 提交于 2019-11-28 12:51:50
This should be very simple, but I can't quite figure it out in Python. I want to have a function which takes two arguments, a UTC time in seconds and a zoneinfo name like 'Europe/Vienna' and returns the offset in seconds from local time and UTC for that point in time. In C it would be: /* ... code to to set local time to the time zone I want to compare against, not shown here. Then call function below to get difference vs localtime. Hardly an ideal solution, but just to demonstrate what I want in a "lingua franca" (C): */ int get_diff_vs_localtime(const time_t original_utc_time) { struct tm*

Daylight savings time in Python

烂漫一生 提交于 2019-11-28 11:02:53
I am writing a program which deals a lot with timezones and crossing them. The two things I deal with most are creating a datetime object from "now" and then localizing a naive datetime object. To create a datetime object from now in the pacific timezone, I am currently doing this (python 2.7.2+) from datetime import datetime import pytz la = pytz.timezone("America/Los_Angeles") now = datetime.now(la) Is this correct with regards to DST? If not, I suppose I should be doing: now2 = la.localize(datetime.now()) My question is why? Can anyone show me a case where the first is wrong and the seconds

pytz.astimezone not accounting for daylight savings?

拜拜、爱过 提交于 2019-11-28 10:50:41
On 2013 Jun 1 I expect the "PST8PDT" timezone to behave like GMT+7, as it is daylight savings in that timezone. However, it behaves like GMT+8: >>> import pytz, datetime >>> Pacific = pytz.timezone("PST8PDT") >>> datetime.datetime(2013, 6, 1, 12, tzinfo=Pacific).astimezone(pytz.utc) datetime.datetime(2013, 6, 1, 20, 0, tzinfo=<UTC>) In contrast, on 2013 Jan 1 it behaves (correctly) like GMT+8: >>> datetime.datetime(2013, 1, 1, 12, tzinfo=Pacific).astimezone(pytz.utc) datetime.datetime(2013, 1, 1, 20, 0, tzinfo=<UTC>) What am I doing wrong? Thanks in advance! You can't assign the timezone in

ImportError, using pytz in google app engine

不打扰是莪最后的温柔 提交于 2019-11-28 10:29:02
问题 I'm trying to use pytz in my web app to apply a timezone to a datetime object. First I tried importing pytz like this from pytz import timezone Then it raised an ImportError saying that the module "pytz" didn't exist. I tried the same line of code in IDLE and it worked fine. Then I saw something online about gae-pytz. So I changed my code to look like this: from pytz.gae import pytz from pytz import timezone Just like the webpage said. It still has the ImportError. It says: ImportError: No

How to properly add PyTZ to a Google App Engine application?

柔情痞子 提交于 2019-11-28 09:35:42
问题 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: Downloaded PyTZ as a tarball Installed it and copied the pytz directory into the root of my app (it is a sibling of the webapp directory, where app.yaml is located

How to get the common name for a pytz timezone eg. EST/EDT for America/New_York

て烟熏妆下的殇ゞ 提交于 2019-11-28 08:54:43
Given a pytz timezone for a particular user(calculated from his offset), i want to display the common name for that timezone. I'm assuming people are more accustomed to seeing EST or PST instead of spelled out like America/NewYork . Does pytz give me those standard names somewhere, or will i have to manually do this via a table? This could potentially get messy, since for example places are EST in a season and shift to showing EDT during others. Given a pytz timezone for a particular user(calculated from his offset), i want to display the common name for that timezone. I'm assuming people are

How to check if a datetime object is localized with pytz?

北城余情 提交于 2019-11-28 07:08:08
I want to store a datetime object with a localized UTC timezone. The method that stores the datetime object can be given a non-localized datetime (naive) object or an object that already has been localized. How do I determine if localization is needed? Code with missing if condition: class MyClass: def set_date(self, d): # what do i check here? # if(d.tzinfo): self.date = d.astimezone(pytz.utc) # else: self.date = pytz.utc.localize(d) jfs How do I determine if localization is needed? From datetime docs : a datetime object d is aware iff: d.tzinfo is not None and d.tzinfo.utcoffset(d) is not

pytz utc conversion

本秂侑毒 提交于 2019-11-28 03:41:24
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_localtime(dt, tz): dt = dt.replace(tzinfo=tz) _dt = tz.normalize(dt) if dt.tzinfo != _dt.tzinfo: # Houston, we have a problem... # find out which one has a dst offset if _dt.tzinfo.dst(_dt): _dt -= _dt.tzinfo.dst(_dt) else: _dt += dt.tzinfo.dst(dt) return _dt.astimezone(pytz.utc)

How to get the first datetime of a day?

為{幸葍}努か 提交于 2019-11-28 01:28:26
问题 Using pytz and Python 3.4, how to get the first datetime of a given day (lets say, 2014-10-19), in a given timezone (lets say 'America/Sao_Paulo' )? 回答1: Use localize() method to attach the timezone: from datetime import datetime import pytz # $ pip install pytz tz = pytz.timezone('America/Sao_Paulo') naive = datetime(2014, 10, 19) aware = tz.localize(naive, is_dst=None) If you run the code; it generates NonExistentTimeError . How to deal with this error depends on the application e.g., to