This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: datetime buggy
Type: behavior Stage: resolved
Components: Versions: Python 2.7
process
Status: closed Resolution: third party
Dependencies: Superseder:
Assigned To: Nosy List: belopolsky, r.david.murray, santhoshch, simeon.visser
Priority: normal Keywords:

Created on 2014-12-04 10:30 by santhoshch, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (8)
msg232119 - (view) Author: santhosh (santhoshch) Date: 2014-12-04 10:30
val=datetime.datetime.strptime("2015-02-01",'%Y-%m-%d').date()
zon=pytz.timezone('US/Pacific')
dt=datetime.datetime(val.year,val.month,val.day, tzinfo=zon)
print dt,zon


output:
2015-02-01 00:00:00-07:53 US/Pacific

output should be
2015-02-01 00:00:00-08:00 US/Pacific
msg232125 - (view) Author: Simeon Visser (simeon.visser) Date: 2014-12-04 10:59
Datetimes in local timezones should be created using localize():

>>> zon.localize(datetime.datetime(2015, 2, 1)).isoformat()
'2015-02-01T00:00:00-08:00'

See the two supported methods of creating local datetimes at: http://pytz.sourceforge.net
msg232128 - (view) Author: santhosh (santhoshch) Date: 2014-12-04 11:25
i dont need local timezone
i need other timezones like: (US/Pacific)
msg232130 - (view) Author: santhosh (santhoshch) Date: 2014-12-04 11:49
zon=pytz.timezone('America/New_York')
dt=datetime.datetime(2014,02,01, tzinfo=zon)
print dt,zon

=> 2015-02-01 00:00:00-04:56 America/New_York

zon=pytz.timezone('Asia/Kolkata')
dt=datetime.datetime(2014,02,01, tzinfo=zon)
print dt,zon

=>2015-02-01 00:00:00+05:53 Asia/Kolkata

all most for all timezones it is giving erroronous outputs
we are dealing many timezones
msg232136 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2014-12-04 13:35
pytz is not a part of the python standard library, it is a 3rd party product.  Please reread Simeon's message (he's talking about any timezone), and if you have further questions ask on whatever support forum pytz has, or on the python-list mailing list.
msg232148 - (view) Author: Alexander Belopolsky (belopolsky) * (Python committer) Date: 2014-12-04 19:38
santhosh,

I would be interested to know how this gets resolved.  I find it strange that zon gets fractional UTC offset:

>>> zon
<DstTzInfo 'US/Pacific' LMT-1 day, 16:07:00 STD>

If you ask on a pytz-related forum, please post a link here.

Some other cases:

>>> pytz.timezone('US/Eastern')
<DstTzInfo 'US/Eastern' LMT-1 day, 19:04:00 STD>

>>> pytz.timezone('US/Central')
<DstTzInfo 'US/Central' LMT-1 day, 18:09:00 STD>

>>> pytz.timezone('Asia/Kolkata')
<DstTzInfo 'Asia/Kolkata' LMT+5:53:00 STD>

but

>>> pytz.timezone('Europe/Paris')
<DstTzInfo 'Europe/Paris' LMT+0:09:00 STD>
msg232149 - (view) Author: Alexander Belopolsky (belopolsky) * (Python committer) Date: 2014-12-04 19:44
It looks like pytz documentation [1] specifically warns about this kind of usage:

"""
Unfortunately using the tzinfo argument of the standard datetime constructors ‘’does not work’’ with pytz for many timezones.
"""


[1] http://pytz.sourceforge.net/#localized-times-and-date-arithmetic
msg232192 - (view) Author: santhosh (santhoshch) Date: 2014-12-05 10:39
Thanks simeon.visser,
I understood your point. Didn't got your point earlier.

Hey belopolsky,
Here is the solution
val=datetime.datetime.strptime("2015-02-01",'%Y-%m-%d').date()
zon=pytz.timezone('US/Pacific')

Bad Code:
dt=datetime.datetime(val.year,val.month,val.day, tzinfo=zon)
Good Code:
dt=zon.localize(datetime.datetime(val.year,val.month,val.day))

In Linux, good code works perfectly

Both code are working perfectly in Mac
History
Date User Action Args
2022-04-11 14:58:10adminsetgithub: 67183
2014-12-05 10:39:38santhoshchsetmessages: + msg232192
2014-12-04 19:44:44belopolskysetmessages: + msg232149
2014-12-04 19:38:59belopolskysetmessages: + msg232148
2014-12-04 13:35:41r.david.murraysetstatus: open -> closed

nosy: + r.david.murray
messages: + msg232136

resolution: third party
stage: resolved
2014-12-04 11:49:56santhoshchsetmessages: + msg232130
2014-12-04 11:25:20santhoshchsetmessages: + msg232128
2014-12-04 10:59:10simeon.vissersetnosy: + simeon.visser
messages: + msg232125
2014-12-04 10:49:56pitrousetnosy: + belopolsky
2014-12-04 10:30:01santhoshchcreate