如何设置datetime的UTC偏移量? [英] How to set UTC offset for datetime?

查看:262
本文介绍了如何设置datetime的UTC偏移量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的基于Python的Web服务器需要使用客户端的时区执行一些日期操作,以UTC偏移量表示。如何使用指定的UTC偏移量作为时区构造一个datetime对象?

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 specified UTC offset as timezone?

推荐答案

使用 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))

这篇关于如何设置datetime的UTC偏移量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆