Python 将具有特定时区的时间戳转换为 UTC 日期时间
Python convert timestamps with specific timezone to datetime in UTC
我正在尝试将具有特定时区 (Europe/Paris) 的时间戳转换为 UTC 日期时间格式。
在我的笔记本电脑上,它适用于下面的解决方案,但是当我在远程服务器(爱尔兰的 AWS-Lambda 函数)中执行我的代码时,我需要轮班 1 小时,因为服务器的本地时区与我的不同。
我怎样才能拥有可以在我的笔记本电脑上工作并同时在远程服务器上工作的代码(动态处理本地时区)?
import pytz
import datetime
def convert_timestamp_in_datetime_utc(timestamp_received):
utc = pytz.timezone('UTC')
now_in_utc = datetime.datetime.utcnow().replace(tzinfo=utc).astimezone(pytz.UTC)
fr = pytz.timezone('Europe/Paris')
new_date = datetime.datetime.fromtimestamp(timestamp_received)
return fr.localize(new_date, is_dst=None).astimezone(pytz.UTC)
谢谢
我不确定 timestamp_received
是什么,但我想你想要的是 utcfromtimestamp()
import pytz
from datetime import datetime
def convert_timestamp_in_datetime_utc(timestamp_received):
dt_naive_utc = datetime.utcfromtimestamp(timestamp_received)
return dt_naive_utc.replace(tzinfo=pytz.utc)
为了完整起见,这里有另一种方法可以通过引用 python-dateutil
的 tzlocal
时区来完成同样的事情:
from dateutil import tz
from datetime import datetime
def convert_timestamp_in_datetime_utc(timestamp_received):
dt_local = datetime.fromtimestamp(timestamp_received, tz.tzlocal())
if tz.datetime_ambiguous(dt_local):
raise AmbiguousTimeError
if tz.datetime_imaginary(dt_local):
raise ImaginaryTimeError
return dt_local.astimezone(tz.tzutc())
class AmbiguousTimeError(ValueError):
pass
class ImaginaryTimeError(ValueError):
pass
(我在 AmbiguousTimeError
和 ImaginaryTimeError
条件中添加以模仿 pytz
界面。)请注意,我将其包括在内以防万一您遇到类似的问题需要出于某种原因参考本地时区 - 如果您有一些东西可以在 UTC 中为您提供正确的答案,最好使用它然后使用 astimezone
将其放入您想要的任何本地时区.
工作原理
既然你在评论中表示你对它的工作原理仍然有点困惑,我想我会澄清为什么它有效。有两个函数将时间戳转换为datetime.datetime
对象,datetime.datetime.fromtimestamp(timestamp, tz=None)
and datetime.datetime.utcfromtimestamp(timestamp)
:
utcfromtimestamp(timestamp)
会给你一个 naive datetime
表示 UTC 时间。然后,您可以执行 dt.replace(tzinfo=pytz.utc)
(或任何其他 utc
实现 - datetime.timezone.utc
、dateutil.tz.tzutc()
等)以了解日期时间并将其转换为您想要的任何时区。
fromtimestamp(timestamp, tz=None)
,当 tz
不是 None
时,会给你一个 aware datetime
相当于utcfromtimestamp(timestamp).replace(tzinfo=timezone.utc).astimezone(tz)
。如果tz
是None
,不是也转换指定的时区,而是转换成你的本地时间(相当于dateutil.tz.tzlocal()
),然后returns一个天真 datetime
.
从 Python 3.6 开始,您可以在 naive 日期时间上使用 datetime.datetime.astimezone(tz=None)
,时区将假定为系统本地时间。因此,如果您正在开发 Python >= 3.6 应用程序或库,您可以使用 datetime.fromtimestamp(timestamp).astimezone(whatever_timezone)
或 datetime.utcfromtimestamp(timestamp).replace(tzinfo=timezone.utc).astimezone(whatever_timezone)
作为等效项。
我正在尝试将具有特定时区 (Europe/Paris) 的时间戳转换为 UTC 日期时间格式。 在我的笔记本电脑上,它适用于下面的解决方案,但是当我在远程服务器(爱尔兰的 AWS-Lambda 函数)中执行我的代码时,我需要轮班 1 小时,因为服务器的本地时区与我的不同。 我怎样才能拥有可以在我的笔记本电脑上工作并同时在远程服务器上工作的代码(动态处理本地时区)?
import pytz
import datetime
def convert_timestamp_in_datetime_utc(timestamp_received):
utc = pytz.timezone('UTC')
now_in_utc = datetime.datetime.utcnow().replace(tzinfo=utc).astimezone(pytz.UTC)
fr = pytz.timezone('Europe/Paris')
new_date = datetime.datetime.fromtimestamp(timestamp_received)
return fr.localize(new_date, is_dst=None).astimezone(pytz.UTC)
谢谢
我不确定 timestamp_received
是什么,但我想你想要的是 utcfromtimestamp()
import pytz
from datetime import datetime
def convert_timestamp_in_datetime_utc(timestamp_received):
dt_naive_utc = datetime.utcfromtimestamp(timestamp_received)
return dt_naive_utc.replace(tzinfo=pytz.utc)
为了完整起见,这里有另一种方法可以通过引用 python-dateutil
的 tzlocal
时区来完成同样的事情:
from dateutil import tz
from datetime import datetime
def convert_timestamp_in_datetime_utc(timestamp_received):
dt_local = datetime.fromtimestamp(timestamp_received, tz.tzlocal())
if tz.datetime_ambiguous(dt_local):
raise AmbiguousTimeError
if tz.datetime_imaginary(dt_local):
raise ImaginaryTimeError
return dt_local.astimezone(tz.tzutc())
class AmbiguousTimeError(ValueError):
pass
class ImaginaryTimeError(ValueError):
pass
(我在 AmbiguousTimeError
和 ImaginaryTimeError
条件中添加以模仿 pytz
界面。)请注意,我将其包括在内以防万一您遇到类似的问题需要出于某种原因参考本地时区 - 如果您有一些东西可以在 UTC 中为您提供正确的答案,最好使用它然后使用 astimezone
将其放入您想要的任何本地时区.
工作原理
既然你在评论中表示你对它的工作原理仍然有点困惑,我想我会澄清为什么它有效。有两个函数将时间戳转换为datetime.datetime
对象,datetime.datetime.fromtimestamp(timestamp, tz=None)
and datetime.datetime.utcfromtimestamp(timestamp)
:
utcfromtimestamp(timestamp)
会给你一个 naivedatetime
表示 UTC 时间。然后,您可以执行dt.replace(tzinfo=pytz.utc)
(或任何其他utc
实现 -datetime.timezone.utc
、dateutil.tz.tzutc()
等)以了解日期时间并将其转换为您想要的任何时区。fromtimestamp(timestamp, tz=None)
,当tz
不是None
时,会给你一个 awaredatetime
相当于utcfromtimestamp(timestamp).replace(tzinfo=timezone.utc).astimezone(tz)
。如果tz
是None
,不是也转换指定的时区,而是转换成你的本地时间(相当于dateutil.tz.tzlocal()
),然后returns一个天真datetime
.
从 Python 3.6 开始,您可以在 naive 日期时间上使用 datetime.datetime.astimezone(tz=None)
,时区将假定为系统本地时间。因此,如果您正在开发 Python >= 3.6 应用程序或库,您可以使用 datetime.fromtimestamp(timestamp).astimezone(whatever_timezone)
或 datetime.utcfromtimestamp(timestamp).replace(tzinfo=timezone.utc).astimezone(whatever_timezone)
作为等效项。