Advertisement
gnamp

What day was this date? [Python]

Aug 14th, 2020
1,011
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.01 KB | None | 0 0
  1. def weekDay(year, month, day):
  2.     offset = [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334]
  3.     week   = ['Sunday',
  4.               'Monday',
  5.               'Tuesday',
  6.               'Wednesday',
  7.               'Thursday',  
  8.               'Friday',
  9.               'Saturday']
  10.     afterFeb = 1
  11.     if month > 2: afterFeb = 0
  12.     aux = year - 1700 - afterFeb
  13.     # dayOfWeek for 1700/1/1 = 5, Friday
  14.     dayOfWeek  = 5
  15.     # partial sum of days betweem current date and 1700/1/1
  16.     dayOfWeek += (aux + afterFeb) * 365                  
  17.     # leap year correction    
  18.     dayOfWeek += aux / 4 - aux / 100 + (aux + 100) / 400    
  19.     # sum monthly and day offsets
  20.     dayOfWeek += offset[month - 1] + (day - 1)              
  21.     dayOfWeek %= 7
  22.     return dayOfWeek, week[dayOfWeek]
  23.  
  24. print weekDay(2013, 6, 15) == (6, 'Saturday')
  25. print weekDay(1969, 7, 20) == (0, 'Sunday')
  26. print weekDay(1945, 4, 30) == (1, 'Monday')
  27. print weekDay(1900, 1, 1)  == (1, 'Monday')
  28. print weekDay(1789, 7, 14) == (2, 'Tuesday')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement