How to set UTC offset for datetime?

后端 未结 3 1542
遇见更好的自我
遇见更好的自我 2021-02-07 05:08

My Python-based web server needs to perform some date manipulation using the client\'s timezone, represented by its UTC offset. How do I construct a datetime object with the spe

3条回答
  •  野趣味
    野趣味 (楼主)
    2021-02-07 05:49

    Using dateutil:

    >>> import datetime
    >>> import dateutil.tz
    >>> datetime.datetime(2013, 9, 11, 0, 17, tzinfo=dateutil.tz.tzoffset(None, 9*60*60))
    datetime.datetime(2013, 9, 11, 0, 17, tzinfo=tzoffset(None, 32400))
    >>> datetime.datetime(2013, 9, 11, 0, 17, tzinfo=dateutil.tz.tzoffset('KST', 9*60*60))
    datetime.datetime(2013, 9, 11, 0, 17, tzinfo=tzoffset('KST', 32400))
    

    >>> dateutil.parser.parse('2013/09/11 00:17 +0900')
    datetime.datetime(2013, 9, 11, 0, 17, tzinfo=tzoffset(None, 32400))
    

提交回复
热议问题