Is there an easy way to convert an RFC 3339 time into a regular Python timestamp?
I\'ve got a script which is reading an ATOM feed and I\'d like to be able to compar
Using Python 3, you can use RegEx to break the RFC 3339 timestamp into its components. Then, directly create the datetime object, no additional modules needed:
import re
import datetime
def parse_rfc3339(dt):
broken = re.search(r'([0-9]{4})-([0-9]{2})-([0-9]{2})T([0-9]{2}):([0-9]{2}):([0-9]{2})(\.([0-9]+))?(Z|([+-][0-9]{2}):([0-9]{2}))', dt)
return(datetime.datetime(
year = int(broken.group(1)),
month = int(broken.group(2)),
day = int(broken.group(3)),
hour = int(broken.group(4)),
minute = int(broken.group(5)),
second = int(broken.group(6)),
microsecond = int(broken.group(8) or "0"),
tzinfo = datetime.timezone(datetime.timedelta(
hours = int(broken.group(10) or "0"),
minutes = int(broken.group(11) or "0")))))
This example theads missing timezones or microseconds as "0" but might need additional error checking. Cheers, Alex
If you're using Django, you could use Django's function parse_datetime:
>>> from django.utils.dateparse import parse_datetime
>>> parse_datetime("2016-07-19T07:30:36+05:00")
datetime.datetime(2016, 7, 19, 7, 30, 36, tzinfo=<django.utils.timezone.FixedOffset object at 0x101c0c1d0>)
The simplest solution for me has been dateutil python standart library.
from dateutil.parser import parse
dt = "2020-11-23T11:08:23.022277705Z"
print(parse(dt))
Output:
2020-11-23 11:08:23.022277+00:00
If you don't need the timezone element, just simply set timezone info to None
print(parse(t).replace(tzinfo=None))
The output is a nice and clean datetime object:
2020-11-23 11:08:23.022277
No builtin, afaik.
feed.date.rfc3339 This is a Python library module with functions for converting timestamp strings in RFC 3339 format to Python time float values, and vice versa. RFC 3339 is the timestamp format used by the Atom feed syndication format.
It is BSD-licensed.
http://home.blarg.net/~steveha/pyfeed.html
(Edited so it's clear I didn't write it. :-)
try this, it works fine for me
datetime_obj = datetime.strptime("2014-01-01T00:00:00Z", '%Y-%m-%dT%H:%M:%SZ')
or
datetime_obj = datetime.strptime("Mon, 01 Jun 2015 16:41:40 GMT", '%a, %d %b %Y %H:%M:%S GMT')
The new datetime.fromisoformat(date_string) method which was added in Python 3.7 will parse most RFC 3339 timestamps, including those with time zone offsets. It's not a full implementation, so be sure to test your use case.
>>> from datetime import datetime
>>> datetime.fromisoformat('2011-11-04')
datetime.datetime(2011, 11, 4, 0, 0)
>>> datetime.fromisoformat('2011-11-04T00:05:23')
datetime.datetime(2011, 11, 4, 0, 5, 23)
>>> datetime.fromisoformat('2011-11-04 00:05:23.283')
datetime.datetime(2011, 11, 4, 0, 5, 23, 283000)
>>> datetime.fromisoformat('2011-11-04 00:05:23.283+00:00')
datetime.datetime(2011, 11, 4, 0, 5, 23, 283000, tzinfo=datetime.timezone.utc)
>>> datetime.fromisoformat('2011-11-04T00:05:23+04:00')
datetime.datetime(2011, 11, 4, 0, 5, 23,
tzinfo=datetime.timezone(datetime.timedelta(seconds=14400)))