Skip to content

Instantly share code, notes, and snippets.

@tedmiston
Last active April 4, 2017 18:55
Show Gist options
  • Save tedmiston/67a349ae2115fe59b5b0fb01577c4664 to your computer and use it in GitHub Desktop.
Save tedmiston/67a349ae2115fe59b5b0fb01577c4664 to your computer and use it in GitHub Desktop.
Generate all ordinal days for a given year.
"""
Generate ordinal days for a given year.
January 1st, January 2nd, January 3rd ... December 31st.
"""
import calendar
import datetime
def suffix(day):
return ({11: 'th', 12: 'th', 13: 'th'}.get(day) or
{1: 'st', 2: 'nd', 3: 'rd'}.get(day % 10, 'th'))
def ord_month(year, month):
cal = calendar.Calendar()
return (str(d) + suffix(d) for d in cal.itermonthdays(year, month) if d)
def ord_year(year):
months = calendar.month_name[1:]
return (f'{m} {d}' for i, m in enumerate(months) for d in ord_month(year, i+1))
def main():
year = datetime.date.today().year
print('\n'.join(ord_year(year)))
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment