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

bpo-31800: Support for colon when parsing time offsets #4015

Merged
merged 13 commits into from
Oct 26, 2017

Conversation

mariocj89
Copy link
Contributor

@mariocj89 mariocj89 commented Oct 16, 2017

Add support to strptime to parse time offsets with a colon between the hour and the minutes.

https://bugs.python.org/issue31800

@mariocj89
Copy link
Contributor Author

included the support for 'Z' in 49098f6 happy to remove it or extract it in a different PR.

When ``%z`` directive is provided to the :meth:`strptime` method, any valid
RFC-822/ISO 8601 standard utftime-offset can be parsed. For example, strings
like ``'+01:00'`` will be parsed as an offset of one hour and or ``'Z'`` will
be transofmred to a 0 utc offset.
Copy link
Member

Choose a reason for hiding this comment

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

Please use all caps for "UTC".

Lib/_strptime.py Outdated
@@ -210,7 +210,7 @@ def __init__(self, locale_time=None):
#XXX: Does 'Y' need to worry about having less or more than
# 4 digits?
'Y': r"(?P<Y>\d\d\d\d)",
'z': r"(?P<z>[+-]\d\d[0-5]\d)",
'z': r"(?P<z>[+-]\d\d:?[0-5]\d|Z)",
Copy link
Member

Choose a reason for hiding this comment

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

Please add support for sub-minute offsets. See bpo-5288.

@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 phrase I 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.

mariocj89 and others added 10 commits October 23, 2017 23:57
This change adds support to _strptime to parse time offsets with a colon
between the hour and the minutes. This unblocks the parsing of datetime
string representation generated via isoformat together with all string
that have the colon in its offset (which is quite common and part of the
ISO8606)
@mariocj89
Copy link
Contributor Author

I have made the requested changes; please review again

@bedevere-bot
Copy link

Thanks for making the requested changes!

@abalkin: please review the changes made to this pull request.

@mariocj89
Copy link
Contributor Author

mariocj89 commented Oct 23, 2017

Added both support for second and microseconds worries me that now the offset can be both a float and an int depending on whether there are microseconds in it though.

Open to suggestions!

Thanks

@abalkin
Copy link
Member

abalkin commented Oct 24, 2017

@mariocj89 - the offset is a timedelta, not int or float. Let me take a look at your changes ...

Lib/_strptime.py Outdated
seconds = int(z[5:7] or 0)
microseconds = int(z[8:] or 0)
gmtoff = (hours * 60 * 60) + (minutes * 60) + seconds
gmtoff = gmtoff + (microseconds / (10**6))
Copy link
Member

Choose a reason for hiding this comment

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

Let's not make gmtoff a float, but instead add gmtoff_fraction to the returned tuple. See how %f format is handled.

@mariocj89
Copy link
Contributor Author

Let's not make gmtoff a float, but instead add gmtoff_fraction to the returned tuple. See how %f format is handled.

Great idea, I've added it at the end like the fraction, if you rather want it as part of the time tuple we can try that out as well.

Thanks @abalkin !

@mariocj89
Copy link
Contributor Author

@abalkin I've added some further information & corrected a typo in the NEWS entry.

Do you want me to add some info in Python3.7 what's new as well or it is not really worth it?

@@ -2174,6 +2174,12 @@ Notes:
.. versionchanged:: 3.7
The UTC offset is not restricted to a whole number of minutes.

.. versionchanged:: 3.7
When ``%z`` directive is provided to the :meth:`strptime` method, any valid
RFC-822/ISO 8601 standard utftime-offset can be parsed. For example, strings
Copy link
Member

Choose a reason for hiding this comment

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

utftime-offset? This should be UTC offset, right?

@@ -0,0 +1,3 @@
Add support for parsing RFC-822/ISO 8601 UTC offsets. strptime '%z' can now
Copy link
Member

Choose a reason for hiding this comment

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

I don't think the RFC or the ISO standard allows seconds, let alone microseconds in the UTC offset. Please note that RFC 822 was obsoleted by RFC 2822 which in turn was obsoleted by 5322. I don't think this matters much for this PR because the RFCs specify the TZ format without a colon. I would remove references to standards and just describe what is implemented.

@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 phrase I 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.

@mariocj89
Copy link
Contributor Author

I have made the requested changes; please review again

@bedevere-bot
Copy link

Thanks for making the requested changes!

@abalkin: please review the changes made to this pull request.

@abalkin
Copy link
Member

abalkin commented Oct 26, 2017

Do you want me to add some info in Python3.7 what's new as well or it is not really worth it?

A NEWS entry is enough. The What's New editor will pick it from there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants