Hi there, I’m receiving the datetime in string in this format ‘2020-09-18T21:39:14+0200’.
Is there some solution already integrated in Django which could parse this including the zone information ?
What’s great about Django is that it lets you use everything that Python has to offer.
>>> a_time = '2020-09-18T21:39:14+0200'
>>> datetime.datetime.strptime(a_time, '%Y-%m-%dT%H:%M:%S%z')
# datetime.datetime(2020, 9, 18, 21, 39, 14, tzinfo=datetime.timezone(datetime.timedelta(seconds=7200)))
You can read more about Python’s built-in datetime package, or specifically about dealing with strings that include timezone information.
There’s also the Django solution - see the django.utils.dateparse module.
Ken
Cool, I haven’t used Django’s own date/time functions for much other than timezone.now()
.
I had a look at django’s codebase to understand a bit about how django goes about this, so I’ll share if it’s interesting to either of you or someone else who comes by (as well as potentially helping future me):
# django/utils/dateparse.py
import datetime
import re
from django.utils.timezone import get_fixed_timezone
# ...
datetime_re = re.compile(
r'(?P<year>\d{4})-(?P<month>\d{1,2})-(?P<day>\d{1,2})'
r'[T ](?P<hour>\d{1,2}):(?P<minute>\d{1,2})'
r'(?::(?P<second>\d{1,2})(?:\.(?P<microsecond>\d{1,6})\d{0,6})?)?'
r'(?P<tzinfo>Z|[+-]\d{2}(?::?\d{2})?)?$'
)
# ...
def parse_datetime(value):
"""Parse a string and return a datetime.datetime.
This function supports time zone offsets. When the input contains one,
the output uses a timezone with a fixed offset from UTC.
Raise ValueError if the input is well formatted but not a valid datetime.
Return None if the input isn't well formatted.
"""
match = datetime_re.match(value)
if match:
kw = match.groupdict()
kw['microsecond'] = kw['microsecond'] and kw['microsecond'].ljust(6, '0')
tzinfo = kw.pop('tzinfo')
if tzinfo == 'Z':
tzinfo = utc
elif tzinfo is not None:
offset_mins = int(tzinfo[-2:]) if len(tzinfo) > 3 else 0
offset = 60 * int(tzinfo[1:3]) + offset_mins
if tzinfo[0] == '-':
offset = -offset
tzinfo = get_fixed_timezone(offset)
kw = {k: int(v) for k, v in kw.items() if v is not None}
kw['tzinfo'] = tzinfo
return datetime.datetime(**kw)
# django/utils/timezone.py
from datetime import datetime, timedelta, timezone, tzinfo
# ...
# function used by parse_datetime in snippet above
def get_fixed_timezone(offset):
"""Return a tzinfo instance with a fixed offset from UTC."""
if isinstance(offset, timedelta):
offset = offset.total_seconds() // 60
sign = '-' if offset < 0 else '+'
hhmm = '%02d%02d' % divmod(abs(offset), 60)
name = sign + hhmm
return timezone(timedelta(minutes=offset), name)
This is how you actually use the relevant function:
>>> from django.utils import dateparse
>>> a_time = '2020-09-18T21:39:14+0200'
>>> dateparse.parse_datetime(a_time)
# datetime.datetime(2020, 9, 18, 21, 39, 14, tzinfo=datetime.timezone(datetime.timedelta(seconds=7200), '+0200'))
Django does something similar to what I did above (datetime.datetime.strptime(a_time, '%Y-%m-%dT%H:%M:%S%z')
), only in a more general and clever way.
First, django looks for certain patterns (as described by the compiled regex in datetime_re
- similar to my %Y-%m-%dT%H:%M:%S%z'
) in the passed string. If the string fits the necessary patterns, django then splits the data into separate components (e. g. ‘hour’ and ‘tzinfo’), bundled in a dictionary. Then the data are “massaged” a bit, if possible/necessary, so that they will better fit with what the datetime package expects, and passes the dictionary’s information as arguments to datetime.datetime
, by using **
in the function call. In the process (see get_fixed_timezone
), django also adds a more reader-friendly ‘name’ for the timezone, which is why there’s the '+0200'
, which the result from the datetime.datetime.strptime
call in my first post didn’t include.
@KenWhitesell @datalowe
thank you both for great answers !!!