添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
考研的台灯  ·  Redis instance for ...·  4 天前    · 
爱看书的签字笔  ·  常用 Django QuerySet ...·  4 天前    · 
犯傻的毛衣  ·  Django admin ...·  4 天前    · 
安静的海豚  ·  mysql启动时Error: 22 ...·  3 周前    · 
飘逸的小熊猫  ·  golang日志库·  1 月前    · 
聪明伶俐的双杠  ·  rlimit - Rust·  9 月前    · 

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.

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.