[英]How do I find the difference in hours between two time zones?
I have this code here that can find the time in two different timezones at the moment.我这里有这段代码,可以在此时找到两个不同时区的时间。 I want to calculate the difference in hours, between the two timezones.我想计算两个时区之间的小时差。
def time_caluclator(timezone1, timezone2):
dt_utcnow = datetime.datetime.now(tz=pytz.UTC)
dt_1 = dt_utcnow.astimezone(pytz.timezone(timezone1))
dt_2 = dt_utcnow.astimezone(pytz.timezone(timezone2))
print(dt_1, dt_2)
This is the code, and it will print this:这是代码,它将打印:
2022-05-15 00:44:22.031149+00:00 2022-05-15 01:44:22.031149+01:00
(First timezone is Zulu, and the other is WET). (第一个时区是 Zulu,另一个是 WET)。
I would do two nested for loops to separate your times into sections by both spaces and ":" so that you can get the individual numbers.我会做两个嵌套的for循环,用空格和“:”将你的时间分成几个部分,这样你就可以获得单独的数字。 Then its just subtraction and maybe a dictionary if you would like to say specifically timezone codes like "EST"然后它只是减法,如果你想特别说像“EST”这样的时区代码,也许是一本字典
def time_caluclator(timezone1, timezone2):
off1 = pytz.timezone(timezone1).utcoffset(datetime.datetime.now())
off2 = pytz.timezone(timezone2).utcoffset(datetime.datetime.now())
return (off2 - off1).seconds // 3600
Works correct for pytz timezones such as "US/Eastern", "Europe/Moscow".适用于“美国/东部”、“欧洲/莫斯科”等 pytz 时区。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.