-
Notifications
You must be signed in to change notification settings - Fork 10.6k
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
xds/cleanup: fix error datetime fromisoformat not available #28953
Conversation
item['creationTimestamp']) <= get_expire_timestamp(): | ||
if datetime.datetime.strptime( | ||
item['creationTimestamp'], | ||
"%Y-%m-%dT%H:%M:%S.%f%z") <= get_expire_timestamp(): |
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.
TIL neither fromisoformat
nor strptime are good enough ISO time parsers.
(1) fromisoformat
: https://docs.python.org/3/library/datetime.html#datetime.datetime.fromisoformat
Caution This does not support parsing arbitrary ISO 8601 strings - it is only intended as the inverse operation of datetime.isoformat(). A more full-featured ISO 8601 parser, dateutil.parser.isoparse is available in the third-party package dateutil.
And it doesn't support Zulu time:
>>> datetime.fromisoformat("2008-09-03T20:56:35.450686Z")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: Invalid isoformat string: '2008-09-03T20:56:35.450686Z'
(2) strptime
does is pretty terrible with timezones in python3.6: https://stackoverflow.com/a/30696682/11697987
# zone with `:` separators not supported
>>> datetime.strptime("2008-09-03T20:56:35.450686+04:00", "%Y-%m-%dT%H:%M:%S.%f%z")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/_strptime.py", line 565, in _strptime_datetime
tt, fraction = _strptime(data_string, format)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/_strptime.py", line 362, in _strptime
(data_string, format))
ValueError: time data '2008-09-03T20:56:35.450686+04:00' does not match format '%Y-%m-%dT%H:%M:%S.%f%z'
# Zulu not supported
>>> datetime.strptime("2008-09-03T20:56:35.450686Z", "%Y-%m-%dT%H:%M:%S.%f%z")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/_strptime.py", line 565, in _strptime_datetime
tt, fraction = _strptime(data_string, format)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/_strptime.py", line 362, in _strptime
(data_string, format))
ValueError: time data '2008-09-03T20:56:35.450686Z' does not match format '%Y-%m-%dT%H:%M:%S.%f%z'
Let's use dateutil.parser.isoparse, as recommended in python docs.
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.
Updated. Using parse
, not isoparse
. Let me know if isoparse
is better.
ec3b238
to
70f77d8
Compare
@@ -337,7 +338,7 @@ def main(argv): | |||
compute = gcp.compute.ComputeV1(gcp.api.GcpApiManager(), project) | |||
leakedHealthChecks = [] | |||
for item in compute.list_health_check()['items']: | |||
if datetime.datetime.fromisoformat( | |||
if dateutil.parser.parse( |
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.
if dateutil.parser.parse( | |
if dateutil.parser.isoparse( |
@@ -2,6 +2,7 @@ Mako~=1.1 | |||
PyYAML~=5.3 | |||
absl-py~=0.11 | |||
dataclasses~=0.8; python_version < '3.7' | |||
dateutil~=2.8.2 |
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.
dateutil~=2.8.2 | |
dateutil~=2.8 |
It's not exactly semver. To allow patch-level updates, the last component needs to be omitted. See https://www.python.org/dev/peps/pep-0440/#compatible-release
~= 2.2
>= 2.2, == 2.*
~= 1.4.5
>= 1.4.5, == 1.4.*
70f77d8
to
1569932
Compare
1569932
to
0658d6a
Compare
All fixed. |
I added a commit to make keep_period configurable. The leaked resources on staging were less than 7 days old. |
21f13d3
to
04b614f
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.
LGTM to unblock the merge.
210b466
to
113ebff
Compare
datetime.fromisoformat
is available in >=python3.7, kokoro is running python3.6