如何抑制警告:没有 tzinfo 的日期时间将被视为 UTC?
How to supress warning: Datetime with no tzinfo will be considered UTC?
我有以下代码用于监视 Azure ADF 管道 运行s。该代码使用“RunFilterParameters”在提取 运行 结果时应用日期范围过滤器:
filter_params = RunFilterParameters(last_updated_after=datetime.now() - timedelta(1), last_updated_before=datetime.now() + timedelta(1))
query_response = adf_client.activity_runs.query_by_pipeline_run(resource_group, adf_name, row.latest_runid,filter_params)
上面的工作正常,但是它抛出了一个警告:
Datetime with no tzinfo will be considered UTC
不确定如何为此添加时区或只是抑制警告?
请帮忙
"no tzinfo" 表示使用 naive datetime,即没有定义时区的日期时间。由于 Python 默认情况下假定本地时间用于天真的日期时间,这可能会导致意外行为。
在问题给出的代码示例中,您可以创建一个感知日期时间对象(将 tz 指定为 UTC),例如
from datetime import datetime, timezone
# and use
datetime.now(timezone.utc)
如果您需要使用 UTC 以外的其他时区,请查看 zoneinfo(Python 3.9+ 标准库)。
我有以下代码用于监视 Azure ADF 管道 运行s。该代码使用“RunFilterParameters”在提取 运行 结果时应用日期范围过滤器:
filter_params = RunFilterParameters(last_updated_after=datetime.now() - timedelta(1), last_updated_before=datetime.now() + timedelta(1))
query_response = adf_client.activity_runs.query_by_pipeline_run(resource_group, adf_name, row.latest_runid,filter_params)
上面的工作正常,但是它抛出了一个警告:
Datetime with no tzinfo will be considered UTC
不确定如何为此添加时区或只是抑制警告? 请帮忙
"no tzinfo" 表示使用 naive datetime,即没有定义时区的日期时间。由于 Python 默认情况下假定本地时间用于天真的日期时间,这可能会导致意外行为。
在问题给出的代码示例中,您可以创建一个感知日期时间对象(将 tz 指定为 UTC),例如
from datetime import datetime, timezone
# and use
datetime.now(timezone.utc)
如果您需要使用 UTC 以外的其他时区,请查看 zoneinfo(Python 3.9+ 标准库)。