Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- Date Handling
- """
- import datetime
- def name_of_weekday(date):
- """What is the name of the weekday?
- No work needed. But, notice that:
- 1. date.weekday() returns an index 0 through 6.
- 2. we use that index to get a name.
- """
- weekday_names = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
- return weekday_names[date.weekday()]
- #--------------------------------------------------------------
- # IMPLEMENT THE FUNCTIONS BELOW THIS LINE
- def text_to_ymd(text):
- """
- Convert text into a year, month, day
- ISO: YYYY-MM-DD
- """
- # 1. split the text on "-":
- # year, month, day = text.split("-")
- # 2. Convert all three values into ints
- # 3. Return the three values: year, month, day
- year, month, day = text.split("-")
- # REPLACE THIS LINE:
- return int(year), int(month), int(day)
- def new_years_day():
- """
- What day of the week was this year's January 1st?
- """
- # 1. Get today's date:
- # today = datetime.date.today()
- # 2. get the year: today.year
- # 3. Make and return new date object:
- # return datetime.date(y, m, d) (pick y, m, d appropriately)
- today = datetime.date.today()
- year = today.year
- # REPLACE THIS LINE:
- return datetime.date(year, 1, 1)
- def n_weeks_after_newyears(n_weeks):
- """What day was n_weeks weeks after new year's day?
- """
- # 1. Call new_years_day() to get the first date of this year.
- # 2. Call datetime.timedelta( X ), which is an interval.
- # X is the number of days. (remember 7 days per week).
- # 3. Add that to the first date, and return
- first_date = new_years_day()
- week = datetime.timedelta(n_weeks * 7)
- result = first_date + week
- # REPLACE THIS LINE:
- return result
- def first_thursday(month):
- """What was the first Thursday of the given month (this year)?
- """
- # 1. Get today, and get today.year to get this year.
- # 2. make a date = datetime.date(year, month, 1) for first day of month.
- # 3. Loop:
- # while True:
- # 4. inside loop, check if date.weekday() == 3
- # if so, it's Thursday. break.
- # 5. otherwise, add one day:
- # date += datetime.timedelta(1)
- # when done, return date
- today = datetime.date.today()
- year = today.year
- date = datetime.date(year, month, 1)
- while True:
- if date.weekday() == 3:
- break
- else:
- date += datetime.timedelta(1)
- # REPLACE THIS LINE:
- return date
- def thanksgiving_thursday():
- """What day is the third Friday of November, this year?
- """
- # 1. Get first Thursday of November (call first_thursday(11))
- # 2. add 14 days to it, and return
- x = first_thursday(11)
- x += datetime.timedelta(14)
- # REPLACE THIS LINE:
- return x
- def days_until_thanksgiving():
- """How many days from today until this year's thanksgiving?
- """
- # 1. Call thanksgiving Thursday
- # 2. Subtract today's date:
- # delta = thanksgiving_thursday() - datetime.date.today()
- # 3. return delta.days
- thanksgiving_thursday()
- delta = thanksgiving_thursday() - datetime.date.today()
- return delta.days
- def election_day(year):
- """When is election day, on the given year?
- It's the first Tuesday AFTER the first Monday of November.
- """
- # You can do it!
- # FIND THE FIRST MONDAY OF NOV
- # THEN ADD ONE DAY
- #today = datetime.date.today()
- #year = today.year
- date = datetime.date(year, 11, 1) #2016 Nov 1st
- while True:
- if date.weekday() == 3: #IF IT FINDS THURSDAY
- date += datetime.timedelta(3) #ADD 3 DAYS WHIC IS MONDAY
- break
- else:
- date += datetime.timedelta(1)
- # REPLACE THIS LINE:
- return date
- def is_leap_year(year):
- """Is the given year a leap year?
- """
- if year % 400 == 0:
- return True
- if year % 100 == 0:
- return False
- if year % 4 == 0:
- return True
- else:
- return False
- """
- Rules (proleptic Gregorian calendar):
- 1. If the year is not divisible by 4, it's NOT a leap year.
- return False
- 2. if the year IS divisible by 400, it's NOT a leap year.
- 3. otherwise, it's a leap year.
- """
- # REPLACE THIS LINE:
- return None
- # IMPLEMENT THE FUNCTIONS ABOVE THIS LINE
- #-------------------------------------------------------------
- def main():
- for text_date in ["2008-09-01", "2015-08-23"]:
- year, month, day = text_to_ymd(text_date)
- date = datetime.date(year, month, day)
- print("{:<22} : {}".format(text_date, date))
- print("New Year's Day ", new_years_day())
- print("New Year's was a ", name_of_weekday(new_years_day()))
- print("10 weeks later is a ",
- name_of_weekday(n_weeks_after_newyears(10)))
- print("First Thursday of Sept: ", first_thursday(9))
- print("Thanksgiving: ", thanksgiving_thursday())
- print("Days until thanksgiving:", days_until_thanksgiving())
- election = election_day(2016)
- print("Election day 2016: ", election)
- print("Election day was a ", name_of_weekday(election))
- for decade in range(1980, 2020, 10):
- for year in range(decade, decade + 10):
- print("{:>4} ".format(year), end = "")
- print()
- for year in range(decade, decade + 10):
- if is_leap_year(year):
- print("{:>4} ".format("Leap"), end = "")
- else:
- print("{:>4} ".format(""), end = "")
- print()
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement