[英]How to add two offset in python?
I am getting a offset form the pytz library from the following line: 我从下面的行中获取了pytz库的偏移量:
offset = datetime.datetime.now(pytz.timezone(timezone)).strftime('%z')
First i pass the US/Eastern
in timezone
variable and then i pass the Asia/Kolkata
in timezone
variable which prints the following value 首先我通过
US/Eastern
的timezone
变量,然后我通过Asia/Kolkata
的timezone
打印出下面的值变
local_utc = -0400
user_utc = +0530
After getting these values i converted it from string to int by following code: 得到这些值后,我通过以下代码将其从字符串转换为整数:
local_utc = int(local_utc)
user_urc = int(user_utc)
Apart from this i have a timetuple also: 除此之外,我还有一个时间表:
hour, minute,days = (timezone_tuple.tm_hour, timezone_tuple.tm_min,
timezone_tuple.tm_mday)
I want to add the difference of local_utc and user_utc
to above tuple such as -0400: 04
such as hour and 00
as minutes. 我想将
local_utc and user_utc
的区别添加到上述元组(例如local_utc and user_utc
: 04
例如小时,将00
作为分钟)。 For example: difference will be : 0930
. 例如:差将是:
0930
。 And 09
will be add to timezone_tuple.tm_hour
and 30
will be add to timezone_tuple.tm_min
并将
09
添加到timezone_tuple.tm_hour
,将30
添加到timezone_tuple.tm_min
I didn't found any situation. 我没有发现任何情况。 how can it be possible?
怎么可能呢? Is there any way to do with
spilit
method 有什么办法可以解决
spilit
方法
Your post showed how to find local_utc
and user_utc
as integers. 您的文章展示了如何找到整数的
local_utc
和user_utc
。 You could just take the difference local_utc-user_utc
to determine the relative offset. 您可以只求出
local_utc-user_utc
之差来确定相对偏移量。
However, datetime
, time
and pytz
should give you all the tools you need to manipulate times without having to parse offsets and do such calculations "manually". 但是,
datetime
, time
和pytz
应该为您提供处理时间所需的所有工具,而不必解析偏移量并“手动”进行此类计算。
For example, 例如,
import pytz
import datetime as dt
import time
eastern = pytz.timezone('US/Eastern')
kolkata = pytz.timezone('Asia/Kolkata')
naive_timetuple = time.localtime(0)
print(naive_timetuple)
# time.struct_time(tm_year=1969, tm_mon=12, tm_mday=31, tm_hour=19, tm_min=0, tm_sec=0, tm_wday=2, tm_yday=365, tm_isdst=0)
Above, I defined a naive timetuple. 上面,我定义了一个简单的时间元组。 Below, I "localize" it to US/Eastern time -- that is, make it a timezone-aware datetime:
在下面,我将其“本地化”为美国/东部时间-也就是说,使其成为可识别时区的日期时间:
naive_datetime = dt.datetime(*naive_timetuple[:6])
print(naive_datetime)
# 1969-12-31 19:00:00
localized_datetime = eastern.localize(naive_datetime)
print(localized_datetime)
# 1969-12-31 19:00:00-05:00
Now to convert a timezone-aware datetime to any other timezone, use the astimezone
method: 现在,要将时区感知的日期时间转换为任何其他时区,请使用
astimezone
方法:
kolkata_datetime = localized_datetime.astimezone(kolkata)
print(kolkata_datetime)
# 1970-01-01 05:30:00+05:30
And if you need to convert a datetime back to a timetuple, use the timetuple method: 如果需要将日期时间转换回时间元组,请使用timetuple方法:
kolkata_timetuple = kolkata_datetime.timetuple()
print(kolkata_timetuple)
# time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=5, tm_min=30, tm_sec=0, tm_wday=3, tm_yday=1, tm_isdst=0)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.