pytz icon indicating copy to clipboard operation
pytz copied to clipboard

pytz library cannot localize days on boundary of representable dates

Open sezal98 opened this issue 6 years ago • 2 comments

Either of the following will error with an OverflowError: date value out of range:

pytz.timezone('Africa/Abidjan').localize(datetime(year=1, month=1, day=1))

OverflowError: date value out of range

pytz.timezone('Africa/Abidjan').localize(datetime(year=9999, month=12, day=31))

OverflowError: date value out of range

where as the outputs should be

datetime.datetime(1, 1, 1, 0, 0, tzinfo=<TimezoneInfo [Africa/Abidjan, LMT, -1:43:52, STD]>)

datetime.datetime(9999, 12, 31, 0, 0, tzinfo=<TimezoneInfo [Africa/Abidjan, GMT, +00:00:00, STD]>)

respectively

sezal98 avatar Jun 07 '18 04:06 sezal98

This is because in the localize function (tzinfo.py:309 in the 2015.2 release) you look at the previous and the next day, but those might not be representable dates.

sezal98 avatar Jun 07 '18 04:06 sezal98

Has there been a change due to this or still up to user to work around it?

EDIT: I found using .astimezone instead of localize was acceptable for my use case.

michaeltoohig avatar Jul 14 '21 02:07 michaeltoohig