Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- SECONDS_PER_UNIX_DAY = 24 * 60 * 60
- DAYS_PER_MONTH = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
- def is_leap(year):
- return ((year % 4) == 0 and not (year % 100) == 0) or (year % 400) == 0
- def in_month(epoch, month):
- assert epoch > 0, "Unix time is only defined after 1.1.1970, 00:00:00 UTC"
- assert 1 <= month < 12, "Invalid month"
- year = 1970
- epoch_of_year = 0
- while True:
- cur_year = (366 if is_leap(year) else 365) * SECONDS_PER_UNIX_DAY
- if cur_year + epoch_of_year > epoch:
- break
- year += 1
- epoch_of_year += cur_year
- days = epoch - epoch_of_year
- _mos = list()
- _mos[:] = DAYS_PER_MONTH[:]
- if is_leap(year):
- _mos[1] += 1
- month_start = sum(_mos[:month - 1]) * SECONDS_PER_UNIX_DAY
- month_end = sum(_mos[:month]) * SECONDS_PER_UNIX_DAY
- return month_start <= days < month_end
- def which_month(epoch):
- for month_no in range(1, 13):
- if in_month(epoch, month_no):
- return month_no
- raise Exception("This should never happen")
- def in_may(epoch):
- return in_month(epoch, 5)
- for ts, may, mo, _ in (
- (1178209938, True, 5, "# Thu May 03 2007 16:32:18 GMT+0000 # květen nepřestupný"),
- ( 957371538, True, 5, "# Wed May 03 2000 16:32:18 GMT+0000 # květen přestupný"),
- (1238776338, False, 4, "# Fri Apr 03 2009 16:32:18 GMT+0000 # nekvěten nepřestupný"),
- (1207240338, False, 4, "# Thu Apr 03 2008 16:32:18 GMT+0000 # nekvěten přestupný")
- ):
- assert in_may(ts) == may
- assert which_month(ts) == mo
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement