Advertisement
acclivity

pySummerTime

Jul 28th, 2022
966
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.57 KB | None | 0 0
  1. # Find the dates of the start and end of Summertime in the UK, for any given year
  2. # Find the minimum and maximum possible duration for Summertime this century
  3. from datetime import *
  4.  
  5. def get_last_sunday(adate):
  6.     d1 = datetime.strptime(adate, "%Y-%m-%d")       # make a datetime object
  7.     dow = datetime.isoweekday(d1)                   # get day of week of last day in month
  8.     dd = 31 - (dow % 7)                             # go back to the last Sunday in the month
  9.     return adate[:8] + str(dd)                      # return full date of last Sunday
  10.  
  11. y = input("Enter a year from this century: ")
  12.  
  13. mindur, maxdur = 999, 0
  14. for yy in range(2000, 2100):                        # loop for all years in this century
  15.     ys = str(yy)
  16.     summerstart = get_last_sunday(ys + "-03-31")    # gat date of last Sunday in March
  17.     summerend = get_last_sunday(ys + "-10-31")      # get date of last Sunday in October
  18.     dstart = datetime.strptime(summerstart, "%Y-%m-%d")     # construct datetime objects
  19.     dend = datetime.strptime(summerend, "%Y-%m-%d")
  20.     duration = abs((dend - dstart).days)            # compute difference in dates
  21.     mindur = min(mindur, duration)                  # get min duration so far
  22.     maxdur = max(maxdur, duration)                  # get max duration so far
  23.     if ys == y:                                     # print results for a specific year
  24.         print(f"Summer time in {ys} is from {summerstart} to {summerend} and lasts for {duration} days")
  25.  
  26. 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