New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
datetime.datetime.now() mangles tzinfo #43952
Comments
When using the pytz package (http://pytz.sf.net/) to create >>> import pytz
>>> info = pytz.timezone("US/Central")
>>> info
<DstTzInfo 'US/Central' CST-1 day, 18:00:00 STD>
>>> import datetime
>>> now = datetime.datetime.now(tz=info)
>>> now
datetime.datetime(2006, 9, 6, 12, 44, 18, 983849,
tzinfo=<DstTzInfo 'US/Central' CDT-1 day, 19:00:00 DST>)
>>> t2 = datetime.datetime(2006, 9, 6, 12, 44, 18,
983849, tzinfo=info)
>>> t2
datetime.datetime(2006, 9, 6, 12, 44, 18, 983849,
tzinfo=<DstTzInfo 'US/Central' CST-1 day, 18:00:00 STD>)
>>> now.tzinfo == info
False
>>> t2.tzinfo == info
True It appears that datetime.datetime.now() makes an (It's also a little annoying that the timezone arg for Skip |
Logged In: YES Since Tim wrote this code AFAIK, there *had* to be a good |
Logged In: YES
In contrast, the datetime constructor never makes any Beyond that, I have no idea why the pytz class passed to >>> from US import Eastern
>>> from datetime import datetime
>>> now = datetime.now(Eastern)
>>> now
datetime.datetime(2006, 9, 7, 12, 49, 48, 430000,
tzinfo=<US.USTimeZone object at 0x009E89B0>)
>>> t2 = datetime(2006, 9, 7, 12, 49, 48, 430000,
tzinfo=Eastern)
>>> t2
datetime.datetime(2006, 9, 7, 12, 49, 48, 430000,
tzinfo=<US.USTimeZone object at 0x009E89B0>)
>>> now.tzinfo == Eastern
True
>>> t2.tzinfo == Eastern
True
>>> t2.tzinfo is now.tzinfo is Eastern
True I expect the pytz developers could shed light on that. tz.fromutc(self) So if pytz overrides the default In short, I believe your primary question here is about how |
Logged In: YES This is a pytz issue, and a result of me abusing Tim's code >>> import pytz
>>> info = pytz.timezone('US/Central')
>>> info
<DstTzInfo 'US/Central' CST-1 day, 18:00:00 STD>
>>> from datetime import datetime
>>> now = info.localize(datetime.now(), is_dst=True)
>>> now
datetime.datetime(2006, 9, 8, 11, 19, 29, 587943,
tzinfo=<DstTzInfo 'US/Central' CDT-1 day, 19:00:00 DST>)
>>> t2 = info.localize(datetime(2006, 9, 8, 11, 19, 29, 587943))
>>> t2
datetime.datetime(2006, 9, 8, 11, 19, 29, 587943,
tzinfo=<DstTzInfo 'US/Central' CDT-1 day, 19:00:00 DST>)
>>> now.tzinfo == info
False
>>> t2.tzinfo == info
False
>>> now.tzinfo == t2.tzinfo
True Last time I tried, it seemed impossible to support both If I was to add an __eq__ method to the tzinfo classes, I'm The pytz bugtracker is at |
Logged In: YES Based on Stuart's comment, I'm closing this. Skip, if |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: