英文:
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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论