Python: strftime() UTC Offset Not working as Expected in Windows

后端 未结 4 1359
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-20 11:44

Every time I use:

time.strftime(\"%z\")

I get:

Eastern Daylight Time

However, I would like the UTC offset

4条回答
  •  萌比男神i
    2020-12-20 12:28

    It will come as no surprise that this bug persists in, what is the latest Windows version available currently, Win 10 Version 1703 (Creators). However, time marches on and there is a lovely date-and-time library called pendulum that does what the question asks for. Sébastien Eustace (principal author of the product?) has shown me this.

    >>> pendulum.now().strftime('%z')
    '-0400'
    

    pendulum assumes UTC/GMT unless told otherwise, and keeps timezone with the date-time object. There are many other possibilities, amongst them these:

    >>> pendulum.now(tz='Europe/Paris').strftime('%z')
    '+0200'
    >>> pendulum.create(year=2016, month=11, day=5, hour=16, minute=23, tz='America/Winnipeg').strftime('%z')
    '-0500'
    >>> pendulum.now(tz='America/Winnipeg').strftime('%z')
    '-0500'
    

提交回复
热议问题