pytz

How can I remove a pytz timezone from a datetime object?

谁说我不能喝 提交于 2019-11-27 10:24:37
问题 Is there a simple way to remove the timezone from a pytz datetime object? e.g. reconstructing dt from dt_tz in this example: >>> import datetime >>> import pytz >>> dt = datetime.datetime.now() >>> dt datetime.datetime(2012, 6, 8, 9, 27, 32, 601000) >>> dt_tz = pytz.utc.localize(dt) >>> dt_tz datetime.datetime(2012, 6, 8, 9, 27, 32, 601000, tzinfo=<UTC>) 回答1: To remove a timezone (tzinfo) from a datetime object: # dt_tz is a datetime.datetime object dt = dt_tz.replace(tzinfo=None) If you are

when does `datetime.now(pytz_timezone)` fail?

我们两清 提交于 2019-11-27 08:43:01
delorean docs show this way to get the current time in a given timezone using datetime : from datetime import datetime from pytz import timezone EST = "US/Eastern" UTC = "UTC" d = datetime.utcnow() utc = timezone(UTC) est = timezone(EST) d = utc.localize(d) d = est.normalize(EST) and compare it with the delorian-based code: from delorean import Delorean EST = "US/Eastern" d = Delorean(timezone=EST) I believe the datetime example should be written as: from datetime import datetime import pytz eastern_timezone = pytz.timezone("US/Eastern") d = datetime.now(eastern_timezone) that is more concise.

Use Python to find out if a timezone currently in daylight savings time [duplicate]

拈花ヽ惹草 提交于 2019-11-27 08:19:57
This question already has an answer here: Is a specific timezone using DST right now? 1 answer We have a server that runs on GMT time. I need to write a Python script that determines if it's currently (at this very second) Daylight Savings Time (DST) in Los Angeles, CA. How can I accomplish this? I took a look at pytz and time, but I can't figure it out. I realize that I could create some logic such as comparing the current time in LA to GMT time, but it would be a lot cleaner if I could use a standard library instead. Thanks Edit: Here's some sample code of me setting up the timezone: from

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

允我心安 提交于 2019-11-27 07:52:40
问题 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

unexpected results converting timezones in python

送分小仙女□ 提交于 2019-11-27 07:46:51
I'm trying to understand why I'm getting these results when converting timezones to UTC: In [74]: d1 = datetime(2007, 12, 5, 6, 30,tzinfo=pytz.timezone('US/Pacific')) In [75]: d1 Out[75]: datetime.datetime(2007, 12, 5, 6, 30, tzinfo=<DstTzInfo 'US/Pacific' LMT-1 day, **16:07:00 STD**>) In [76]: d1.astimezone(pytz.utc) Out[76]: datetime.datetime(2007, 12, 5, 14, 23, tzinfo=<UTC>) Why did 6:30am become 2:23pm? On the other hand, if I use the following approach, I get the expected result: In [90]: d2 = datetime(2007, 12, 5, 6, 30) In [91]: uspac = pytz.timezone('US/Pacific') In [92]: d2_aware =

pytz and Etc/GMT-5

a 夏天 提交于 2019-11-27 06:59:13
问题 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 to convert that to UTC: >>> gmt_5.localize(dt).astimezone(pytz.utc) datetime.datetime(2009, 9, 9, 5, 0, tzinfo=<UTC>) So to me it seems that when converting

Could not find a version that satisfies the requirement pytz

喜夏-厌秋 提交于 2019-11-27 06:43:25
I have a problem installing pytz in virtualenv. Downloading/unpacking pytz Could not find a version that satisfies the requirement pytz (from versions: 2009r, 2008b, 2009f, 2008c, 2007g, 2011g, 2005m, 2011e, 2007f, 2011k, 2007k, 2006j, 2008h, 2008i, 2011e, 2008a, 2009e, 2006g, 2011j, 2010l, 2005m, 2008i, 2005k, 2008g, 2007c, 2007i, 2009l, 2009r, 2006j, 2011k, 2007d, 2006p, 2009i, 2009u, 2007i, 2009f, 2010g, 2008h, 2009a, 2007g, 2011e, 2006p, 2012b, 2010k, 2005r, 2007f, 2009l, 2009p, 2008c, 2009j, 2008g, 2010g, 2010h, 2011h, 2010k, 2007c, 2007d, 2011d, 2009l, 2011c, 2008a, 2005m, 2007k, 2009n,

Why doesn't pytz localize() produce a datetime object with tzinfo matching the tz object that localized it?

橙三吉。 提交于 2019-11-27 06:03:50
问题 Is there anyone who can help me understand what's going on here? import pytz from datetime import datetime tz = pytz.timezone('Europe/Berlin') print repr(tz) # <DstTzInfo 'Europe/Berlin' LMT+0:53:00 STD> dt = datetime(2011, 1, 3, 18, 40) result = tz.localize(dt) print repr(result.tzinfo) # <DstTzInfo 'Europe/Berlin' CET+1:00:00 STD> assert result.tzinfo == tz, "Why aren't these the same timezone?" My understanding was that the localize() method on a pytz timezone object would take a naive

pytz.astimezone not accounting for daylight savings?

我怕爱的太早我们不能终老 提交于 2019-11-27 05:53:54
问题 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

pytz - Converting UTC and timezone to local time

拈花ヽ惹草 提交于 2019-11-27 04:29:08
问题 I have a datetime in utc time zone, for example: utc_time = datetime.datetime.utcnow() And a pytz timezone object: tz = timezone('America/St_Johns') What is the proper way to convert utc_time to the given timezone? 回答1: I think I got it: pytz.utc.localize(utc_time, is_dst=None).astimezone(tz) This line first converts the naive (time zone unaware) utc_time datetime object to a datetime object that contains a timezone (UTC). Then it uses the astimezone function to adjust the time according to