fwarren

Find workdays

Mar 14th, 2017
426
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.77 KB | None | 0 0
  1. import datetime as dt
  2.  
  3. holidays = [
  4.     "2017-01-01", "2017-05-29", "2017-07-04", "2017-09-04",
  5.     "2017-11-24", "2017-11-25", "2017-12-25",
  6.     "2018-01-01", "2018-05-28", "2018-07-04", "2018-09-03",
  7.     "2018-11-22", "2018-11-23", "2018-12-25",
  8.     "2019-01-01", "2019-05-27", "2019-07-04", "2019-09-02",
  9.     "2019-11-28", "2019-11-29", "2019-12-25",
  10.     "2020-01-01", "2020-05-25", "2020-07-03", "2020-09-07",
  11.     "2020-11-26", "2020-11-27", "2020-12-25",
  12.     "2020-12-31", "2021-05-31", "2021-07-05", "2021-09-06",
  13.     "2021-11-25", "2021-11-26", "2021-12-25",
  14.     "2021-12-31", "2022-05-30", "2022-07-04", "2022-09-05",
  15.     "2022-11-24", "2022-11-25", "2022-12-24",
  16.     "2023-01-02", "2023-05-29", "2023-07-04", "2023-09-04",
  17.     "2023-11-22", "2023-11-23", "2023-12-26",
  18.     "2024-01-01", "2024-05-27", "2024-07-04", "2024-09-02",
  19.     "2024-11-28", "2024-11-29", "2024-12-25",
  20.     "2025-01-01", "2025-05-26", "2025-07-04", "2025-09-01",
  21.     "2025-11-27", "2025-11-28", "2025-12-25",
  22. ]
  23.  
  24.  
  25. def make_from_ymd(text):
  26.     """ convert text to a datetime date object
  27.  
  28.    Args:
  29.        text (string): date in YYYY-MM-DD format
  30.  
  31.    Returns:
  32.        date: datetime date object
  33.    """
  34.  
  35.     return dt.datetime.strptime(text, "%Y-%m-%d").date()
  36.  
  37.  
  38. def is_holiday(day):
  39.     """Is a weekday date a holiday
  40.    Args:
  41.        day (date): weekday in datetime date format
  42.  
  43.    Returns:
  44.        bool: True if the date is a holiday
  45.  
  46.    """
  47.  
  48.     return day.strftime('%Y-%m-%d') in holidays
  49.  
  50.  
  51. def is_workday(day):
  52.     """Is the date a workday (Exclude Holiday and Weekends)
  53.  
  54.    Args:
  55.        day (date): weekday in datetime date format
  56.  
  57.    Return:
  58.        bool: True if the date is a workday
  59.  
  60.    """
  61.  
  62.     return (not is_holiday(day)) and day.weekday() < 5
  63.  
  64.  
  65. def not_workday(day):
  66.     """Is the date nota a workday (Either a Holiday or Weekend)
  67.  
  68.    Args:
  69.        day (date): weekday in datetime date format
  70.  
  71.    Return:
  72.        bool: True if the date is not a workday
  73.  
  74.    """
  75.  
  76.     return is_holiday(day) or day.weekday() > 4
  77.  
  78.  
  79. def startday(start):
  80.     """The startdate of the event period
  81.  
  82.    Making the assumption that today is a workday the first day of the
  83.    event period will include today and any day prior to today that is
  84.    a holiday or a Sunday. Example: Tues May 30th, 2017 would include
  85.    itself, Monday May 29th (Memorial Day) and Sunday May 28th. With
  86.    the start date being 2017-05-28
  87.  
  88.    Args:
  89.        start (datetime.date): the current valid workday in date format
  90.  
  91.    Returns:
  92.        datetime.date: the start date for the current workday
  93.  
  94.    """
  95.  
  96.     day = start
  97.     while True:
  98.         day -= dt.timedelta(days=1)
  99.         if is_workday(day) or day.weekday() == 5:
  100.             day += dt.timedelta(days=1)
  101.             return day
  102.  
  103.  
  104. def endday(start):
  105.     """The end date of the event period
  106.  
  107.    Making the assumption that today is a workday the last day of the
  108.    event period will include today and any following days if they are
  109.    a holiday or a Saturday. Example: Today is Friday May 26, 2017 then
  110.    this period ends with Saturday May 27th 2017-05-27
  111.  
  112.    Args:
  113.        start (datetime.date): the current valid workday in date format
  114.  
  115.    Returns:
  116.        datetime.date: the end date for the current event period
  117.  
  118.    """
  119.  
  120.     day = start
  121.     while True:
  122.         day += dt.timedelta(days=1)
  123.         if is_workday(day) or day.weekday() == 6:
  124.             day -= dt.timedelta(days=1)
  125.             return day
  126.  
  127.  
  128. def example(text):
  129.     """Dispaly info about what are event days for a date
  130.  
  131.    Args:
  132.        text (string): text represntation of YYY-MM-DD of event
  133.  
  134.    Return:
  135.        null:
  136.    """
  137.     day = make_from_ymd(text)
  138.     print "The Event is from %s to %s" % (startday(day), endday(day))
Advertisement
Add Comment
Please, Sign In to add comment