Last active
December 13, 2021 03:22
-
-
Save AntiKnot/3e3d2e8656e619b1c0e6880ecde717f5 to your computer and use it in GitHub Desktop.
Zeller's congruence
This file contains hidden or 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
| """ | |
| https://en.wikipedia.org/wiki/Zeller%27s_congruence | |
| https://zh.wikipedia.org/wiki/%E8%94%A1%E5%8B%92%E5%85%AC%E5%BC%8F | |
| """ | |
| from functools import wraps | |
| from icecream import ic | |
| # h is the day of the week (0 = Saturday, 1 = Sunday, 2 = Monday, ..., 6 = Friday) | |
| WEEK = {0: 'Sun.', | |
| 1: 'Mon.', | |
| 2: 'Tue.', | |
| 3: 'Wed.', | |
| 4: 'Thu.', | |
| 5: 'Fri.', | |
| 6: 'Sat.'} | |
| def fix_month(func): | |
| """ | |
| m is the month (3 = March, 4 = April, 5 = May, ..., 14 = February) | |
| """ | |
| @wraps(func) | |
| def wrapper(*args, **kwargs): | |
| m = kwargs.get('m') | |
| if m == 1: | |
| kwargs.update({'m': 13}) | |
| elif m == 2: | |
| kwargs.update({'m': 14}) | |
| return func(*args, **kwargs) | |
| return wrapper | |
| @fix_month | |
| def zeller(c, y, m, d): | |
| w = (y + y // 4 + c // 4 - 2 * c + (26 * (m + 1) // 10) + d - 1) | |
| # deal with negative number | |
| w = (w % 7 + 7) % 7 | |
| return WEEK[w] | |
| if __name__ == '__main__': | |
| year = 2006 | |
| month = 4 | |
| day = 4 | |
| res = zeller(20, 6, 4, 4) | |
| ic(res) | |
| # ---- | |
| # ic| res: 'Tue.' | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment