Last active
August 29, 2015 14:13
-
-
Save bitzip/c269505dc181bfccab94 to your computer and use it in GitHub Desktop.
列出指定时区从1970年01月01日至今,时区历史改动日期,以及夏令开始和结束日期。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding=utf8 | |
""" | |
# 功能 | |
列出指定时区从1970年01月01日至今,时区历史改动日期,以及夏令开始和结束日期。 | |
# 时区改动 | |
- 时区改动检查时期为00:00:00,意味着如果时区改动生效时间不是零点零时,时区改动日期将为 | |
检查结果的下一日。 | |
- 不考虑在夏令开始或结束当日,时区发生改变的情况。依目前看,夏令开始和结束的时间调整点 | |
不同地区不同,举例(重庆的夏令结束时间调整点不发生在零点,而吉隆波则在零点调整) | |
""" | |
import pytz | |
import datetime | |
import time | |
tz_utc = pytz.utc | |
# 指定的时区 | |
tz_local = pytz.timezone('Asia/Chongqing') | |
utc_date = datetime.datetime(1970, 1, 1).replace(tzinfo=tz_utc) | |
local_date = tz_local.fromutc(utc_date.replace(tzinfo=tz_local)) | |
def get_timestamp(dt): | |
return time.mktime(dt.timetuple()) | |
def get_time_delta(dtfrom, dtto): | |
delta = local_date.replace(tzinfo=None) - utc_date.replace(tzinfo=None) | |
return delta.total_seconds() / 60 / 60 | |
def get_day_length_delta(dt): | |
d = dt.date() | |
delta = get_timestamp(d + datetime.timedelta(days=1)) - get_timestamp(d) | |
return delta / 60 / 60 - 24.0 | |
def print_change_date(dt, old_delta, new_delta): | |
print u'================================================' | |
print u'%s => 时区改变' % dt | |
print u'时差从`%f`小时变成`%f`小时' % (old_delta, new_delta) | |
print u'================================================' | |
time_delta = get_time_delta(local_date, utc_date) | |
cur_day_length_delta = 0 | |
offset = 0 | |
while 1: | |
utc_date = utc_date + datetime.timedelta(days=1) | |
local_date = tz_local.fromutc(utc_date.replace(tzinfo=tz_local)) | |
new_time_delta = get_time_delta(local_date, utc_date) | |
change_time = new_time_delta - time_delta | |
day_length_delta = get_day_length_delta(local_date) | |
if utc_date.year > datetime.datetime.today().year: | |
break | |
if day_length_delta: | |
cur_day_length_delta = cur_day_length_delta + day_length_delta | |
if day_length_delta < 0: | |
print u'%s => 夏令开始,日长改变%s小时' % ( | |
local_date.date(), day_length_delta) | |
if day_length_delta > 0: | |
print u'%s => 夏令结束,日长改变%s小时' % ( | |
local_date.date(), day_length_delta) | |
continue | |
if change_time: | |
if cur_day_length_delta: | |
offset = change_time + cur_day_length_delta | |
if offset != 0: | |
print_change_date( | |
utc_date.date(), time_delta, time_delta + offset) | |
time_delta = new_time_delta | |
else: | |
print_change_date( | |
utc_date.date(), time_delta, time_delta + change_time) | |
time_delta = new_time_delta |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment