What possible values does datetime.strptime() accept for %Z?

前端 未结 2 682
臣服心动
臣服心动 2021-01-17 16:20

Python\'s datetime.strptime() is documented as supporting a timezone in the %Z field. So, for example:

In [1]: datetime.strptime(\'2009-08-19 14:20:36 UTC\',         


        
相关标签:
2条回答
  • 2021-01-17 16:36

    This is from the time module, but I'm almost certain it applies to datetime:

    Support for the %Z directive is based on the values contained in tzname and whether daylight is true. Because of this, it is platform-specific except for recognizing UTC and GMT which are always known (and are considered to be non-daylight savings timezones).

    https://docs.python.org/library/time.html

    On my system:

    >>> import time
    >>> time.tzname
    ('PST', 'PDT')
    

    Using anything but these in datetime.strptime results in an exception. So, look to see what you have available on your machine.

    0 讨论(0)
  • 2021-01-17 16:46

    I gather they are GMT, UTC, and whatever is listed in time.tzname.

    >>> for t in time.tzname:
    ...     print t
    ...
    Eastern Standard Time
    Eastern Daylight Time
    >>> datetime.strptime('2009-08-19 14:20:36 Eastern Standard Time', "%Y-%m-%d %H:%M:%S %Z")
    datetime.datetime(2009, 8, 19, 14, 20, 36)
    >>> datetime.strptime('2009-08-19 14:20:36 UTC', "%Y-%m-%d %H:%M:%S %Z")
    datetime.datetime(2009, 8, 19, 14, 20, 36)
    >>> datetime.strptime('2009-08-19 14:20:36 GMT', "%Y-%m-%d %H:%M:%S %Z")
    datetime.datetime(2009, 8, 19, 14, 20, 36)
    

    These settings are machine-specific, of course, and yours will be different in all likelihood.

    0 讨论(0)
提交回复
热议问题