简体   繁体   English

如何找到两个时区之间的小时差?

[英]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.

相关问题 如何计算python中两个时区之间的时差? - How to compute the time difference between two time zones in python? 如何从不同的时区获取python daytime.time之间的正确差异? - How do I get the correct difference between python daytime.time from different time zones? 如何找到两个日期时间之间的差异,不包括一天中的时间间隔? - How do I find the difference between two datetimes excluding a time gap in the day? 如何使用Google日历api python找到两个事件之间的时差 - How do I find the time difference between two events using the Google calender api python 如何在python中找到两个日期时间对象之间的时差? - How do I find the time difference between two datetime objects in python? 我如何获得两个日期之间的差异,但也有一种方法可以总结每天的小时数? - How do I get difference between two dates but also have a way to sum up hours by day? 如何在熊猫中找到昨天和今天之间的时间差异? - How do I find the difference in time between yesterday and today in pandas? 带有 dateindex 的 Pandas 时间序列,如何找到值小于数字的两个小时之间的小时数? - Pandas timeseries with dateindex, how do I find hours between two where a value is < than a number? 以小时和分钟为单位计算两个 Pandas 列之间的时间差 - Calculate Time Difference Between Two Pandas Columns in Hours and Minutes 如何找到大熊猫两个日期时间的差异? - How to find difference between two date time in pandas?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM