<commit_before>###########################################################
#
# Copyright (c) 2005, Southpaw Technology
#                     All Rights Reserved
#
# PROPRIETARY INFORMATION.  This software is proprietary to
# Southpaw Technology, and is not to be reproduced, transmitted,
# or disclosed in any way without written permission.
#
#
#
import tacticenv

__all__ = ['SPTDate']


from datetime import datetime, timedelta
from dateutil import parser
from dateutil.tz import *

TZLOCAL = tzlocal()
TZUTC = tzutc()
TZGMT = gettz('GMT')

class SPTDate(object):

    def now(cls):
        # utcnow is already converted to GMT
        date = datetime.utcnow()
        date = date.replace(tzinfo=TZGMT)
        date = cls.convert(date)
        return date
    now = classmethod(now)

    def today(cls):
        date = datetime.today()
        return cls.convert(date)
    today = classmethod(today)


    def start_of_today(cls):
        today = datetime.today()
        today = cls.convert(today)
        today = datetime(today.year, today.month, today.day)
        return today
    start_of_today = classmethod(start_of_today)


    def strip_time(cls, date):
        if isinstance(date, basestring):
            date = parser.parse(date)

        date = datetime(date.year, date.month, date.day)
        return date
    strip_time = classmethod(strip_time)


    def strip_timezone(cls, date):
        if isinstance(date, basestring):
            date = parser.parse(date)

        date = date.replace(tzinfo=None)
        return date
    strip_timezone = classmethod(strip_timezone)


    def set_noon(cls, date):
        date = datetime(date.year, date.month, date.day, hour=12, minute=0, second=0)
        return date
    set_noon = classmethod(set_noon)



    def timedelta(cls, **kwargs):
        delta = timedelta(**kwargs)
        return delta
    timedelta = classmethod(timedelta)

    def parse(cls, expression):
        date = parser.parse(expression)
        date = cls.convert(date)
        return date
    parse = classmethod(parse)



    def is_weekday(cls, date):
        if date.weekday() in (5,6):
            return False
        else:
            return True

    is_weekday = classmethod(is_weekday)



    def add_business_days(cls, from_date, add_days, holidays=[]):
        business_days_to_add = add_days
        current_date = from_date
        
        while business_days_to_add >= 0:
            if business_days_to_add > 1:
                current_date += timedelta(days=1)
                weekday = current_date.weekday()
                if weekday >= 5: # sunday = 6
                    continue
                if current_date in holidays:
                    continue
                business_days_to_add -= 1
                if business_days_to_add == 0:
                    break
            else:
                current_date +=timedelta(days=business_days_to_add)
                weekday = current_date.weekday()
                if weekday >= 5 or current_date in holidays:
                    current_date += timedelta(days=1)
                else:
                    break
                business_days_to_add = 0

        return current_date
    add_business_days = classmethod(add_business_days)



    def convert_to_local(cls, date):
        '''convert a time to local time with timezone'''
        if not date:
            return None

        if isinstance(date, basestring):
            try:<commit_msg>Added subtract_business_days function.<commit_after>###########################################################
#
# Copyright (c) 2005, Southpaw Technology
#                     All Rights Reserved
#
# PROPRIETARY INFORMATION.  This software is proprietary to
# Southpaw Technology, and is not to be reproduced, transmitted,
# or disclosed in any way without written permission.
#
#
#
import tacticenv

__all__ = ['SPTDate']


from datetime import datetime, timedelta
from dateutil import parser
from dateutil.tz import *

TZLOCAL = tzlocal()
TZUTC = tzutc()
TZGMT = gettz('GMT')

class SPTDate(object):

    def now(cls):
        # utcnow is already converted to GMT
        date = datetime.utcnow()
        date = date.replace(tzinfo=TZGMT)
        date = cls.convert(date)
        return date
    now = classmethod(now)

    def today(cls):
        date = datetime.today()
        return cls.convert(date)
    today = classmethod(today)


    def start_of_today(cls):
        today = datetime.today()
        today = cls.convert(today)
        today = datetime(today.year, today.month, today.day)
        return today
    start_of_today = classmethod(start_of_today)


    def strip_time(cls, date):
        if isinstance(date, basestring):
            date = parser.parse(date)

        date = datetime(date.year, date.month, date.day)
        return date
    strip_time = classmethod(strip_time)


    def strip_timezone(cls, date):
        if isinstance(date, basestring):
            date = parser.parse(date)

        date = date.replace(tzinfo=None)
        return date
    strip_timezone = classmethod(strip_timezone)


    def set_noon(cls, date):
        date = datetime(date.year, date.month, date.day, hour=12, minute=0, second=0)
        return date
    set_noon = classmethod(set_noon)



    def timedelta(cls, **kwargs):
        delta = timedelta(**kwargs)
        return delta
    timedelta = classmethod(timedelta)

    def parse(cls, expression):
        date = parser.parse(expression)
        date = cls.convert(date)
        return date
    parse = classmethod(parse)



    def is_weekday(cls, date):
        if date.weekday() in (5,6):
            return False
        else:
            return True

    is_weekday = classmethod(is_weekday)



    def add_business_days(cls, from_date, add_days, holidays=[]):
        business_days_to_add = add_days
        current_date = from_date
        
        while business_days_to_add >= 0:
            if business_days_to_add > 1:
                current_date += timedelta(days=1)
                weekday = current_date.weekday()
                if weekday >= 5: # sunday = 6
                    continue
                if current_date in holidays:
                    continue
                business_days_to_add -= 1
                if business_days_to_add == 0:
                    break
            else:
                current_date +=timedelta(days=business_days_to_add)
                weekday = current_date.weekday()
                if weekday >= 5 or current_date in holidays:
                    current_date += timedelta(days=1)
                else:
                    break
                business_days_to_add = 0

        return current_date
    add_business_days = classmethod(add_business_days)


    def subtract_business_days(cls, from_date, sub_days, holidays=[]): 
        business_days_to_sub = sub_days
        current_date = from_date

        while business_days_to_sub >= 0:
            if business_days_to_sub > 1:
                current_date -= timedelta(days=1)
                weekday = current_date.weekday()
                if weekday >= 5: # sunday = 6
                    continue
                if current_date in holidays:
                    continue
                business_days_to_sub -= 1
                if business_days_to_sub == 0:
                    break
            else:
                current_date -=timedelta(days=business_days_to_sub)
                weekday = current_date.weekday()
                if weekday >= 5 or current_date in holidays:
                    current_date -= timedelta(days=1)
                else:
                    break
                business_days_to_sub = 0

        return current_date
    subtract_business_days = classmethod(subtract_business_days)



    def convert_to_local(cls, date):
        '''convert a time to local time with timezone'''
        if not date:
            return None

        if isinstance(date, basestring):
            try:
{'fraction_non_alphanumeric': 0.09284818067754078, 'fraction_numerical': 0.006273525721455458, 'mean_word_length': 1.9887514060742406, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3717694', 'n_tokens_mistral': 2380, 'n_tokens_neox': 2177, 'n_words': 565}