Python时间 altzone() 方法
Python altzone() 方法用于返回本地 DST 时区的偏移量,以 UTC 以西的秒数为单位。我们还可以通过使用 timezone 和 altzone 方法获取当前时区和 UTC 之间的当前时间。
句法:
time.altzone
例子:
Python3
# import the time module
import time
# display altzone
print(time.altzone)
Python3
# import time module
import time
# get_zone function to get the
# current time between current time zone and UTC
# by using timezone and altzone method
def get_zone():
dst = time.daylight and time.localtime().tm_isdst
# get altzone if there exists dst otherwise
# get timezone
offset = time.altzone if dst else time.timezone
# check if offset greater than 0
westerly = offset > 0
# get the minutes and seconds
minutes, seconds = divmod(abs(offset), 60)
hours, minutes = divmod(minutes, 60)
# return the timezone
return '{}{:02d}{:02d}'.format('-' if westerly else '+', hours, minutes)
# call the function
get_zone()
输出:
25200
示例: Python程序使用 timezone 和 altzone 方法获取当前时区和 UTC 之间的当前时间
Python3
# import time module
import time
# get_zone function to get the
# current time between current time zone and UTC
# by using timezone and altzone method
def get_zone():
dst = time.daylight and time.localtime().tm_isdst
# get altzone if there exists dst otherwise
# get timezone
offset = time.altzone if dst else time.timezone
# check if offset greater than 0
westerly = offset > 0
# get the minutes and seconds
minutes, seconds = divmod(abs(offset), 60)
hours, minutes = divmod(minutes, 60)
# return the timezone
return '{}{:02d}{:02d}'.format('-' if westerly else '+', hours, minutes)
# call the function
get_zone()
输出:
+0000