如何使用实际本地时间而不是+02:00标记。

huangapple go评论67阅读模式
英文:

How to use actual local time instead of +02:00 notation

问题

服务器日志延迟了两个小时,原因不明。我试图将其调整为本地时间的2小时。目前,我的脚本输出格式如下:

2023-06-07 15:10:50+02:00

实际上,我需要它是这样的:

2023-06-07 17:10:50

以下是脚本:

import datetime
import pytz

vcenter_timestamp = '2023-06-07T15:10:50.065397578Z'[:19]
rome_tz = pytz.timezone('Europe/Rome')
pattern = '%Y-%m-%dT%H:%M:%S'
# time in UTC
dt = datetime.datetime.strptime(vcenter_timestamp, pattern)
# convert to Rome time with daylight savings
dt_rome = dt.astimezone(rome_tz)
print(dt_rome)

当前输出:

2023-06-07 15:10:50+02:00

期望输出:

2023-06-07 17:10:50
英文:

The server logs are two hours behind for whatever reason. I'm trying to at 2 hours because of the local time. At the moment, my script outputs in the following format:

2023-06-07 15:10:50+02:00

I actually need it to be:

2023-06-07 17:10:50

Here is the script:

import datetime
import pytz

vcenter_timestamp = '2023-06-07T15:10:50.065397578Z'[:19]
rome_tz = pytz.timezone('Europe/Rome')
pattern = '%Y-%m-%dT%H:%M:%S'
# time in UTC
dt = datetime.datetime.strptime(vcenter_timestamp, pattern)
# convert to Rome time with daylight savings
dt_rome = dt.astimezone(rome_tz)
print(dt_rome)

Current output:

2023-06-07 15:10:50+02:00

Expected output:

2023-06-07 17:10:50

答案1

得分: 1

你可以尝试以下:

英文:

You could try the following:

dt = datetime.datetime.strptime(vcenter_timestamp, pattern)
# convert to Rome time without considering daylight savings
dt_rome = dt.replace(tzinfo=pytz.UTC).astimezone(rome_tz)

# Remove time zone offset from the output
dt_rome = dt_rome.replace(tzinfo=None)

print(dt_rome)

huangapple
  • 本文由 发表于 2023年6月8日 01:15:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76425669.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定