fwarren icon

Find workdays

fwarren | PRO | 03/14/17 05:34:58 PM UTC | 0 ⭐ | 385 👁️ | Never ⏰ | []
Python |

3.77 KB

|

None

|

0 👍

/

0 👎

import datetime as dt
 
holidays = [
    "2017-01-01", "2017-05-29", "2017-07-04", "2017-09-04",
    "2017-11-24", "2017-11-25", "2017-12-25",
    "2018-01-01", "2018-05-28", "2018-07-04", "2018-09-03",
    "2018-11-22", "2018-11-23", "2018-12-25",
    "2019-01-01", "2019-05-27", "2019-07-04", "2019-09-02",
    "2019-11-28", "2019-11-29", "2019-12-25",
    "2020-01-01", "2020-05-25", "2020-07-03", "2020-09-07",
    "2020-11-26", "2020-11-27", "2020-12-25",
    "2020-12-31", "2021-05-31", "2021-07-05", "2021-09-06",
    "2021-11-25", "2021-11-26", "2021-12-25",
    "2021-12-31", "2022-05-30", "2022-07-04", "2022-09-05",
    "2022-11-24", "2022-11-25", "2022-12-24",
    "2023-01-02", "2023-05-29", "2023-07-04", "2023-09-04",
    "2023-11-22", "2023-11-23", "2023-12-26",
    "2024-01-01", "2024-05-27", "2024-07-04", "2024-09-02",
    "2024-11-28", "2024-11-29", "2024-12-25",
    "2025-01-01", "2025-05-26", "2025-07-04", "2025-09-01",
    "2025-11-27", "2025-11-28", "2025-12-25",
]
 
 
def make_from_ymd(text):
    """ convert text to a datetime date object
 
    Args:
        text (string): date in YYYY-MM-DD format
 
    Returns:
        date: datetime date object
    """
 
    return dt.datetime.strptime(text, "%Y-%m-%d").date()
 
 
def is_holiday(day):
    """Is a weekday date a holiday
    Args:
        day (date): weekday in datetime date format
 
    Returns:
        bool: True if the date is a holiday
 
    """
 
    return day.strftime('%Y-%m-%d') in holidays
 
 
def is_workday(day):
    """Is the date a workday (Exclude Holiday and Weekends)
 
    Args:
        day (date): weekday in datetime date format
 
    Return:
        bool: True if the date is a workday
 
    """
 
    return (not is_holiday(day)) and day.weekday() < 5
 
 
def not_workday(day):
    """Is the date nota a workday (Either a Holiday or Weekend)
 
    Args:
        day (date): weekday in datetime date format
 
    Return:
        bool: True if the date is not a workday
 
    """
 
    return is_holiday(day) or day.weekday() > 4
 
 
def startday(start):
    """The startdate of the event period
 
    Making the assumption that today is a workday the first day of the
    event period will include today and any day prior to today that is
    a holiday or a Sunday. Example: Tues May 30th, 2017 would include
    itself, Monday May 29th (Memorial Day) and Sunday May 28th. With
    the start date being 2017-05-28
 
    Args:
        start (datetime.date): the current valid workday in date format
 
    Returns:
        datetime.date: the start date for the current workday
 
    """
 
    day = start
    while True:
        day -= dt.timedelta(days=1)
        if is_workday(day) or day.weekday() == 5:
            day += dt.timedelta(days=1)
            return day
 
 
def endday(start):
    """The end date of the event period
 
    Making the assumption that today is a workday the last day of the
    event period will include today and any following days if they are
    a holiday or a Saturday. Example: Today is Friday May 26, 2017 then
    this period ends with Saturday May 27th 2017-05-27
 
    Args:
        start (datetime.date): the current valid workday in date format
 
    Returns:
        datetime.date: the end date for the current event period
 
    """
 
    day = start
    while True:
        day += dt.timedelta(days=1)
        if is_workday(day) or day.weekday() == 6:
            day -= dt.timedelta(days=1)
            return day
 
 
def example(text):
    """Dispaly info about what are event days for a date
 
    Args:
        text (string): text represntation of YYY-MM-DD of event
 
    Return:
        null:
    """
    day = make_from_ymd(text)
    print "The Event is from %s to %s" % (startday(day), endday(day))

Comments