|
| 1 | +#!/usr/bin/python3 |
| 2 | +from datetime import datetime |
| 3 | +import sys |
| 4 | + |
| 5 | + |
| 6 | +def is_leap(year): |
| 7 | + return year % 4 == 0 and year % 100 != 0 or year % 400 == 0 |
| 8 | + |
| 9 | + |
| 10 | +def get_days(year, month): |
| 11 | + days = [[31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31], |
| 12 | + [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]] |
| 13 | + return days[is_leap(year)][month - 1] |
| 14 | + |
| 15 | + |
| 16 | +def main(): |
| 17 | + if len(sys.argv) > 1: |
| 18 | + month = int(sys.argv[1]) |
| 19 | + year = int(sys.argv[2]) |
| 20 | + else: |
| 21 | + current_date = datetime.now() |
| 22 | + year = current_date.year |
| 23 | + month = current_date.month |
| 24 | + year2 = year if month >= 3 else year - 1 |
| 25 | + c = year2 // 100 |
| 26 | + y = year2 % 100 |
| 27 | + m = month if month >= 3 else month + 12 |
| 28 | + w = y + y // 4 + c // 4 - 2 * c + 26 * (m + 1) // 10 |
| 29 | + w %= 7 |
| 30 | + months = ['January', 'February', 'March', 'April', 'May', 'June', |
| 31 | + 'July', 'August', 'September', 'October', 'November', 'December'] |
| 32 | + print(f'{months[month - 1]} {year}'.center(20)) |
| 33 | + print('Su Mo Tu We Th Fr Sa') |
| 34 | + print(' ' * 3 * w, end='') |
| 35 | + total_days = get_days(year, month) |
| 36 | + for day in range(1, total_days + 1): |
| 37 | + print(f'{day}'.rjust(2), end=' ') |
| 38 | + w += 1 |
| 39 | + if w == 7: |
| 40 | + print() |
| 41 | + w = 0 |
| 42 | + print() |
| 43 | + |
| 44 | + |
| 45 | +if __name__ == '__main__': |
| 46 | + main() |
0 commit comments