Estimate delivery date - check if a date is a business day

62 views
Skip to first unread message

Michael Lind Hjulskov

unread,
Aug 8, 2014, 9:39:47 AM8/8/14
to django...@googlegroups.com
In case someone want to estimate the delivery date here is a few functions I created, that might help

Off course you'll have to change the holidays in get_holidays, as its Danish holidays

Hope it helps someone

If there is a nother place for oscar/commerse related snippets like this, youre welcome to share it there

from datetime import date, timedelta
from dateutil import easter

def get_holidays(year=date.year):
    """ Returns Polish hollidays dates (legally considered non-working days) """
    easter_sunday = easter.easter(year)
    holidays = [
        # These holidays have a fixed date
        date(year,1,1), # New Year
        date(year,5,1), # Labor Day
        date(year,6,5), # Constitution Day (Grundlovsdag)
        date(year,12,24), # Christmas  Night
        date(year,12,25), # 1st Christmas  Day
        date(year,12,26),# 2st Christmas  Day
        date(year, 12, 31), # 31. dec Nytårsaften

        # These days have a date depending on easter
        easter_sunday - timedelta(days=7), # Palmesøndag
        easter_sunday - timedelta(days=3), # Skærtorsdag
        easter_sunday - timedelta(days=2), # Langfredag
        easter_sunday, # Easter Sunday
        easter_sunday + timedelta(days=1), # Easter Monday (2. Påskedag)
        easter_sunday + timedelta(days=26), # Great praior day (St. Bededag)
        easter_sunday + timedelta(days=39), # Kristi Himmelfart
        easter_sunday + timedelta(days=49), # Pentecost Sunday Pinsedag (7th Sunday after Easter)
        easter_sunday + timedelta(days=50), # 1. Pinsedag
    ]
    return holidays


def is_date_a_business_day(current_date=date.today()):
    weekday = current_date.weekday()
    if weekday > 4: # saturday=5, sunday = 6
        return False
    holidays = get_holidays(current_date.year)
    if current_date in holidays:
        return False
    return True


def date_by_adding_business_days(start_date=date.today(), add_days=1):
    current_date = start_date
    business_days_to_add = add_days
    while business_days_to_add > 0:
        current_date += timedelta(days=1)
        if not is_date_a_business_day(current_date):
            continue
        business_days_to_add -= 1
    return current_date


def date_by_substracting_business_days(start_date=date.today(), add_days=1):
    current_date = start_date
    business_days_to_add = add_days
    while business_days_to_add > 0:
        current_date -= timedelta(days=1)
        if not is_date_a_business_day(current_date):
            continue
        business_days_to_add -= 1
    return current_date


def get_next_business_day(current_date=date.today()):
    return date_by_adding_business_days(current_date, 1)


def get_previous_business_day(current_date=date.today()):
    return date_by_substracting_business_days(current_date, 1)

David Winterbottom

unread,
Aug 11, 2014, 4:57:25 PM8/11/14
to django-oscar
Thanks for sharing these Michael - I'm sure they'll be useful.

One suggestion: unformatted code is quite hard to read in an email - I suggest you save your code snippets as a gist (https://gist.github.com/) and send the link. That will make it easier for others to digest.


--
https://github.com/tangentlabs/django-oscar
http://django-oscar.readthedocs.org/en/latest/
https://twitter.com/django_oscar
---
You received this message because you are subscribed to the Google Groups "django-oscar" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-oscar...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-oscar.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-oscar/629cd943-b664-4135-9bae-599fc87ba4a5%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
David Winterbottom
Technical Director

Tangent Snowball
84-86 Great Portland Street
London W1W 7NR
England, UK

Michael Lind Hjulskov

unread,
Aug 12, 2014, 3:31:30 AM8/12/14
to django...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages