Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Find the dates of the start and end of Summertime in the UK, for any given year
- # Find the minimum and maximum possible duration for Summertime this century
- from datetime import *
- def get_last_sunday(adate):
- d1 = datetime.strptime(adate, "%Y-%m-%d") # make a datetime object
- dow = datetime.isoweekday(d1) # get day of week of last day in month
- dd = 31 - (dow % 7) # go back to the last Sunday in the month
- return adate[:8] + str(dd) # return full date of last Sunday
- y = input("Enter a year from this century: ")
- mindur, maxdur = 999, 0
- for yy in range(2000, 2100): # loop for all years in this century
- ys = str(yy)
- summerstart = get_last_sunday(ys + "-03-31") # gat date of last Sunday in March
- summerend = get_last_sunday(ys + "-10-31") # get date of last Sunday in October
- dstart = datetime.strptime(summerstart, "%Y-%m-%d") # construct datetime objects
- dend = datetime.strptime(summerend, "%Y-%m-%d")
- duration = abs((dend - dstart).days) # compute difference in dates
- mindur = min(mindur, duration) # get min duration so far
- maxdur = max(maxdur, duration) # get max duration so far
- if ys == y: # print results for a specific year
- print(f"Summer time in {ys} is from {summerstart} to {summerend} and lasts for {duration} days")
- print(f"In years from 2000 to 2099, Summertime can last between {mindur} and {maxdur} days")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement