Skip to content
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 buggy #67183

Closed
santhoshch mannequin opened this issue Dec 4, 2014 · 8 comments
Closed

datetime buggy #67183

santhoshch mannequin opened this issue Dec 4, 2014 · 8 comments
Labels
type-bug An unexpected behavior, bug, or error

Comments

@santhoshch
Copy link
Mannequin

santhoshch mannequin commented Dec 4, 2014

BPO 22994
Nosy @abalkin, @bitdancer

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:

assignee = None
closed_at = <Date 2014-12-04.13:35:41.041>
created_at = <Date 2014-12-04.10:30:01.555>
labels = ['type-bug']
title = 'datetime buggy'
updated_at = <Date 2014-12-05.10:39:38.528>
user = 'https://bugs.python.org/santhoshch'

bugs.python.org fields:

activity = <Date 2014-12-05.10:39:38.528>
actor = 'santhoshch'
assignee = 'none'
closed = True
closed_date = <Date 2014-12-04.13:35:41.041>
closer = 'r.david.murray'
components = []
creation = <Date 2014-12-04.10:30:01.555>
creator = 'santhoshch'
dependencies = []
files = []
hgrepos = []
issue_num = 22994
keywords = []
message_count = 8.0
messages = ['232119', '232125', '232128', '232130', '232136', '232148', '232149', '232192']
nosy_count = 4.0
nosy_names = ['belopolsky', 'r.david.murray', 'simeon.visser', 'santhoshch']
pr_nums = []
priority = 'normal'
resolution = 'third party'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue22994'
versions = ['Python 2.7']

@santhoshch
Copy link
Mannequin Author

santhoshch mannequin commented Dec 4, 2014

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

@santhoshch santhoshch mannequin added the type-bug An unexpected behavior, bug, or error label Dec 4, 2014
@simeonvisser
Copy link
Mannequin

simeonvisser mannequin commented Dec 4, 2014

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

@santhoshch
Copy link
Mannequin Author

santhoshch mannequin commented Dec 4, 2014

i dont need local timezone
i need other timezones like: (US/Pacific)

@santhoshch
Copy link
Mannequin Author

santhoshch mannequin commented Dec 4, 2014

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

@bitdancer
Copy link
Member

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.

@abalkin
Copy link
Member

abalkin commented Dec 4, 2014

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>

@abalkin
Copy link
Member

abalkin commented Dec 4, 2014

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

@santhoshch
Copy link
Mannequin Author

santhoshch mannequin commented Dec 5, 2014

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

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

2 participants