如何从日期时间对象中删除 pytz 时区? [英] How can I remove a pytz timezone from a datetime object?

查看:30
本文介绍了如何从日期时间对象中删除 pytz 时区?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种简单的方法可以从 pytz 日期时间对象中删除时区?
例如在这个例子中从 dt_tz 重构 dt:

<预><代码>>>>导入日期时间>>>进口pytz>>>dt = datetime.datetime.now()>>>dtdatetime.datetime(2012, 6, 8, 9, 27, 32, 601000)>>>dt_tz = pytz.utc.localize(dt)>>>dt_tzdatetime.datetime(2012, 6, 8, 9, 27, 32, 601000, tzinfo=<UTC>)

解决方案

从日期时间对象中删除时区 (tzinfo):

# dt_tz 是一个 datetime.datetime 对象dt = dt_tz.replace(tzinfo=None)

如果您使用的是像 arrow 这样的库,那么您可以通过简单地将箭头对象转换为到 datetime 对象,然后执行与上面示例相同的操作.

# arrowObj = arrow.get('2014-10-09T10:56:09.347444-07:00')# datetime.datetime(2014, 10, 9, 10, 56, 9, 347444, tzinfo=tzoffset(None, -25200))tmpDatetime = arrowObj.datetime# datetime.datetime(2014, 10, 9, 10, 56, 9, 347444)tmpDatetime = tmpDatetime.replace(tzinfo=None)

你为什么要这样做?一个例子是 mysql 不支持其 DATETIME 类型的时区.因此,当您给它一个 datetime.datetime 对象以插入到数据库中时,使用 ORM 就像 sqlalchemy 将简单地删除时区.解决方案是将您的 datetime.datetime 对象转换为 UTC(因此您的数据库中的所有内容都是 UTC,因为它无法指定时区)然后将其插入到数据库中(无论如何都会删除时区)或者自己删除.另请注意,您不能比较 datetime.datetime 对象,其中一个是时区感知,另一个是时区天真.

########################################################################## MySQL 示例!MySQL 不支持 DATETIME 类型的时区!##########################################################################arrowObj = arrow.get('2014-10-09T10:56:09.347444-07:00')arrowDt = arrowObj.to("utc").datetime# 插入 datetime.datetime(2014, 10, 9, 17, 56, 9, 347444, tzinfo=tzutc())insertIntoMysqlDatabase(arrowDt)# 返回 datetime.datetime(2014, 10, 9, 17, 56, 9, 347444)dbDatetimeNoTz = getFromMysqlDatabase()# 无法比较时区感知和时区幼稚dbDatetimeNoTz == arrowDt # False,或 3.3 之前的 Python 版本上的 TypeError# 比较两者都知道的日期时间或两者都是天真的工作但是dbDatetimeNoTz == arrowDt.replace(tzinfo=None) # True

Is there a simple way to remove the timezone from a pytz datetime object?
e.g. reconstructing dt from dt_tz in this example:

>>> import datetime
>>> import pytz
>>> dt = datetime.datetime.now()
>>> dt
datetime.datetime(2012, 6, 8, 9, 27, 32, 601000)
>>> dt_tz = pytz.utc.localize(dt)
>>> dt_tz
datetime.datetime(2012, 6, 8, 9, 27, 32, 601000, tzinfo=<UTC>)

解决方案

To remove a timezone (tzinfo) from a datetime object:

# dt_tz is a datetime.datetime object
dt = dt_tz.replace(tzinfo=None)

If you are using a library like arrow, then you can remove timezone by simply converting an arrow object to to a datetime object, then doing the same thing as the example above.

# <Arrow [2014-10-09T10:56:09.347444-07:00]>
arrowObj = arrow.get('2014-10-09T10:56:09.347444-07:00')

# datetime.datetime(2014, 10, 9, 10, 56, 9, 347444, tzinfo=tzoffset(None, -25200))
tmpDatetime = arrowObj.datetime

# datetime.datetime(2014, 10, 9, 10, 56, 9, 347444)
tmpDatetime = tmpDatetime.replace(tzinfo=None)

Why would you do this? One example is that mysql does not support timezones with its DATETIME type. So using ORM's like sqlalchemy will simply remove the timezone when you give it a datetime.datetime object to insert into the database. The solution is to convert your datetime.datetime object to UTC (so everything in your database is UTC since it can't specify timezone) then either insert it into the database (where the timezone is removed anyway) or remove it yourself. Also note that you cannot compare datetime.datetime objects where one is timezone aware and another is timezone naive.

##############################################################################
# MySQL example! where MySQL doesn't support timezones with its DATETIME type!
##############################################################################

arrowObj = arrow.get('2014-10-09T10:56:09.347444-07:00')

arrowDt = arrowObj.to("utc").datetime

# inserts datetime.datetime(2014, 10, 9, 17, 56, 9, 347444, tzinfo=tzutc())
insertIntoMysqlDatabase(arrowDt)

# returns datetime.datetime(2014, 10, 9, 17, 56, 9, 347444)
dbDatetimeNoTz = getFromMysqlDatabase()

# cannot compare timzeone aware and timezone naive
dbDatetimeNoTz == arrowDt # False, or TypeError on python versions before 3.3

# compare datetimes that are both aware or both naive work however
dbDatetimeNoTz == arrowDt.replace(tzinfo=None) # True

这篇关于如何从日期时间对象中删除 pytz 时区?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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