Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import time
- month_map = {
- 1: 1,
- 2: 4,
- 3: 4,
- 4: 0,
- 5: 2,
- 6: 5,
- 7: 0,
- 8: 3,
- 9: 6,
- 10: 1,
- 11: 4,
- 12: 6,
- }
- month_list = [31,28,31,30,31,30,31,31,30,31,30,31]
- def compute_year_code(year_num: int) -> int:
- year_num_str = str(year_num)
- last_part = int(year_num_str[2:4])
- return (6 + last_part + int(last_part/4)) % 7
- def compute_month_by_week(week_num: int, year_num: int, is_long_year: bool):
- month_list[1] = 28
- if is_long_year:
- month_list[1] = 29
- days = week_num*7
- month = 0
- for i in month_list:
- days -= i
- month += 1
- if days <= 0:
- return days + i - get_day_week(year_num, is_long_year=is_long_year) + 1, month
- def is_long_year(year_num: int) -> bool:
- is_long = False
- if not year_num%400:
- return True
- if not year_num%4:
- is_long = True
- if not year_num%100:
- is_long = False
- return is_long
- def correct_day(day: int):
- return_day = day + 6
- if return_day > 7:
- return_day -= 7
- return return_day
- def get_day_week(year_num, day_code=1, month=1, is_long_year=False):
- year_code = compute_year_code(year_num)
- month_code = month_map[month]
- day_week = (day_code + month_code + year_code) % 7
- if is_long_year:
- day_week -= 1
- if day_week < 0:
- day_week += 7
- return correct_day(day_week)
- def compute_day_week(week_num: int, year_num: int, is_long_year: bool):
- monday = 2
- day_code, month = compute_month_by_week(week_num, year_num, is_long_year)
- if day_code - 7 < 1:
- raise Exception("Wrong number of week")
- year_code = compute_year_code(year_num)
- month_code = month_map[month]
- day_week = (day_code + month_code + year_code) % 7
- if is_long_year:
- day_week -= 1
- if day_week < 0:
- day_week += 7
- if day_week > monday:
- day_code -= (day_week - monday)
- if day_week < monday:
- day_code += (monday - day_week)
- return day_code, month
- def compute(week_num: int, year_num: int):
- is_long = is_long_year(year_num)
- print(year_num, compute_day_week(week_num, year_num, is_long))
- start_time = time.time()
- compute(2, 2000)
- print(time.time())
- print(start_time)
- print(time.time() - start_time)
Add Comment
Please, Sign In to add comment