Exemple #1
0
 def __add__(self, other):
     if isinstance(other, relativedelta):
         ret = rd.__add__(self, other)
         ret.__class__ = relativedelta
         for attr in ('bdays', 'bhours', 'bminutes', 'bseconds'):
             if getattr(self, attr, None) is not None:
                 if getattr(other, attr, None) is not None:
                     setattr(ret, attr,
                             getattr(self, attr) + getattr(other, attr))
                 else:
                     setattr(ret, attr, getattr(self, attr))
             elif getattr(other, attr, None) is not None:
                 setattr(ret, attr, getattr(other, attr))
         return ret
     ret = parse(other)
     # If we are adding any time (not just dates) the ret object to return
     # must be a datetime object; a date object will not work
     if not isinstance(ret, datetime) \
             and (self.bhours or self.bminutes or self.bseconds):
         ret = datetime.combine(ret, datetime.min.time())
     for attr in ('bseconds', 'bminutes', 'bhours', 'bdays'):
         if getattr(self, attr, None) is not None:
             while ret.weekday() in self.weekdays_off or ret in self.holidays:
                 ret += rd(days=+1)
             while attr != "bdays" and \
                     (ret.time() < self.btstart or
                      ret.time() >= self.btend):
                 ret += rd(**{attr[1:]: +1})
             i = getattr(self, attr)
             a = +1 if i > 0 else -1
             while i != 0:
                 ret += rd(**{attr[1:]: a})
                 while ret.weekday() in self.weekdays_off or ret in self.holidays:
                     ret += rd(days=a)
                 while attr != "bdays" and \
                         (ret.time() < self.btstart or
                          ret.time() >= self.btend):
                     ret += rd(**{attr[1:]: a})
                 i -= a
     return rd.__add__(self, ret)
Exemple #2
0
 def __add__(self, other):
     if isinstance(other, relativedelta):
         ret = rd.__add__(self, other)
         ret.__class__ = relativedelta
         for attr in ('bdays', 'bhours', 'bminutes', 'bseconds'):
             if getattr(self, attr, None) is not None:
                 if getattr(other, attr, None) is not None:
                     setattr(ret, attr,
                             getattr(self, attr) + getattr(other, attr))
                 else:
                     setattr(ret, attr, getattr(self, attr))
             elif getattr(other, attr, None) is not None:
                 setattr(ret, attr, getattr(other, attr))
         return ret
     ret = parse(other)
     # If we are adding any time (not just dates) the ret object to return
     # must be a datetime object; a date object will not work
     if not isinstance(ret, datetime) \
             and (self.bhours or self.bminutes or self.bseconds):
         ret = datetime.combine(ret, datetime.min.time())
     for attr in ('bseconds', 'bminutes', 'bhours', 'bdays'):
         if getattr(self, attr, None) is not None:
             while ret.weekday() in (5, 6) or ret in self.holidays:
                 ret += rd(days=+1)
             while attr != "bdays" and \
                     (ret.time() < self.btstart or
                      ret.time() >= self.btend):
                 ret += rd(**{attr[1:]: +1})
             i = getattr(self, attr)
             a = +1 if i > 0 else -1
             while i != 0:
                 ret += rd(**{attr[1:]: a})
                 while ret.weekday() in (5, 6) or ret in self.holidays:
                     ret += rd(days=a)
                 while attr != "bdays" and \
                         (ret.time() < self.btstart or
                          ret.time() >= self.btend):
                     ret += rd(**{attr[1:]: a})
                 i -= a
     return rd.__add__(self, ret)
Exemple #3
0
 def __add__(self, other):
     other_class = other.__class__
     if isinstance(other, relativedelta):
         ret = rd.__add__(self, other)
         ret.__class__ = self.__class__
         for attr in ('bdays', 'bhours', 'bminutes', 'bseconds'):
             if getattr(self, attr, None) is not None:
                 if getattr(other, attr, None) is not None:
                     setattr(ret, attr,
                             getattr(self, attr) + getattr(other, attr))
                 else:
                     setattr(ret, attr, getattr(self, attr))
             elif getattr(other, attr, None) is not None:
                 setattr(ret, attr, getattr(other, attr))
         return ret
     # If we are adding any time (not just dates) the ret object to return
     # must be a datetime object; a date object will not work
     if isinstance(other, date) and not isinstance(other, datetime) \
             and (getattr(self, 'bhours', 0) or
                  getattr(self, 'bminutes', 0) or
                  getattr(self, 'bseconds', 0) or
                  getattr(self, 'hours', 0) or
                  getattr(self, 'minutes', 0) or
                  getattr(self, 'seconds', 0) or
                  getattr(self, 'microseconds', 0) or
                  getattr(self, 'hour', 0) or
                  getattr(self, 'minute', 0) or
                  getattr(self, 'second', 0) or
                  getattr(self, 'microsecond', 0)):
         other = datetime.combine(other, datetime.min.time())
         other_class = other.__class__
     if isinstance(other, time):
         other = datetime.combine(date.today(), other)
     for attr in ('bseconds', 'bminutes', 'bhours', 'bdays'):
         if getattr(self, attr, None) is not None:
             while other.weekday() not in self.workdays \
                     or other not in self.holidays:
                 other += rd(days=+1)
             while attr != "bdays" and \
                     (other.time() < self.btstart or
                      other.time() >= self.btend):
                 other += rd(**{attr[1:]: +1})
             i = getattr(self, attr)
             a = +1 if i > 0 else -1
             while i != 0:
                 other += rd(**{attr[1:]: a})
                 while other.weekday() not in self.workdays \
                         or other not in self.holidays:
                     other += rd(days=a)
                 while attr != "bdays" and \
                         (other.time() < self.btstart or
                          other.time() >= self.btend):
                     other += rd(**{attr[1:]: a})
                 i -= a
     ret = rd.__add__(self, other)
     # Ensure when relativedelta is added to `other` object that it returns
     # the same type of object as `other`
     if ret.__class__ != other_class:
         if issubclass(other_class, datetime):
             ret = other_class(*(ret.timetuple()[:6] +
                                 (ret.microsecond, ret.tzinfo)))
         elif issubclass(other_class, date):
             ret = other_class(*(ret.timetuple()[:3]))
         elif issubclass(other_class, time):
             ret = other_class(ret.hour, ret.minute, ret.second,
                               ret.microsecond, ret.tzinfo)
     return ret
Exemple #4
0
 def __add__(self, other):
     other_class = other.__class__
     if isinstance(other, relativedelta):
         ret = rd.__add__(self, other)
         ret.__class__ = self.__class__
         for attr in ('bdays', 'bhours', 'bminutes', 'bseconds'):
             if getattr(self, attr, None) is not None:
                 if getattr(other, attr, None) is not None:
                     setattr(ret, attr,
                             getattr(self, attr) + getattr(other, attr))
                 else:
                     setattr(ret, attr, getattr(self, attr))
             elif getattr(other, attr, None) is not None:
                 setattr(ret, attr, getattr(other, attr))
         return ret
     # If we are adding any time (not just dates) the ret object to return
     # must be a datetime object; a date object will not work
     if isinstance(other, date) and not isinstance(other, datetime) \
             and (getattr(self, 'bhours', 0) or
                  getattr(self, 'bminutes', 0) or
                  getattr(self, 'bseconds', 0) or
                  getattr(self, 'hours', 0) or
                  getattr(self, 'minutes', 0) or
                  getattr(self, 'seconds', 0) or
                  getattr(self, 'microseconds', 0) or
                  getattr(self, 'hour', 0) or
                  getattr(self, 'minute', 0) or
                  getattr(self, 'second', 0) or
                  getattr(self, 'microsecond', 0)):
         other = datetime.combine(other, datetime.min.time())
         other_class = other.__class__
     if isinstance(other, time):
         other = datetime.combine(date.today(), other)
     for attr in ('bseconds', 'bminutes', 'bhours', 'bdays'):
         if getattr(self, attr, None) is not None:
             while other.weekday() not in self.workdays \
                     or other in self.holidays:
                 other += rd(days=+1)
             while attr != "bdays" and \
                     (other.time() < self.btstart or
                      other.time() >= self.btend):
                 other += rd(**{attr[1:]: +1})
             i = getattr(self, attr)
             a = +1 if i > 0 else -1
             while i != 0:
                 other += rd(**{attr[1:]: a})
                 while other.weekday() not in self.workdays \
                         or other in self.holidays:
                     other += rd(days=a)
                 while attr != "bdays" and \
                         (other.time() < self.btstart or
                          other.time() >= self.btend):
                     other += rd(**{attr[1:]: a})
                 i -= a
     ret = rd.__add__(self, other)
     # Ensure when relativedelta is added to `other` object that it returns
     # the same type of object as `other`
     if ret.__class__ != other_class:
         if issubclass(other_class, datetime):
             ret = other_class(*(ret.timetuple()[:6] +
                                 (ret.microsecond, ret.tzinfo)))
         elif issubclass(other_class, date):
             ret = other_class(*(ret.timetuple()[:3]))
         elif issubclass(other_class, time):
             ret = other_class(ret.hour, ret.minute, ret.second,
                               ret.microsecond, ret.tzinfo)
     return ret