Exemple #1
0
 def __init__(self, dt1=None, dt2=None, bdays=None, holidays=None,
              bhours=None, bminutes=None, bseconds=None,
              btstart=None, btend=None, weekdays_off=None, *args, **kwargs):
     self.holidays = holidays
     if self.holidays is None:
         self.holidays = getattr(relativedelta, 'holidays', ())
     self.weekdays_off = weekdays_off
     if self.weekdays_off is None:
         self.weekdays_off = getattr(relativedelta, 'weekdays_off', (5, 6))
     self.btstart = btstart
     if self.btstart is None:
         self.btstart = getattr(relativedelta, 'btstart', time(9))
     self.btend = btend
     if self.btend is None:
         self.btend = getattr(relativedelta, 'btend', time(17))
     if dt1 and dt2:
         # Convert to datetime objects
         dt1 = parse(dt1)
         dt2 = parse(dt2)
         if isinstance(dt1, date) and not isinstance(dt1, datetime):
             dt1 = datetime.combine(dt1, datetime.min.time())
         elif isinstance(dt1, time):
             dt1 = datetime.combine(datetime.now(), dt1)
         if isinstance(dt2, date) and not isinstance(dt2, datetime):
             dt2 = datetime.combine(dt2, datetime.min.time())
         elif isinstance(dt2, time):
             dt2 = datetime.combine(datetime.now(), dt2)
         # Call super init before setting self.bdays to avoid base __radd__
         # from calling child __add__ and creating infinite loop
         rd.__init__(self, dt1, dt2, *args, **kwargs)
         c = defaultdict(int)
         d1 = max(dt1, dt2)
         d2 = min(dt1, dt2)
         if d1.weekday() in self.weekdays_off or d1 in self.holidays:
             c['bdays'] += 1
         for attr in ('bhours', 'bminutes', 'bseconds'):
             while getattr(d1, attr[1:-1]) != getattr(d2, attr[1:-1]):
                 d2 += rd(**{attr[1:]: +1})
                 if d2.time() >= self.btstart and d2.time() < self.btend:
                     c[attr] += 1
         while d1 > d2:
             d2 += rd(days=+1)
             if d2.weekday() not in self.weekdays_off and d2 not in self.holidays:
                 c['bdays'] += 1
         self.bdays = c['bdays']
         self.bhours = c['bhours']
         self.bminutes = c['bminutes']
         self.bseconds = c['bseconds']
         if dt2 > dt1:
             self.bdays *= -1
             self.bhours *= -1
             self.bminutes *= -1
             self.bseconds *= -1
     else:
         self.bdays = bdays
         self.bhours = bhours
         self.bminutes = bminutes
         self.bseconds = bseconds
         bd = rd(datetime.combine(datetime.now(), self.btend),
                 datetime.combine(datetime.now(), self.btstart))
         if isinstance(self.bdays, float):
             self.bhours = self.bhours or 0
             self.bhours += (self.bdays % 1) * \
                            (bd.hours + bd.minutes / 60 +
                             bd.seconds / 60 / 60)
             self.bdays = int(math.floor(self.bdays))
             if self.bdays == 0:
                 self.bdays = None
         if isinstance(self.bhours, float):
             self.bminutes = self.bminutes or 0
             self.bminutes += (self.bhours % 1) * 60
             self.bhours = int(math.floor(self.bhours))
             if self.bhours == 0:
                 self.bhours = None
         if isinstance(self.bminutes, float):
             self.bseconds = self.bseconds or 0
             self.bseconds += int((self.bminutes % 1) * 60)
             self.bminutes = int(math.floor(self.bminutes))
             if self.bminutes == 0:
                 self.bminutes = None
         rd.__init__(self, dt1, dt2, *args, **kwargs)
Exemple #2
0
    def __init__(self,
                 dt1=None,
                 dt2=None,
                 bdays=None,
                 bhours=None,
                 bminutes=None,
                 bseconds=None,
                 holidays=None,
                 workdays=None,
                 btstart=None,
                 btend=None,
                 *args,
                 **kwargs):
        self.holidays = holidays
        if self.holidays is None:
            self.holidays = bdateutil.HOLIDAYS
        self.workdays = workdays
        if self.workdays is None:
            self.workdays = bdateutil.WORKDAYS
        if not self.workdays or self.workdays[0] not in range(7):
            raise ValueError("workdays must contain integers 0-6")
        self.btstart = btstart
        if self.btstart is None:
            self.btstart = bdateutil.BTSTART
        self.btend = btend
        if self.btend is None:
            self.btend = bdateutil.BTEND
        if not isinstance(self.btstart, time):
            raise TypeError("btstart must be of type time")
        if not isinstance(self.btend, time):
            raise TypeError("btend must be of type time")
        if self.btstart >= self.btend:
            raise ValueError("btend must be greater than btstart")
        if dt1 and dt2:
            # Convert to datetime objects
            dt1 = parse(dt1)
            dt2 = parse(dt2)
            if isinstance(dt1, date) and not isinstance(dt1, datetime):
                dt1 = datetime.combine(dt1, datetime.min.time())
            elif isinstance(dt1, time):
                dt1 = datetime.combine(datetime.now(), dt1)
            if isinstance(dt2, date) and not isinstance(dt2, datetime):
                dt2 = datetime.combine(dt2, datetime.min.time())
            elif isinstance(dt2, time):
                dt2 = datetime.combine(datetime.now(), dt2)
            # Call super init before setting self.bdays to avoid base __radd__
            # from calling child __add__ and creating infinite loop
            rd.__init__(self, dt1, dt2, *args, **kwargs)
            c = defaultdict(int)
            d1 = max(dt1, dt2)
            d2 = min(dt1, dt2)
            while d2.weekday() not in self.workdays or d2 not in self.holidays:
                d2 += rd(days=+1)
            for attr in ('bhours', 'bminutes', 'bseconds'):
                while getattr(d1, attr[1:-1]) != getattr(d2, attr[1:-1]):
                    d2 += rd(**{attr[1:]: +1})
                    if d2.weekday() not in self.workdays \
                            or d2 not in self.holidays:
                        d2 += rd(days=+1)
                    if d2.time() >= self.btstart and d2.time() < self.btend:
                        c[attr] += 1
            while d1 >= d2:
                if d2.weekday() in self.workdays and d2 not in self.holidays:
                    c['bdays'] += 1
                d2 += rd(days=+1)

            self.bdays = c['bdays']
            self.bhours = c['bhours']
            self.bminutes = c['bminutes']
            self.bseconds = c['bseconds']
            if dt2 > dt1:
                self.bdays *= -1
                self.bhours *= -1
                self.bminutes *= -1
                self.bseconds *= -1
        else:
            self.bdays = bdays
            self.bhours = bhours
            self.bminutes = bminutes
            self.bseconds = bseconds
            bd = rd(datetime.combine(datetime.now(), self.btend),
                    datetime.combine(datetime.now(), self.btstart))
            if isinstance(self.bdays, float):
                self.bhours = self.bhours or 0
                self.bhours += (self.bdays % 1) * \
                               (bd.hours + bd.minutes / 60 +
                                bd.seconds / 60 / 60)
                self.bdays = int(math.floor(self.bdays))
                if self.bdays == 0:
                    self.bdays = None
            if isinstance(self.bhours, float):
                self.bminutes = self.bminutes or 0
                self.bminutes += (self.bhours % 1) * 60
                self.bhours = int(math.floor(self.bhours))
                if self.bhours == 0:
                    self.bhours = None
            if isinstance(self.bminutes, float):
                self.bseconds = self.bseconds or 0
                self.bseconds += int((self.bminutes % 1) * 60)
                self.bminutes = int(math.floor(self.bminutes))
                if self.bminutes == 0:
                    self.bminutes = None
            rd.__init__(self, dt1, dt2, *args, **kwargs)
Exemple #3
0
 def __init__(self, dt1=None, dt2=None, bdays=None, holidays=None,
              bhours=None, bminutes=None, bseconds=None,
              btstart=None, btend=None, *args, **kwargs):
     self.holidays = holidays
     if self.holidays is None:
         self.holidays = getattr(relativedelta, 'holidays', ())
     self.btstart = btstart
     if self.btstart is None:
         self.btstart = getattr(relativedelta, 'btstart', time(9))
     self.btend = btend
     if self.btend is None:
         self.btend = getattr(relativedelta, 'btend', time(17))
     if dt1 and dt2:
         # Convert to datetime objects
         dt1 = parse(dt1)
         dt2 = parse(dt2)
         if isinstance(dt1, date) and not isinstance(dt1, datetime):
             dt1 = datetime.combine(dt1, datetime.min.time())
         elif isinstance(dt1, time):
             dt1 = datetime.combine(datetime.now(), dt1)
         if isinstance(dt2, date) and not isinstance(dt2, datetime):
             dt2 = datetime.combine(dt2, datetime.min.time())
         elif isinstance(dt2, time):
             dt2 = datetime.combine(datetime.now(), dt2)
         # Call super init before setting self.bdays to avoid base __radd__
         # from calling child __add__ and creating infinite loop
         rd.__init__(self, dt1, dt2, *args, **kwargs)
         c = defaultdict(int)
         d1 = max(dt1, dt2)
         d2 = min(dt1, dt2)
         if d1.weekday() in (5, 6) or d1 in self.holidays:
             c['bdays'] += 1
         for attr in ('bhours', 'bminutes', 'bseconds'):
             while getattr(d1, attr[1:-1]) != getattr(d2, attr[1:-1]):
                 d2 += rd(**{attr[1:]: +1})
                 if d2.time() >= self.btstart and d2.time() < self.btend:
                     c[attr] += 1
         while d1 > d2:
             d2 += rd(days=+1)
             if d2.weekday() not in (5, 6) and d2 not in self.holidays:
                 c['bdays'] += 1
         self.bdays = c['bdays']
         self.bhours = c['bhours']
         self.bminutes = c['bminutes']
         self.bseconds = c['bseconds']
         if dt2 > dt1:
             self.bdays *= -1
             self.bhours *= -1
             self.bminutes *= -1
             self.bseconds *= -1
     else:
         self.bdays = bdays
         self.bhours = bhours
         self.bminutes = bminutes
         self.bseconds = bseconds
         bd = rd(datetime.combine(datetime.now(), self.btend),
                 datetime.combine(datetime.now(), self.btstart))
         if isinstance(self.bdays, float):
             self.bhours = self.bhours or 0
             self.bhours += (self.bdays % 1) * \
                            (bd.hours + bd.minutes / 60 +
                             bd.seconds / 60 / 60)
             self.bdays = int(math.floor(self.bdays))
             if self.bdays == 0:
                 self.bdays = None
         if isinstance(self.bhours, float):
             self.bminutes = self.bminutes or 0
             self.bminutes += (self.bhours % 1) * 60
             self.bhours = int(math.floor(self.bhours))
             if self.bhours == 0:
                 self.bhours = None
         if isinstance(self.bminutes, float):
             self.bseconds = self.bseconds or 0
             self.bseconds += int((self.bminutes % 1) * 60)
             self.bminutes = int(math.floor(self.bminutes))
             if self.bminutes == 0:
                 self.bminutes = None
         rd.__init__(self, dt1, dt2, *args, **kwargs)
Exemple #4
0
 def __init__(self, dt1=None, dt2=None, bdays=None,
              bhours=None, bminutes=None, bseconds=None,
              holidays=None, workdays=None, btstart=None, btend=None,
              *args, **kwargs):
     self.holidays = holidays
     if self.holidays is None:
         self.holidays = bdateutil.HOLIDAYS
     self.workdays = workdays
     if self.workdays is None:
         self.workdays = bdateutil.WORKDAYS
     if not self.workdays or self.workdays[0] not in range(7):
         raise ValueError("workdays must contain integers 0-6")
     self.btstart = btstart
     if self.btstart is None:
         self.btstart = bdateutil.BTSTART
     self.btend = btend
     if self.btend is None:
         self.btend = bdateutil.BTEND
     if not isinstance(self.btstart, time):
         raise TypeError("btstart must be of type time")
     if not isinstance(self.btend, time):
         raise TypeError("btend must be of type time")
     if self.btstart >= self.btend:
         raise ValueError("btend must be greater than btstart")
     if dt1 and dt2:
         # Convert to datetime objects
         dt1 = parse(dt1)
         dt2 = parse(dt2)
         if isinstance(dt1, date) and not isinstance(dt1, datetime):
             dt1 = datetime.combine(dt1, datetime.min.time())
         elif isinstance(dt1, time):
             dt1 = datetime.combine(datetime.now(), dt1)
         if isinstance(dt2, date) and not isinstance(dt2, datetime):
             dt2 = datetime.combine(dt2, datetime.min.time())
         elif isinstance(dt2, time):
             dt2 = datetime.combine(datetime.now(), dt2)
         # Call super init before setting self.bdays to avoid base __radd__
         # from calling child __add__ and creating infinite loop
         rd.__init__(self, dt1, dt2, *args, **kwargs)
         c = defaultdict(int)
         d1 = max(dt1, dt2)
         d2 = min(dt1, dt2)
         if d1.weekday() not in self.workdays or d1 in self.holidays:
             c['bdays'] += 1
         for attr in ('bhours', 'bminutes', 'bseconds'):
             while getattr(d1, attr[1:-1]) != getattr(d2, attr[1:-1]):
                 d2 += rd(**{attr[1:]: +1})
                 if d2.time() >= self.btstart and d2.time() < self.btend:
                     c[attr] += 1
         while d1 > d2:
             d2 += rd(days=+1)
             if d2.weekday() in self.workdays and d2 not in self.holidays:
                 c['bdays'] += 1
         self.bdays = c['bdays']
         self.bhours = c['bhours']
         self.bminutes = c['bminutes']
         self.bseconds = c['bseconds']
         if dt2 > dt1:
             self.bdays *= -1
             self.bhours *= -1
             self.bminutes *= -1
             self.bseconds *= -1
     else:
         self.bdays = bdays
         self.bhours = bhours
         self.bminutes = bminutes
         self.bseconds = bseconds
         bd = rd(datetime.combine(datetime.now(), self.btend),
                 datetime.combine(datetime.now(), self.btstart))
         if isinstance(self.bdays, float):
             self.bhours = self.bhours or 0
             self.bhours += (self.bdays % 1) * \
                            (bd.hours + bd.minutes / 60 +
                             bd.seconds / 60 / 60)
             self.bdays = int(math.floor(self.bdays))
             if self.bdays == 0:
                 self.bdays = None
         if isinstance(self.bhours, float):
             self.bminutes = self.bminutes or 0
             self.bminutes += (self.bhours % 1) * 60
             self.bhours = int(math.floor(self.bhours))
             if self.bhours == 0:
                 self.bhours = None
         if isinstance(self.bminutes, float):
             self.bseconds = self.bseconds or 0
             self.bseconds += int((self.bminutes % 1) * 60)
             self.bminutes = int(math.floor(self.bminutes))
             if self.bminutes == 0:
                 self.bminutes = None
         rd.__init__(self, dt1, dt2, *args, **kwargs)