Guest User

Untitled

a guest
Aug 20th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.25 KB | None | 0 0
  1. import time
  2.  
  3.  
  4. month_map = {
  5. 1: 1,
  6. 2: 4,
  7. 3: 4,
  8. 4: 0,
  9. 5: 2,
  10. 6: 5,
  11. 7: 0,
  12. 8: 3,
  13. 9: 6,
  14. 10: 1,
  15. 11: 4,
  16. 12: 6,
  17. }
  18. month_list = [31,28,31,30,31,30,31,31,30,31,30,31]
  19.  
  20.  
  21.  
  22. def compute_year_code(year_num: int) -> int:
  23. year_num_str = str(year_num)
  24. last_part = int(year_num_str[2:4])
  25. return (6 + last_part + int(last_part/4)) % 7
  26.  
  27.  
  28. def compute_month_by_week(week_num: int, year_num: int, is_long_year: bool):
  29. month_list[1] = 28
  30. if is_long_year:
  31. month_list[1] = 29
  32. days = week_num*7
  33. month = 0
  34. for i in month_list:
  35. days -= i
  36. month += 1
  37. if days <= 0:
  38. return days + i - get_day_week(year_num, is_long_year=is_long_year) + 1, month
  39.  
  40.  
  41. def is_long_year(year_num: int) -> bool:
  42. is_long = False
  43. if not year_num%400:
  44. return True
  45.  
  46. if not year_num%4:
  47. is_long = True
  48.  
  49. if not year_num%100:
  50. is_long = False
  51.  
  52. return is_long
  53.  
  54.  
  55. def correct_day(day: int):
  56. return_day = day + 6
  57. if return_day > 7:
  58. return_day -= 7
  59. return return_day
  60.  
  61.  
  62. def get_day_week(year_num, day_code=1, month=1, is_long_year=False):
  63. year_code = compute_year_code(year_num)
  64. month_code = month_map[month]
  65. day_week = (day_code + month_code + year_code) % 7
  66. if is_long_year:
  67. day_week -= 1
  68. if day_week < 0:
  69. day_week += 7
  70. return correct_day(day_week)
  71.  
  72.  
  73. def compute_day_week(week_num: int, year_num: int, is_long_year: bool):
  74. monday = 2
  75. day_code, month = compute_month_by_week(week_num, year_num, is_long_year)
  76. if day_code - 7 < 1:
  77. raise Exception("Wrong number of week")
  78. year_code = compute_year_code(year_num)
  79. month_code = month_map[month]
  80. day_week = (day_code + month_code + year_code) % 7
  81. if is_long_year:
  82. day_week -= 1
  83. if day_week < 0:
  84. day_week += 7
  85.  
  86. if day_week > monday:
  87. day_code -= (day_week - monday)
  88. if day_week < monday:
  89. day_code += (monday - day_week)
  90.  
  91. return day_code, month
  92.  
  93.  
  94. def compute(week_num: int, year_num: int):
  95. is_long = is_long_year(year_num)
  96. print(year_num, compute_day_week(week_num, year_num, is_long))
  97.  
  98.  
  99. start_time = time.time()
  100. compute(2, 2000)
  101. print(time.time())
  102. print(start_time)
  103. print(time.time() - start_time)
Add Comment
Please, Sign In to add comment