Python strptime() and timezones?

后端 未结 5 1152
栀梦
栀梦 2020-11-22 11:58

I have a CSV dumpfile from a Blackberry IPD backup, created using IPDDump. The date/time strings in here look something like this (where EST is an Australian ti

5条回答
  •  无人及你
    2020-11-22 12:40

    The datetime module documentation says:

    Return a datetime corresponding to date_string, parsed according to format. This is equivalent to datetime(*(time.strptime(date_string, format)[0:6])).

    See that [0:6]? That gets you (year, month, day, hour, minute, second). Nothing else. No mention of timezones.

    Interestingly, [Win XP SP2, Python 2.6, 2.7] passing your example to time.strptime doesn't work but if you strip off the " %Z" and the " EST" it does work. Also using "UTC" or "GMT" instead of "EST" works. "PST" and "MEZ" don't work. Puzzling.

    It's worth noting this has been updated as of version 3.2 and the same documentation now also states the following:

    When the %z directive is provided to the strptime() method, an aware datetime object will be produced. The tzinfo of the result will be set to a timezone instance.

    Note that this doesn't work with %Z, so the case is important. See the following example:

    In [1]: from datetime import datetime
    
    In [2]: start_time = datetime.strptime('2018-04-18-17-04-30-AEST','%Y-%m-%d-%H-%M-%S-%Z')
    
    In [3]: print("TZ NAME: {tz}".format(tz=start_time.tzname()))
    TZ NAME: None
    
    In [4]: start_time = datetime.strptime('2018-04-18-17-04-30-+1000','%Y-%m-%d-%H-%M-%S-%z')
    
    In [5]: print("TZ NAME: {tz}".format(tz=start_time.tzname()))
    TZ NAME: UTC+10:00
    

提交回复
热议问题