Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

bpo-30699: Improve example on tzinfo instances#4290

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
vstinner merged 2 commits intopython:masterfrommariocj89:gmtdocs
Jun 4, 2019

Conversation

mariocj89
Copy link
Contributor

@mariocj89mariocj89 commentedNov 5, 2017
edited by bedevere-bot
Loading

Move from GMTX to TZX when naming the classes, as GMT1 might be rather confusing as seen in the reported issue.
In addition, move to UTC over GMT and improve the tzname implementation.

https://bugs.python.org/issue30699

Copy link
Member

@vstinnervstinner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

LGTM.

@Mariatta
Copy link
Member

Appveyor was stuck. I'll close and re-open the PR to retrigger it.

@MariattaMariatta reopened thisMar 1, 2018
@Mariatta
Copy link
Member

Thanks, the changes LGTM. If no objection from@abalkin I'll merge it.

@mariocj89
Copy link
ContributorAuthor

Thanks a lot for reviewing this old PRs :) really appreciated

@abalkin
Copy link
Member

abalkin commentedMar 1, 2018
edited
Loading

If we are going to update these examples, I think we should make themPEP 495 compliant and I don't think this PR achieves it.

Mariatta reacted with thumbs up emoji

@mariocj89
Copy link
ContributorAuthor

@abalkin not sure I understand what you mean. The examples are moved form UTC naming to GMT, there is no geographical timezone involved and therefore the fold attribute is not relevant. Isn't it?

@abalkin
Copy link
Member

abalkin commentedMar 2, 2018
edited
Loading

@mariocj89 - the fold attribute is only irrelevant for a TZ with a never changing offset. As soon as you have DST changes as is the case in these examples, you have to take fold into account when implementing the.utcoffset() and.dst() methods and the.fromutc() method should properly set the fold attribute. SeeGuidelines for New tzinfo Implementations.

@mariocj89
Copy link
ContributorAuthor

mariocj89 commentedMar 2, 2018
edited
Loading

@abalkin absolutely right, missed theUTC +2" if self.dst(dt) else "UTC +1 in the example. I'll have a look into updating the PR to include that

abalkin
abalkin previously requested changesMar 2, 2018
Copy link
Member

@abalkinabalkin left a comment
edited
Loading

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Let's make the examplesPEP 495 compliant.

@bedevere-bot
Copy link

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phraseI have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

And if you don't make the requested changes,you will be poked with soft cushions!

@pganssle
Copy link
Member

@abalkin I trust Mario to come up with something here, but I do think writing a fold-aware example implementation would be way easier ifthis issue were resolved. I think that would also makefromutc implementations that wraptzinfo.fromutc much faster, since most of the code would be executed in C (though I could be wrong on that point).

I think the fact that it's actually fairly difficult to write afromutc that's simple enough for a documentation example might be a sign that we should try to alleviate that cognitive overhead by having basicfold support intzinfo.fromutc.

@mariocj89
Copy link
ContributorAuthor

I am currently quite overwhelmed with work and other organisations, but as soon as I have time i’d Like to get into it and update the examples. I realised when doing it that it is not a 10 minutes job :p

TL;DR
I haven’t forgotten, will get into it “soon”

@mariocj89
Copy link
ContributorAuthor

@abalkin The code for the timezone classes with support for dst and fold attributes is rather complex.

Given the section is to illustrate how to work with datetime and tzinfo objects, what about moving to timezone.utc and a user-defined timezone class based on Kabul? I chose Kabul as it moved its offset once but has no ambiguous times (only imaginary).

This still showcases how datetime and tzinfo interact and the code is much simpler!

Note there are examples on how to work with tzinfo further down the file.

Example implementation:mariocj89@c132630

I can push this to this PR for review if the idea looks good.

@abalkin
Copy link
Member

@mariocj89 - Using Asia/Kabul for the documentation example sounds fine, but your implementation is incomplete. For the "imaginary" times the result of utcoffset() must depend on the fold value. SeeMind the Gap section in PEP 495.

@mariocj89
Copy link
ContributorAuthor

Good point! Always thought that the PEP referred only to DST but indeed apply to all “variable offsets”.

Will then update it and push it to this branch. Thanks.

abalkin reacted with thumbs up emoji

@pganssle
Copy link
Member

As a note, if something like#7425 (which addsfold support directly into the defaultfromutc implementation) were to be merged, you could write a super-basic fold-aware implementation like this:

fromdatetimeimportdatetime,timedelta,tzinfo,timezoneUTC=timezone.utcclassExampleFold(tzinfo):deftzname(self,dt):ifself.dst(dt):return"DST"else:return"STD"defutcoffset(self,dt):returntimedelta(hours=0)+self.dst(dt)defdst(self,dt):dt_start=datetime(1,3,17,1).replace(year=dt.year)dt_end=datetime(1,11,4,1).replace(year=dt.year)naive_dt=dt.replace(tzinfo=None)dst_offset=timedelta(hours=1)ifdt_start<=naive_dt<dt_end:returndst_offsetelse:ifnaive_dt<dt_end+dt_offset:ifdt.fold:returntimedelta(0)else:returndst_offsetreturntimedelta(0)

Obviously that's a sorta weird case because there's no timezone that uses fixed days of the year for offsets, but you can see how easy the "add fold support" part is oncefromutc respects it.

@mariocj89
Copy link
ContributorAuthor

mariocj89 commentedJun 5, 2018
edited
Loading

@abalkin I've pushed a potential example implementation. Had to overridefromutc as well, maybe there is a simpler way :/ Let me know if anything needs changing.

Also updated the content to pass doctests (only the modified part).

Tested that Kabul implementation against the dateutil one before at and after the move.

@serhiy-storchakaserhiy-storchaka added the docsDocumentation in the Doc dir labelDec 8, 2018
@vstinner
Copy link
Member

I removed the " needs backport to 3.6" label, the 3.6 branch no longer accept bugfixes (only security fixes are accepted):https://devguide.python.org/#status-of-python-branches

Copy link
Member

@pgansslepganssle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Almost all of my comments here are completely optional and I think this can be merged whether they are incorporated or not.

The only "hard" change that I recommend is the use of"+01:00" instead of"UTC+1" in theTZ1 class.

datetime.timedelta(seconds=3600)
datetime.timedelta(0, 14400)
>>> # Datetime after the change
>>> dt2 = datetime(2006, 6, 14, 13, 0, tzinfo=tz1)
>>> dt2.utcoffset()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Can we change this to:

>>>print(dt.utcoffset())4:30:00

It's a very minor thing, but to me the standardrepr fortimedelta is completely unreadable.

... return timedelta(0)
... def tzname(self,dt):
... return "GMT +2"
... return "+04"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I would mildly prefer"+04:00" here for consistency, but I don't care strongly about this, it's just a documentation example and I do realize that@abalkin suggested this label.

... def utcoffset(self, dt):
... return timedelta(hours=1)
... def dst(self, dt):
... return timedelta(0)
... def tzname(self,dt):
... return "Europe/Prague"
... return "UTC+1"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Super annoyingly,POSIX-style offset names have an inverted sense, sodateutil.parser.parse (and I thinkTZ variables) would interpretUTC+1 as the same as-01:00. Of course, many people use this in the opposite sense, which gives rise to some confusion.

I recommend bypassing the whole thing by returning "+01:00" here.

Move from GMTX to TZX when naming the classes, as GMT1 might be ratherconfusing as seen in the reported issue.In addition, move to UTC over GMT and improve the tzname implementation.
Move the example in the documentation to just use timezone.utc and auser defined Kabul timezone rather than having two user definedtimezones with DST.Kabul timezone is still interesting as it changes its offset but notbased on DST. This is more accurate as the previous example was missinginformation about the fold attribute. Additionally, implementing the foldattribute was rather complex and probably not relevant enough for thesection "datetime with tzinfo".
@csabella
Copy link
Contributor

@mariocj89@pganssle
There was a lot of activity on this issue and 2 approvals, but I'm not quite sure where it's at. Can you take another look and comment if it's ready to merge? Thanks!

@csabellacsabella requested a review fromabalkinJune 1, 2019 01:23
@mariocj89
Copy link
ContributorAuthor

Wow! Totally forgot about this PR 😆 .

@abalkinrequested to make this PEP 495 complient.

There were some suggestions by@pganssle as well but were the opposite of what@abalkin was proposing when formatting offsets.

Let me know if there is any change to make!

@pganssle
Copy link
Member

We can ignore any of my comments that contradict@abalkin's. This is clearly an improvement over the previous state of affairs, and we can always change it later.

@vstinner
Copy link
Member

@pganssle: You didn't approve this PR. Does it mean that it still lacks someone, or is it good as it is? If yes, can you please officially approve it?

Copy link
Member

@pgansslepganssle left a comment
edited
Loading

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

@vstinner No problem, approved. I think this change is clearly an improvement over the status quo.

I think documentation backports are only going back to 3.7 now anyway, but in case they aren't, this relies on PEP 495, so it can't be backported any further than 3.6. I think it's fine to backport to 3.7 unless@mariocj89 disagrees.

@vstinnervstinner dismissedabalkin’sstale reviewJune 4, 2019 15:17

Paul wrote: "We can ignore any of my comments that contradict@abalkin's."

@vstinnervstinner merged commitf0b5ae4 intopython:masterJun 4, 2019
@miss-islington
Copy link
Contributor

Thanks@mariocj89 for the PR, and@vstinner for merging it 🌮🎉.. I'm working now to backport this PR to: 3.7.
🐍🍒⛏🤖 I'm not a witch! I'm not a witch!

miss-islington pushed a commit to miss-islington/cpython that referenced this pull requestJun 4, 2019
* Improve example on tzinfo instancesMove from GMTX to TZX when naming the classes, as GMT1 might be ratherconfusing as seen in the reported issue.In addition, move to UTC over GMT and improve the tzname implementation.* Simplify datetime with tzinfo exampleMove the example in the documentation to just use timezone.utc and auser defined Kabul timezone rather than having two user definedtimezones with DST.Kabul timezone is still interesting as it changes its offset but notbased on DST. This is more accurate as the previous example was missinginformation about the fold attribute. Additionally, implementing the foldattribute was rather complex and probably not relevant enough for thesection "datetime with tzinfo".(cherry picked from commitf0b5ae4)Co-authored-by: Mario Corchero <mcorcherojim@bloomberg.net>
@vstinner
Copy link
Member

I think documentation backports are only going back to 3.7 now anyway, but in case they aren't, this relies on PEP 495, so it can't be backported any further than 3.6. I think it's fine to backport to 3.7 unless@mariocj89 disagrees.

Python 3.6 no longer accept doc changes, only security fixes:https://devguide.python.org/#status-of-python-branches

@bedevere-bot
Copy link

GH-13811 is a backport of this pull request to the3.7 branch.

miss-islington added a commit that referenced this pull requestJun 4, 2019
* Improve example on tzinfo instancesMove from GMTX to TZX when naming the classes, as GMT1 might be ratherconfusing as seen in the reported issue.In addition, move to UTC over GMT and improve the tzname implementation.* Simplify datetime with tzinfo exampleMove the example in the documentation to just use timezone.utc and auser defined Kabul timezone rather than having two user definedtimezones with DST.Kabul timezone is still interesting as it changes its offset but notbased on DST. This is more accurate as the previous example was missinginformation about the fold attribute. Additionally, implementing the foldattribute was rather complex and probably not relevant enough for thesection "datetime with tzinfo".(cherry picked from commitf0b5ae4)Co-authored-by: Mario Corchero <mcorcherojim@bloomberg.net>
@vstinner
Copy link
Member

Thanks Mario Corchero for the doc enhancement, and Paul for the review!

DinoV pushed a commit to DinoV/cpython that referenced this pull requestJan 14, 2020
* Improve example on tzinfo instancesMove from GMTX to TZX when naming the classes, as GMT1 might be ratherconfusing as seen in the reported issue.In addition, move to UTC over GMT and improve the tzname implementation.* Simplify datetime with tzinfo exampleMove the example in the documentation to just use timezone.utc and auser defined Kabul timezone rather than having two user definedtimezones with DST.Kabul timezone is still interesting as it changes its offset but notbased on DST. This is more accurate as the previous example was missinginformation about the fold attribute. Additionally, implementing the foldattribute was rather complex and probably not relevant enough for thesection "datetime with tzinfo".
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@vstinnervstinnervstinner approved these changes

@pgansslepgansslepganssle approved these changes

@MariattaMariattaMariatta approved these changes

@abalkinabalkinAwaiting requested review from abalkin

Assignees
No one assigned
Labels
docsDocumentation in the Doc dirskip news
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

10 participants
@mariocj89@Mariatta@abalkin@bedevere-bot@pganssle@vstinner@csabella@miss-islington@serhiy-storchaka@the-knights-who-say-ni

[8]ページ先頭

©2009-2025 Movatter.jp