Python:如何将日期时间/时间戳从一个时区转换为另一个时区? [英] Python: How do you convert a datetime/timestamp from one timezone to another timezone?
问题描述
具体来说,给定服务器的时区(系统时间角度)和时区输入,我如何计算系统时间,就像它在那个新时区中一样(不管夏令时如何)?
导入日期时间
current_time = datetime.datetime.now()#系统时间
server_timezone =美国/东部
new_timezone =美国/太平洋地区
current_time_in_new_timezone = ???
如果您知道原始时区和您想将其转换为它,结果非常简单:
-
制作一个
pytz.timezone
当前时区和新时区的对象,例如pytz.timezone(美国/太平洋)
。如果您不知道时区的正式名称,只需调用pytz.all_timezones
-
使用当前时区的pytz对象以及您的datetime / timestamp作为输入,调用
.localize()
以将其本地化为当前时区。例如current_timezone.localize(timestamp)
-
最后,致电
.astimezone()
在步骤2中新定位的日期时间/时间戳上,并以所需的新时区的pytz对象作为输入,例如localized_timestamp.astimezone(new_timezone)
。
完成! / p>
完整示例:
导入日期时间
导入pytz
#一个时间戳,我想转换
my_timestamp = datetime.datetime.now()
#创建两个时区对象
old_timezone = pytz.timezone( US /东部)
new_timezone = pytz.timezone(美国/太平洋)
#两步处理
localized_timestamp = old_timezone.localize(my_timestamp)
new_timezone_timestamp = localized_timestamp.astimezone(new_timezone)
#或作为单线
new_timezone_timestamp = old_timezone.localize(my_timestamp).astimezone(new_timezone)
奖金:但是,如果您需要的只是特定时区的当前时间,则可以方便地将该时区直接传递到datetime.now()以获取当前时间直接:
datetime.datetime.now(new_timezone)
一般而言,在需要进行时区转换时,我强烈建议您应将所有时间戳存储在UTC的数据库中。没有夏令时(DST)过渡。作为一种好习惯,应该始终选择启用时区支持(即使您的用户都在一个时区中!)。这将帮助您避免困扰着当今众多软件的DST过渡问题。
除了DST之外,软件时间通常还很棘手。为了大致了解在软件中处理时间有多困难,这里提供了一个潜在的启发性资源: http:// yourcalendricalfallacyis.com
即使将日期时间/时间戳转换为日期也看似简单,但操作似乎并不明显。正如此有用的文档指出的那样:
日期时间表示时间点。绝对是绝对的:它不依赖任何内容。相反,日期是一个日历概念。这是一个时间段,其范围取决于考虑日期的时区。如您所见,这两个概念在根本上是不同的。
了解这一区别是避免基于时间的错误的关键一步。祝你好运。
Specifically, given the timezone of my server (system time perspective) and a timezone input, how do I calculate the system time as if it were in that new timezone (regardless of daylight savings, etc)?
import datetime
current_time = datetime.datetime.now() #system time
server_timezone = "US/Eastern"
new_timezone = "US/Pacific"
current_time_in_new_timezone = ???
If you know your origin timezone and the new timezone that you want to convert it to, it turns out to be very straightforward:
Make a
pytz.timezone
object for both the current timezone and the new timezone e.g.pytz.timezone("US/Pacific")
. If you don't know a timezone's official name, you can find a list of all official timezones by simply callingpytz.all_timezones
Call
.localize()
using the current timezone's pytz object with your datetime/timestamp as input to localize it to the current timezone. e.g.current_timezone.localize(timestamp)
Finally, call
.astimezone()
on the newly localized datetime/timestamp from step 2 with the desired new timezone's pytz object as input e.g.localized_timestamp.astimezone(new_timezone)
.
Done!
As a full example:
import datetime
import pytz
# a timestamp I'd like to convert
my_timestamp = datetime.datetime.now()
# create both timezone objects
old_timezone = pytz.timezone("US/Eastern")
new_timezone = pytz.timezone("US/Pacific")
# two-step process
localized_timestamp = old_timezone.localize(my_timestamp)
new_timezone_timestamp = localized_timestamp.astimezone(new_timezone)
# or alternatively, as an one-liner
new_timezone_timestamp = old_timezone.localize(my_timestamp).astimezone(new_timezone)
Bonus: but if all you need is the current time in a specific timezone, you can conveniently pass that timezone directly into datetime.now() to get the current times directly:
datetime.datetime.now(new_timezone)
When it comes to needing timezones conversions generally, I would strongly advise that one should store all timestamps in your database in UTC, which has no daylight savings time (DST) transition. And as a good practice, one should always choose to enable time zone support (even if your users are all in a single time zone!). This will help you avoid the DST transition problem that plagues so much software today.
Beyond DST, time in software can be generally quite tricky. To get a sense of just how difficult it is to deal with time in software in general, here is a potentially enlightening resource: http://yourcalendricalfallacyis.com
Even a seemingly simple operation as converting a datetime/timestamp into a date can become non-obvious. As this helpful documentation points out:
A datetime represents a point in time. It’s absolute: it doesn’t depend on anything. On the contrary, a date is a calendaring concept. It’s a period of time whose bounds depend on the time zone in which the date is considered. As you can see, these two concepts are fundamentally different.
Understanding this difference is a key step towards avoiding time-based bugs. Good luck.
这篇关于Python:如何将日期时间/时间戳从一个时区转换为另一个时区?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!