Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use dateutil for managing ISO8601 (#5232)
- Loading branch information
Showing
4 changed files
with
15 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,3 +14,4 @@ astroid>=1.6.5 | |
deprecation>=2.0, <2.1 | ||
tqdm>=4.28.1, <5 | ||
Jinja2>=2.3, <3 | ||
python-dateutil>=2.6.0, <3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,15 @@ | ||
import datetime | ||
import re | ||
from dateutil import parser | ||
|
||
|
||
def from_timestamp_to_iso8601(timestamp): | ||
return "%sZ" % datetime.datetime.utcfromtimestamp(int(timestamp)).isoformat() | ||
|
||
|
||
def from_iso8601_to_datetime(iso_str): | ||
|
||
def transform_to_z_notation(the_str): | ||
for p in ("+00:00", "+0000", "+00"): | ||
if the_str.endswith(p): | ||
the_str = the_str[0:-len(p)] | ||
return "{}Z".format(the_str) | ||
return the_str | ||
|
||
base_pattern = "%Y-%m-%dT%H:%M:%S" | ||
if "." in iso_str: | ||
pattern = "{}.%fZ".format(base_pattern) | ||
else: | ||
pattern = '{}Z'.format(base_pattern) | ||
|
||
iso_str = transform_to_z_notation(iso_str) | ||
if not iso_str.endswith("Z"): | ||
iso_str += "Z" | ||
return datetime.datetime.strptime(iso_str, pattern) | ||
return parser.isoparse(iso_str) | ||
|
||
|
||
def iso8601_to_str(iso_str): | ||
dt = from_iso8601_to_datetime(iso_str) | ||
return dt.strftime('%Y-%m-%d %H:%M:%S UTC') | ||
|
||
|
||
def valid_iso8601(the_date): | ||
regex = r'^(-?(?:[1-9][0-9]*)?[0-9]{4})-(1[0-2]|0[1-9])-(3[01]|0[1-9]|[12][0-9])T' \ | ||
r'(2[0-3]|[01][0-9]):([0-5][0-9]):([0-5][0-9])(\.[0-9]+)?(Z|[+-]' \ | ||
r'(?:2[0-3]|[01][0-9]):' \ | ||
r'[0-5][0-9])?$' | ||
return re.match(regex, the_date) |