Created
January 31, 2018 13:58
-
-
Save d0zingcat/ce7dc108175eb71e82243e544a2bebfd to your computer and use it in GitHub Desktop.
A simple unix calendar implementation in python
This file contains 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
# -*- coding: utf-8 -*- | |
""" | |
Spyder Editor | |
This is a temporary script file. | |
""" | |
import os | |
def print_months(year, month): | |
if year < 1900: | |
print("CAN NOT PROCESS!!!") | |
return | |
start_year = 1900 | |
start_month = 1 | |
start_day = 1 | |
start_weekday = 1 | |
print_title(year, month) | |
days = calculation(year, month, 1) | |
day_of_week = days % 7 | |
for i in range(day_of_week): | |
print('%3s' % '', end='') | |
for i in range(month_days(year, month)): | |
if ((i+1+day_of_week) % 7 == 0): | |
print('%3s' % str(i+1), end='') | |
print() | |
else: | |
print('%3s' % str(i+1), end='') | |
def print_title(year, month): | |
months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', | |
'August', 'September', 'October', 'November', 'December'] | |
month = months[month-1] | |
month_len = len(month) | |
center_content = month + " " + str(year) | |
indent_len = (21 - len(center_content))/2 | |
indent_blank = '' | |
for i in range(int(indent_len)): | |
indent_blank += ' ' | |
print(indent_blank + center_content) | |
print("%3s%3s%3s%3s%3s%3s%3s" % ("Su", "Mo", "Tu", "We", "Th", "Fr", "Sa")) | |
def is_prime_year(year): | |
if year % 400 == 0 or year %4 == 0 and year % 100 != 0: | |
return True | |
else: | |
return False | |
def month_days(year, month): | |
is_prime = is_prime_year(year) | |
if is_prime and month == 2: | |
return 29 | |
elif not is_prime and month == 2: | |
return 28; | |
elif month in (1, 3, 5, 7, 8, 10, 12): | |
return 31 | |
else: | |
return 30 | |
def year_days(year): | |
if is_prime_year(year): | |
return 366 | |
else: | |
return 365 | |
def calculation(year, month, day): | |
days = 0 | |
for i in range(1900, year): | |
days += year_days(i) | |
for i in range(1, month): | |
days += month_days(year, i) | |
days += day | |
return days | |
if __name__ == "__main__": | |
print_months(2018, 2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment