Skip to content

Instantly share code, notes, and snippets.

@jcchurch
Last active August 22, 2024 02:08
Show Gist options
  • Save jcchurch/7321bec73f319c27c90d6eeb17a6c50e to your computer and use it in GitHub Desktop.
Save jcchurch/7321bec73f319c27c90d6eeb17a6c50e to your computer and use it in GitHub Desktop.
Adds line to let bash know that this is a python 3 script.
#!/usr/bin/env python3
import datetime
import argparse
import re
def createDate(stringDate):
if re.match("\d\d\d\d-\d\d-\d\d$", stringDate) is None:
raise ValueError("This is not in the correct date format. Use YYYY-MM-DD")
(yearStr, monthStr, dayStr) = stringDate.split("-")
year = int(yearStr)
month = int(monthStr)
day = int(dayStr)
if year < 1970:
raise ValueError("Year is less than 1970.")
if year > 2100:
raise ValueError("Year is greater than 2100.")
if month < 1 or month > 12:
raise ValueError("Month is not 01 to 12.")
if day < 1 or day > 31:
raise ValueError("Day is not 01 to 31.")
return datetime.date(year, month, day)
def displaySequentialDates(startStr, endStr, daysOfWeek, months, dateFormat):
startDate = createDate(options.startDate)
endDate = createDate(options.endDate)
while startDate <= endDate:
if getLetterCodeForDayOfWeek(startDate) in daysOfWeek and startDate.month in months:
print(startDate.strftime(dateFormat))
startDate += datetime.timedelta(days=1)
def getLetterCodeForDayOfWeek(date):
return "MTWRFSU"[ date.weekday() ]
if __name__ == '__main__':
p = argparse.ArgumentParser(description="List dates in sequential order")
p.add_argument("-s", "--start", metavar="2016-01-01", dest="startDate",
help="Start Date in YYYY-MM-DD. Uses current date as the default.")
p.add_argument("-e", "--end", dest="endDate",
help="End Date in YYYY-MM-DD. Uses current date as the default.", metavar="2016-01-31")
p.add_argument("-d", "--dow", dest="daysOfWeek", metavar="MTWRFSU", default="MTWRFSU",
help="Only print these days of the week: Monday, Tuesday, Wednesday, thuRsday, Friday, Saturday, sUnday. Default is MTWRFSU")
p.add_argument("-y", "--year", metavar="2016", dest="year",
help="Sets the start and end dates to be first and last day of year initially. This can be further overwritten by -s and -e flags.")
p.add_argument("-m", "--months", nargs="+", choices=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], type=int, dest="months",
help="Limits output to certain months. 1 is January, 2 is February, ..., 12 is December.")
p.add_argument("-f", "--format", dest="dateFormat", metavar="'%Y-%m-%d'", default="%A. %B %d, %Y",
help="Specify Date Format. Uses Python3's datetime format. Default is '%%A. %%B %%d, %%Y'")
options = p.parse_args()
if re.match("[MTWRFSUmtwrfsu]+$", options.daysOfWeek) is None:
raise ValueError("This is not in day of week pattern. Use only the appropriate 1 letter codes.")
options.daysOfWeek = options.daysOfWeek.upper()
if options.year is not None:
options.startDate = "{0:04d}-{1:02d}-{2:02d}".format(int(options.year), 1, 1)
options.endDate = "{0:04d}-{1:02d}-{2:02d}".format(int(options.year), 12, 31)
if options.startDate is None:
now = datetime.date.today()
options.startDate = "{0:04d}-{1:02d}-{2:02d}".format(now.year, now.month, now.day)
if options.endDate is None:
now = datetime.date.today()
options.endDate = "{0:04d}-{1:02d}-{2:02d}".format(now.year, now.month, now.day)
if options.months is None:
options.months = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
displaySequentialDates(options.startDate, options.endDate, options.daysOfWeek, options.months, options.dateFormat)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment