Python pytz:将本地时间转换为utc。本地化似乎没有转化

时间:2022-09-18 21:44:37

I have a database that stores datetime as UTC. I need to look up info from a particular time, but the date and time are given in a local time, let's say 'Europe/Copenhagen'. I'm given these as:

我有一个将datetime存储为UTC的数据库。我需要查找特定时间的信息,但是日期和时间是在当地时间给出的,比如“欧洲/哥本哈根”。我考虑到这些:

year = 2012; month = 12; day = 2; hour = 13; min = 1;

So, I need to convert these to UTC so I can look them up in the database. I want to do this using pytz. I am looking at localize:

因此,我需要将它们转换为UTC以便在数据库中查找它们。我想用pytz。我关注的是本地化:

 local_tz = timezone('Europe/Copenhagen')
 t = local_tz.localize(datetime.datetime(year, month, day, hour, min))

But I'm confused about localize(). Is this assuming that year, etc, are given to me in local time? Or, is it assuming that they they are given in UTC and now it has converted them to local time?

但是我对本地化()感到困惑。这是假设那一年,等等,是在当地时间给我的吗?或者,它是否假设它们是在UTC中给出的,现在它已经将它们转换为本地时间?

print t gives me:

印刷t给我:

2012-12-02 13:01:00+01:00

So it seems that it assumed that the original year, etc was in utc; hours is now 13+1 instead of 13. So what should I do instead? I have read the pytz documentation and this does not make it clearer to me. It mentions a lot that things are tricky so I'm not sure whether pytz is actually solving these issues. And, I don't always know if the examples are showing me things that work or things that won't work.

所以它似乎假设原始年份是utc;小时现在是13+1而不是13。那么我该怎么做呢?我已经阅读了pytz文档,但这并不能使我更清楚地理解它。它提到很多事情都很棘手,所以我不确定pytz是否真的解决了这些问题。而且,我也不知道这些例子是给我看有用的还是没用的。

I tried normalize:

我试着规范化:

print local_tz.normalize(t)

That gives me the same result as print t.

结果和t一样。

EDIT: With the numbers given above for year etc. it should match up with information in the database for 2012-12-2 12:01. (since Copenhagen is utc+1 on that date)

编辑:上面给出的数字为年份等,应该与2012-12-2 12:01的数据库信息相匹配。(自哥本哈根会议以来,哥本哈根是utc+1)

2 个解决方案

#1


17  

localize() attaches the timezone to a naive datetime.datetime instance in the local timezone.

localize()将时区连接到一个简单的datetime上。本地时区中的datetime实例。

If you have datetime values in a local timezone, localize to that timezone, then use .astimezone() to cast the value to UTC:

如果在本地时区中有datetime值,请本地化到该时区,然后使用.astimezone()将值转换为UTC:

>>> localdt = local_tz.localize(datetime.datetime(year, month, day, hour, min))
>>> localdt.astimezone(pytz.UTC)
datetime.datetime(2012, 12, 2, 12, 1, tzinfo=<UTC>)

Note that you don't need to do this, datetime objects with a timezone can be compared; they'll both be normalized to UTC for the test:

注意,您不需要这样做,可以比较带有时区的datetime对象;它们都将被规范化为UTC进行测试:

>>> localdt.astimezone(pytz.UTC) == localdt
True

#2


0  

If you know the incoming time representation is in the Europe/Copenhagen timezone, you can create it as timezone-aware to begin with:

如果您知道即将到来的时间表示在欧洲/哥本哈根时区,您可以将其创建为timezone-aware to begin:

local_tz = timezone('Europe/Copenhagen')
t = local_tz.localize(datetime.datetime(year, month, day, hour, min))

You can then "convert" this to UTC with:

然后你可以将这个“转换”到UTC:

t_utc = t.astimezone(pytz.UTC)

but this might not be necessary, depending on how sane your database drivers are. t and t_utc represent the same point-in-time and well-behaving code should treat them interchangeably. The (year, month, day, hour, minute, second, …) tuple is merely a human-readable representation of this point-in-time in a specific time zone and calendar system.

但这可能不是必需的,这取决于您的数据库驱动程序是否正常。t和t_utc表示相同的时间点,行为良好的代码应该可以互换使用它们。元组(年、月、日、小时、分、秒、……)仅仅是在特定时区和日历系统中对这个时间点的可读表示。

#1


17  

localize() attaches the timezone to a naive datetime.datetime instance in the local timezone.

localize()将时区连接到一个简单的datetime上。本地时区中的datetime实例。

If you have datetime values in a local timezone, localize to that timezone, then use .astimezone() to cast the value to UTC:

如果在本地时区中有datetime值,请本地化到该时区,然后使用.astimezone()将值转换为UTC:

>>> localdt = local_tz.localize(datetime.datetime(year, month, day, hour, min))
>>> localdt.astimezone(pytz.UTC)
datetime.datetime(2012, 12, 2, 12, 1, tzinfo=<UTC>)

Note that you don't need to do this, datetime objects with a timezone can be compared; they'll both be normalized to UTC for the test:

注意,您不需要这样做,可以比较带有时区的datetime对象;它们都将被规范化为UTC进行测试:

>>> localdt.astimezone(pytz.UTC) == localdt
True

#2


0  

If you know the incoming time representation is in the Europe/Copenhagen timezone, you can create it as timezone-aware to begin with:

如果您知道即将到来的时间表示在欧洲/哥本哈根时区,您可以将其创建为timezone-aware to begin:

local_tz = timezone('Europe/Copenhagen')
t = local_tz.localize(datetime.datetime(year, month, day, hour, min))

You can then "convert" this to UTC with:

然后你可以将这个“转换”到UTC:

t_utc = t.astimezone(pytz.UTC)

but this might not be necessary, depending on how sane your database drivers are. t and t_utc represent the same point-in-time and well-behaving code should treat them interchangeably. The (year, month, day, hour, minute, second, …) tuple is merely a human-readable representation of this point-in-time in a specific time zone and calendar system.

但这可能不是必需的,这取决于您的数据库驱动程序是否正常。t和t_utc表示相同的时间点,行为良好的代码应该可以互换使用它们。元组(年、月、日、小时、分、秒、……)仅仅是在特定时区和日历系统中对这个时间点的可读表示。