-
Notifications
You must be signed in to change notification settings - Fork 45
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
Implement fromutc
for FixedOffset
#113
Conversation
33ecb53
to
ca2fe6d
Compare
Thank you! |
60e3b99
to
2b8eeb5
Compare
2b8eeb5
to
e6078d0
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks good.
But I don't understand why fromutc()
works this way in general.
tzinfo.fromutc(dt)
This is called from the default datetime.astimezone() implementation. When called from that, dt.tzinfo is self, and dt’s date and time data are to be viewed as expressing a UTC time.
If it views the dt
as expressing UTC time, why does it expect a datetime with a non-UTC tzinfo? Shouldn't it just take an unaware datetime? Just doesn't make sense to me or I'm missing something. But datetimes and timezones in Python are hard.
…parameter names to match upstream
They cannot be called without parameters
e6078d0
to
85f594d
Compare
Yeah, the design doesn't seem to be all that sensible. That's why it took me so many weeks to make this fairly simple fix. Every time I read the docs I got confused by the disconnect between what the docs say and the actual implementation code in cpython. At this point, I'm fairly confident in what I've done here, and stuck to what the implementation of |
Looks like it isn't building: https://app.circleci.com/pipelines/github/closeio/ciso8601/175/workflows/346aa53f-c46a-41c0-997d-03ac8f8adbb0/jobs/2309 |
@thomasst It's been failing since 2021-11-14. It's unrelated to this PR. I'll look into it. Update: Here is the fix. |
What are you trying to accomplish?
Fixes #108. cc @davidkraljic @deargle
What approach did you choose and why?
The docs are a little inconsistent when they describe what the behaviour of
dst
should be for fixed offsettzinfo
objects:The way I think about fixed offset
tzinfo
objects, is that they don't know any DST information, so they should returnNone
.Indeed, that is what the
timezone_dst
upstream code does.However, the docs continue to say...
Which suggests that a fixed-offset implementation should return
timedelta(0)
.Indeed, the default implementation of
fromutc
checks to ensure thatdst
does not returnNone
(which is #108).The
timezone
implementation avoids this by implementing its ownfromutc
.So that covers the two options I had to fix this...
FixedOffset.dst
to returntimedelta(0)
None
, but implement afromutc
methodI chose the latter, since I want to maintain exact behaviour with
timezone
. It also matches my understanding of how fixed offset implementations should behave; returningtimedelta(0)
implies that it knows something about DST, which it does not.I copied the upstream implementation , and made the minimal modifications so that it would work outside of the cPython codebase.
In the process, I uncovered a few more latent bugs:
utcoffset
,dst
, andtzname
without any parameters, which is wrong but went unnoticed since none of those methods actually use the parameters (and the methods were marked asMETH_VARARGS
and notMETH_O
.tzname
in Python 2.7 needs to return astr
, when it was returning aunicode
What should reviewers focus on?
🤷