[英]Get the offset for a specific time and timezone in python
I need a function that will return the offset for a specific timezone at a specific moment. 我需要一个函数,该函数将在特定时刻返回特定时区的偏移量。 I have the timezone in a string variable in the format America/Los_Angeles
and I have the date in format 2014-12-01
我将时区放在格式为America/Los_Angeles
的字符串变量中,并且将日期设置为2014-12-01
The offset needs to be DST sensitive, meaning that for Los Angeles, it should return -07 during summer and -08 during winter. 偏移量必须对DST敏感,这意味着对于洛杉矶,夏季应返回-07,冬季应返回-08。
You can convert the string into a timezone object with pytz : 您可以使用pytz将字符串转换为时区对象:
tz = pytz.timezone('America/Los_Angeles')
Convert the date to a datetime
object with strptime
: 使用strptime
将日期转换为datetime
对象:
dt = datetime.datetime.strptime('2014-12-01', '%Y-%m-%d')
Now the hour offset can be derived: 现在可以得出小时偏移量:
offset = int(tz.utcoffset(dt).total_seconds() / 3600.0)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.