-
-
Save albertofwb/1ef52334d5dc04b4d30fd6408a3a0d38 to your computer and use it in GitHub Desktop.
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
#! /usr/bin/env python3 | |
#encoding: utf-8 | |
# function: | |
# generates a column of dates on this month | |
# I wrote this script for a task about updating the dates | |
# of an excel file. | |
import datetime | |
import calendar | |
import sys | |
SkipWeekends = True | |
WeekMaps = {0: "星期日", 1: "星期一", 2: "星期二", 3: "星期三", 4: "星期四", 5: "星期五", 6: "星期六"} | |
def generate_dates(start_month, start_year=2018): | |
first_day = datetime.datetime(start_year, start_month, 1) | |
end_day = days_of_this_month(start_year, start_month) | |
start_week = first_day.weekday() | |
start_day = 1 | |
days = [] | |
while start_day <= end_day: | |
week = (start_week + start_day) % 7 | |
day_text = "%02d-%02d %s" % (start_month, start_day, WeekMaps[week]) | |
if not (SkipWeekends and week in [0, 6]): | |
days.append(day_text) | |
start_day += 1 | |
return days | |
def format_days_to_csv(days_list): | |
fp = open("days.csv", 'w', encoding="gbk") | |
for line in days_list: | |
line += '\n' | |
fp.write(line) | |
fp.close() | |
def days_of_this_month(year, month): | |
return calendar.monthrange(year, month)[1] | |
def parse_args(): | |
month = datetime.datetime.now().month | |
if len(sys.argv) > 1: | |
month = int(sys.argv[1]) | |
return month | |
def main(): | |
start_month = parse_args() | |
month_days = generate_dates(start_month) | |
print("get to work for %d days" % len(month_days)) | |
print('\n'.join(month_days)) | |
format_days_to_csv(month_days) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment