jpayne@68: # -*- coding: utf-8 -*- jpayne@68: import datetime jpayne@68: import calendar jpayne@68: jpayne@68: import operator jpayne@68: from math import copysign jpayne@68: jpayne@68: from six import integer_types jpayne@68: from warnings import warn jpayne@68: jpayne@68: from ._common import weekday jpayne@68: jpayne@68: MO, TU, WE, TH, FR, SA, SU = weekdays = tuple(weekday(x) for x in range(7)) jpayne@68: jpayne@68: __all__ = ["relativedelta", "MO", "TU", "WE", "TH", "FR", "SA", "SU"] jpayne@68: jpayne@68: jpayne@68: class relativedelta(object): jpayne@68: """ jpayne@68: The relativedelta type is designed to be applied to an existing datetime and jpayne@68: can replace specific components of that datetime, or represents an interval jpayne@68: of time. jpayne@68: jpayne@68: It is based on the specification of the excellent work done by M.-A. Lemburg jpayne@68: in his jpayne@68: `mx.DateTime `_ extension. jpayne@68: However, notice that this type does *NOT* implement the same algorithm as jpayne@68: his work. Do *NOT* expect it to behave like mx.DateTime's counterpart. jpayne@68: jpayne@68: There are two different ways to build a relativedelta instance. The jpayne@68: first one is passing it two date/datetime classes:: jpayne@68: jpayne@68: relativedelta(datetime1, datetime2) jpayne@68: jpayne@68: The second one is passing it any number of the following keyword arguments:: jpayne@68: jpayne@68: relativedelta(arg1=x,arg2=y,arg3=z...) jpayne@68: jpayne@68: year, month, day, hour, minute, second, microsecond: jpayne@68: Absolute information (argument is singular); adding or subtracting a jpayne@68: relativedelta with absolute information does not perform an arithmetic jpayne@68: operation, but rather REPLACES the corresponding value in the jpayne@68: original datetime with the value(s) in relativedelta. jpayne@68: jpayne@68: years, months, weeks, days, hours, minutes, seconds, microseconds: jpayne@68: Relative information, may be negative (argument is plural); adding jpayne@68: or subtracting a relativedelta with relative information performs jpayne@68: the corresponding arithmetic operation on the original datetime value jpayne@68: with the information in the relativedelta. jpayne@68: jpayne@68: weekday: jpayne@68: One of the weekday instances (MO, TU, etc) available in the jpayne@68: relativedelta module. These instances may receive a parameter N, jpayne@68: specifying the Nth weekday, which could be positive or negative jpayne@68: (like MO(+1) or MO(-2)). Not specifying it is the same as specifying jpayne@68: +1. You can also use an integer, where 0=MO. This argument is always jpayne@68: relative e.g. if the calculated date is already Monday, using MO(1) jpayne@68: or MO(-1) won't change the day. To effectively make it absolute, use jpayne@68: it in combination with the day argument (e.g. day=1, MO(1) for first jpayne@68: Monday of the month). jpayne@68: jpayne@68: leapdays: jpayne@68: Will add given days to the date found, if year is a leap jpayne@68: year, and the date found is post 28 of february. jpayne@68: jpayne@68: yearday, nlyearday: jpayne@68: Set the yearday or the non-leap year day (jump leap days). jpayne@68: These are converted to day/month/leapdays information. jpayne@68: jpayne@68: There are relative and absolute forms of the keyword jpayne@68: arguments. The plural is relative, and the singular is jpayne@68: absolute. For each argument in the order below, the absolute form jpayne@68: is applied first (by setting each attribute to that value) and jpayne@68: then the relative form (by adding the value to the attribute). jpayne@68: jpayne@68: The order of attributes considered when this relativedelta is jpayne@68: added to a datetime is: jpayne@68: jpayne@68: 1. Year jpayne@68: 2. Month jpayne@68: 3. Day jpayne@68: 4. Hours jpayne@68: 5. Minutes jpayne@68: 6. Seconds jpayne@68: 7. Microseconds jpayne@68: jpayne@68: Finally, weekday is applied, using the rule described above. jpayne@68: jpayne@68: For example jpayne@68: jpayne@68: >>> from datetime import datetime jpayne@68: >>> from dateutil.relativedelta import relativedelta, MO jpayne@68: >>> dt = datetime(2018, 4, 9, 13, 37, 0) jpayne@68: >>> delta = relativedelta(hours=25, day=1, weekday=MO(1)) jpayne@68: >>> dt + delta jpayne@68: datetime.datetime(2018, 4, 2, 14, 37) jpayne@68: jpayne@68: First, the day is set to 1 (the first of the month), then 25 hours jpayne@68: are added, to get to the 2nd day and 14th hour, finally the jpayne@68: weekday is applied, but since the 2nd is already a Monday there is jpayne@68: no effect. jpayne@68: jpayne@68: """ jpayne@68: jpayne@68: def __init__(self, dt1=None, dt2=None, jpayne@68: years=0, months=0, days=0, leapdays=0, weeks=0, jpayne@68: hours=0, minutes=0, seconds=0, microseconds=0, jpayne@68: year=None, month=None, day=None, weekday=None, jpayne@68: yearday=None, nlyearday=None, jpayne@68: hour=None, minute=None, second=None, microsecond=None): jpayne@68: jpayne@68: if dt1 and dt2: jpayne@68: # datetime is a subclass of date. So both must be date jpayne@68: if not (isinstance(dt1, datetime.date) and jpayne@68: isinstance(dt2, datetime.date)): jpayne@68: raise TypeError("relativedelta only diffs datetime/date") jpayne@68: jpayne@68: # We allow two dates, or two datetimes, so we coerce them to be jpayne@68: # of the same type jpayne@68: if (isinstance(dt1, datetime.datetime) != jpayne@68: isinstance(dt2, datetime.datetime)): jpayne@68: if not isinstance(dt1, datetime.datetime): jpayne@68: dt1 = datetime.datetime.fromordinal(dt1.toordinal()) jpayne@68: elif not isinstance(dt2, datetime.datetime): jpayne@68: dt2 = datetime.datetime.fromordinal(dt2.toordinal()) jpayne@68: jpayne@68: self.years = 0 jpayne@68: self.months = 0 jpayne@68: self.days = 0 jpayne@68: self.leapdays = 0 jpayne@68: self.hours = 0 jpayne@68: self.minutes = 0 jpayne@68: self.seconds = 0 jpayne@68: self.microseconds = 0 jpayne@68: self.year = None jpayne@68: self.month = None jpayne@68: self.day = None jpayne@68: self.weekday = None jpayne@68: self.hour = None jpayne@68: self.minute = None jpayne@68: self.second = None jpayne@68: self.microsecond = None jpayne@68: self._has_time = 0 jpayne@68: jpayne@68: # Get year / month delta between the two jpayne@68: months = (dt1.year - dt2.year) * 12 + (dt1.month - dt2.month) jpayne@68: self._set_months(months) jpayne@68: jpayne@68: # Remove the year/month delta so the timedelta is just well-defined jpayne@68: # time units (seconds, days and microseconds) jpayne@68: dtm = self.__radd__(dt2) jpayne@68: jpayne@68: # If we've overshot our target, make an adjustment jpayne@68: if dt1 < dt2: jpayne@68: compare = operator.gt jpayne@68: increment = 1 jpayne@68: else: jpayne@68: compare = operator.lt jpayne@68: increment = -1 jpayne@68: jpayne@68: while compare(dt1, dtm): jpayne@68: months += increment jpayne@68: self._set_months(months) jpayne@68: dtm = self.__radd__(dt2) jpayne@68: jpayne@68: # Get the timedelta between the "months-adjusted" date and dt1 jpayne@68: delta = dt1 - dtm jpayne@68: self.seconds = delta.seconds + delta.days * 86400 jpayne@68: self.microseconds = delta.microseconds jpayne@68: else: jpayne@68: # Check for non-integer values in integer-only quantities jpayne@68: if any(x is not None and x != int(x) for x in (years, months)): jpayne@68: raise ValueError("Non-integer years and months are " jpayne@68: "ambiguous and not currently supported.") jpayne@68: jpayne@68: # Relative information jpayne@68: self.years = int(years) jpayne@68: self.months = int(months) jpayne@68: self.days = days + weeks * 7 jpayne@68: self.leapdays = leapdays jpayne@68: self.hours = hours jpayne@68: self.minutes = minutes jpayne@68: self.seconds = seconds jpayne@68: self.microseconds = microseconds jpayne@68: jpayne@68: # Absolute information jpayne@68: self.year = year jpayne@68: self.month = month jpayne@68: self.day = day jpayne@68: self.hour = hour jpayne@68: self.minute = minute jpayne@68: self.second = second jpayne@68: self.microsecond = microsecond jpayne@68: jpayne@68: if any(x is not None and int(x) != x jpayne@68: for x in (year, month, day, hour, jpayne@68: minute, second, microsecond)): jpayne@68: # For now we'll deprecate floats - later it'll be an error. jpayne@68: warn("Non-integer value passed as absolute information. " + jpayne@68: "This is not a well-defined condition and will raise " + jpayne@68: "errors in future versions.", DeprecationWarning) jpayne@68: jpayne@68: if isinstance(weekday, integer_types): jpayne@68: self.weekday = weekdays[weekday] jpayne@68: else: jpayne@68: self.weekday = weekday jpayne@68: jpayne@68: yday = 0 jpayne@68: if nlyearday: jpayne@68: yday = nlyearday jpayne@68: elif yearday: jpayne@68: yday = yearday jpayne@68: if yearday > 59: jpayne@68: self.leapdays = -1 jpayne@68: if yday: jpayne@68: ydayidx = [31, 59, 90, 120, 151, 181, 212, jpayne@68: 243, 273, 304, 334, 366] jpayne@68: for idx, ydays in enumerate(ydayidx): jpayne@68: if yday <= ydays: jpayne@68: self.month = idx+1 jpayne@68: if idx == 0: jpayne@68: self.day = yday jpayne@68: else: jpayne@68: self.day = yday-ydayidx[idx-1] jpayne@68: break jpayne@68: else: jpayne@68: raise ValueError("invalid year day (%d)" % yday) jpayne@68: jpayne@68: self._fix() jpayne@68: jpayne@68: def _fix(self): jpayne@68: if abs(self.microseconds) > 999999: jpayne@68: s = _sign(self.microseconds) jpayne@68: div, mod = divmod(self.microseconds * s, 1000000) jpayne@68: self.microseconds = mod * s jpayne@68: self.seconds += div * s jpayne@68: if abs(self.seconds) > 59: jpayne@68: s = _sign(self.seconds) jpayne@68: div, mod = divmod(self.seconds * s, 60) jpayne@68: self.seconds = mod * s jpayne@68: self.minutes += div * s jpayne@68: if abs(self.minutes) > 59: jpayne@68: s = _sign(self.minutes) jpayne@68: div, mod = divmod(self.minutes * s, 60) jpayne@68: self.minutes = mod * s jpayne@68: self.hours += div * s jpayne@68: if abs(self.hours) > 23: jpayne@68: s = _sign(self.hours) jpayne@68: div, mod = divmod(self.hours * s, 24) jpayne@68: self.hours = mod * s jpayne@68: self.days += div * s jpayne@68: if abs(self.months) > 11: jpayne@68: s = _sign(self.months) jpayne@68: div, mod = divmod(self.months * s, 12) jpayne@68: self.months = mod * s jpayne@68: self.years += div * s jpayne@68: if (self.hours or self.minutes or self.seconds or self.microseconds jpayne@68: or self.hour is not None or self.minute is not None or jpayne@68: self.second is not None or self.microsecond is not None): jpayne@68: self._has_time = 1 jpayne@68: else: jpayne@68: self._has_time = 0 jpayne@68: jpayne@68: @property jpayne@68: def weeks(self): jpayne@68: return int(self.days / 7.0) jpayne@68: jpayne@68: @weeks.setter jpayne@68: def weeks(self, value): jpayne@68: self.days = self.days - (self.weeks * 7) + value * 7 jpayne@68: jpayne@68: def _set_months(self, months): jpayne@68: self.months = months jpayne@68: if abs(self.months) > 11: jpayne@68: s = _sign(self.months) jpayne@68: div, mod = divmod(self.months * s, 12) jpayne@68: self.months = mod * s jpayne@68: self.years = div * s jpayne@68: else: jpayne@68: self.years = 0 jpayne@68: jpayne@68: def normalized(self): jpayne@68: """ jpayne@68: Return a version of this object represented entirely using integer jpayne@68: values for the relative attributes. jpayne@68: jpayne@68: >>> relativedelta(days=1.5, hours=2).normalized() jpayne@68: relativedelta(days=+1, hours=+14) jpayne@68: jpayne@68: :return: jpayne@68: Returns a :class:`dateutil.relativedelta.relativedelta` object. jpayne@68: """ jpayne@68: # Cascade remainders down (rounding each to roughly nearest microsecond) jpayne@68: days = int(self.days) jpayne@68: jpayne@68: hours_f = round(self.hours + 24 * (self.days - days), 11) jpayne@68: hours = int(hours_f) jpayne@68: jpayne@68: minutes_f = round(self.minutes + 60 * (hours_f - hours), 10) jpayne@68: minutes = int(minutes_f) jpayne@68: jpayne@68: seconds_f = round(self.seconds + 60 * (minutes_f - minutes), 8) jpayne@68: seconds = int(seconds_f) jpayne@68: jpayne@68: microseconds = round(self.microseconds + 1e6 * (seconds_f - seconds)) jpayne@68: jpayne@68: # Constructor carries overflow back up with call to _fix() jpayne@68: return self.__class__(years=self.years, months=self.months, jpayne@68: days=days, hours=hours, minutes=minutes, jpayne@68: seconds=seconds, microseconds=microseconds, jpayne@68: leapdays=self.leapdays, year=self.year, jpayne@68: month=self.month, day=self.day, jpayne@68: weekday=self.weekday, hour=self.hour, jpayne@68: minute=self.minute, second=self.second, jpayne@68: microsecond=self.microsecond) jpayne@68: jpayne@68: def __add__(self, other): jpayne@68: if isinstance(other, relativedelta): jpayne@68: return self.__class__(years=other.years + self.years, jpayne@68: months=other.months + self.months, jpayne@68: days=other.days + self.days, jpayne@68: hours=other.hours + self.hours, jpayne@68: minutes=other.minutes + self.minutes, jpayne@68: seconds=other.seconds + self.seconds, jpayne@68: microseconds=(other.microseconds + jpayne@68: self.microseconds), jpayne@68: leapdays=other.leapdays or self.leapdays, jpayne@68: year=(other.year if other.year is not None jpayne@68: else self.year), jpayne@68: month=(other.month if other.month is not None jpayne@68: else self.month), jpayne@68: day=(other.day if other.day is not None jpayne@68: else self.day), jpayne@68: weekday=(other.weekday if other.weekday is not None jpayne@68: else self.weekday), jpayne@68: hour=(other.hour if other.hour is not None jpayne@68: else self.hour), jpayne@68: minute=(other.minute if other.minute is not None jpayne@68: else self.minute), jpayne@68: second=(other.second if other.second is not None jpayne@68: else self.second), jpayne@68: microsecond=(other.microsecond if other.microsecond jpayne@68: is not None else jpayne@68: self.microsecond)) jpayne@68: if isinstance(other, datetime.timedelta): jpayne@68: return self.__class__(years=self.years, jpayne@68: months=self.months, jpayne@68: days=self.days + other.days, jpayne@68: hours=self.hours, jpayne@68: minutes=self.minutes, jpayne@68: seconds=self.seconds + other.seconds, jpayne@68: microseconds=self.microseconds + other.microseconds, jpayne@68: leapdays=self.leapdays, jpayne@68: year=self.year, jpayne@68: month=self.month, jpayne@68: day=self.day, jpayne@68: weekday=self.weekday, jpayne@68: hour=self.hour, jpayne@68: minute=self.minute, jpayne@68: second=self.second, jpayne@68: microsecond=self.microsecond) jpayne@68: if not isinstance(other, datetime.date): jpayne@68: return NotImplemented jpayne@68: elif self._has_time and not isinstance(other, datetime.datetime): jpayne@68: other = datetime.datetime.fromordinal(other.toordinal()) jpayne@68: year = (self.year or other.year)+self.years jpayne@68: month = self.month or other.month jpayne@68: if self.months: jpayne@68: assert 1 <= abs(self.months) <= 12 jpayne@68: month += self.months jpayne@68: if month > 12: jpayne@68: year += 1 jpayne@68: month -= 12 jpayne@68: elif month < 1: jpayne@68: year -= 1 jpayne@68: month += 12 jpayne@68: day = min(calendar.monthrange(year, month)[1], jpayne@68: self.day or other.day) jpayne@68: repl = {"year": year, "month": month, "day": day} jpayne@68: for attr in ["hour", "minute", "second", "microsecond"]: jpayne@68: value = getattr(self, attr) jpayne@68: if value is not None: jpayne@68: repl[attr] = value jpayne@68: days = self.days jpayne@68: if self.leapdays and month > 2 and calendar.isleap(year): jpayne@68: days += self.leapdays jpayne@68: ret = (other.replace(**repl) jpayne@68: + datetime.timedelta(days=days, jpayne@68: hours=self.hours, jpayne@68: minutes=self.minutes, jpayne@68: seconds=self.seconds, jpayne@68: microseconds=self.microseconds)) jpayne@68: if self.weekday: jpayne@68: weekday, nth = self.weekday.weekday, self.weekday.n or 1 jpayne@68: jumpdays = (abs(nth) - 1) * 7 jpayne@68: if nth > 0: jpayne@68: jumpdays += (7 - ret.weekday() + weekday) % 7 jpayne@68: else: jpayne@68: jumpdays += (ret.weekday() - weekday) % 7 jpayne@68: jumpdays *= -1 jpayne@68: ret += datetime.timedelta(days=jumpdays) jpayne@68: return ret jpayne@68: jpayne@68: def __radd__(self, other): jpayne@68: return self.__add__(other) jpayne@68: jpayne@68: def __rsub__(self, other): jpayne@68: return self.__neg__().__radd__(other) jpayne@68: jpayne@68: def __sub__(self, other): jpayne@68: if not isinstance(other, relativedelta): jpayne@68: return NotImplemented # In case the other object defines __rsub__ jpayne@68: return self.__class__(years=self.years - other.years, jpayne@68: months=self.months - other.months, jpayne@68: days=self.days - other.days, jpayne@68: hours=self.hours - other.hours, jpayne@68: minutes=self.minutes - other.minutes, jpayne@68: seconds=self.seconds - other.seconds, jpayne@68: microseconds=self.microseconds - other.microseconds, jpayne@68: leapdays=self.leapdays or other.leapdays, jpayne@68: year=(self.year if self.year is not None jpayne@68: else other.year), jpayne@68: month=(self.month if self.month is not None else jpayne@68: other.month), jpayne@68: day=(self.day if self.day is not None else jpayne@68: other.day), jpayne@68: weekday=(self.weekday if self.weekday is not None else jpayne@68: other.weekday), jpayne@68: hour=(self.hour if self.hour is not None else jpayne@68: other.hour), jpayne@68: minute=(self.minute if self.minute is not None else jpayne@68: other.minute), jpayne@68: second=(self.second if self.second is not None else jpayne@68: other.second), jpayne@68: microsecond=(self.microsecond if self.microsecond jpayne@68: is not None else jpayne@68: other.microsecond)) jpayne@68: jpayne@68: def __abs__(self): jpayne@68: return self.__class__(years=abs(self.years), jpayne@68: months=abs(self.months), jpayne@68: days=abs(self.days), jpayne@68: hours=abs(self.hours), jpayne@68: minutes=abs(self.minutes), jpayne@68: seconds=abs(self.seconds), jpayne@68: microseconds=abs(self.microseconds), jpayne@68: leapdays=self.leapdays, jpayne@68: year=self.year, jpayne@68: month=self.month, jpayne@68: day=self.day, jpayne@68: weekday=self.weekday, jpayne@68: hour=self.hour, jpayne@68: minute=self.minute, jpayne@68: second=self.second, jpayne@68: microsecond=self.microsecond) jpayne@68: jpayne@68: def __neg__(self): jpayne@68: return self.__class__(years=-self.years, jpayne@68: months=-self.months, jpayne@68: days=-self.days, jpayne@68: hours=-self.hours, jpayne@68: minutes=-self.minutes, jpayne@68: seconds=-self.seconds, jpayne@68: microseconds=-self.microseconds, jpayne@68: leapdays=self.leapdays, jpayne@68: year=self.year, jpayne@68: month=self.month, jpayne@68: day=self.day, jpayne@68: weekday=self.weekday, jpayne@68: hour=self.hour, jpayne@68: minute=self.minute, jpayne@68: second=self.second, jpayne@68: microsecond=self.microsecond) jpayne@68: jpayne@68: def __bool__(self): jpayne@68: return not (not self.years and jpayne@68: not self.months and jpayne@68: not self.days and jpayne@68: not self.hours and jpayne@68: not self.minutes and jpayne@68: not self.seconds and jpayne@68: not self.microseconds and jpayne@68: not self.leapdays and jpayne@68: self.year is None and jpayne@68: self.month is None and jpayne@68: self.day is None and jpayne@68: self.weekday is None and jpayne@68: self.hour is None and jpayne@68: self.minute is None and jpayne@68: self.second is None and jpayne@68: self.microsecond is None) jpayne@68: # Compatibility with Python 2.x jpayne@68: __nonzero__ = __bool__ jpayne@68: jpayne@68: def __mul__(self, other): jpayne@68: try: jpayne@68: f = float(other) jpayne@68: except TypeError: jpayne@68: return NotImplemented jpayne@68: jpayne@68: return self.__class__(years=int(self.years * f), jpayne@68: months=int(self.months * f), jpayne@68: days=int(self.days * f), jpayne@68: hours=int(self.hours * f), jpayne@68: minutes=int(self.minutes * f), jpayne@68: seconds=int(self.seconds * f), jpayne@68: microseconds=int(self.microseconds * f), jpayne@68: leapdays=self.leapdays, jpayne@68: year=self.year, jpayne@68: month=self.month, jpayne@68: day=self.day, jpayne@68: weekday=self.weekday, jpayne@68: hour=self.hour, jpayne@68: minute=self.minute, jpayne@68: second=self.second, jpayne@68: microsecond=self.microsecond) jpayne@68: jpayne@68: __rmul__ = __mul__ jpayne@68: jpayne@68: def __eq__(self, other): jpayne@68: if not isinstance(other, relativedelta): jpayne@68: return NotImplemented jpayne@68: if self.weekday or other.weekday: jpayne@68: if not self.weekday or not other.weekday: jpayne@68: return False jpayne@68: if self.weekday.weekday != other.weekday.weekday: jpayne@68: return False jpayne@68: n1, n2 = self.weekday.n, other.weekday.n jpayne@68: if n1 != n2 and not ((not n1 or n1 == 1) and (not n2 or n2 == 1)): jpayne@68: return False jpayne@68: return (self.years == other.years and jpayne@68: self.months == other.months and jpayne@68: self.days == other.days and jpayne@68: self.hours == other.hours and jpayne@68: self.minutes == other.minutes and jpayne@68: self.seconds == other.seconds and jpayne@68: self.microseconds == other.microseconds and jpayne@68: self.leapdays == other.leapdays and jpayne@68: self.year == other.year and jpayne@68: self.month == other.month and jpayne@68: self.day == other.day and jpayne@68: self.hour == other.hour and jpayne@68: self.minute == other.minute and jpayne@68: self.second == other.second and jpayne@68: self.microsecond == other.microsecond) jpayne@68: jpayne@68: def __hash__(self): jpayne@68: return hash(( jpayne@68: self.weekday, jpayne@68: self.years, jpayne@68: self.months, jpayne@68: self.days, jpayne@68: self.hours, jpayne@68: self.minutes, jpayne@68: self.seconds, jpayne@68: self.microseconds, jpayne@68: self.leapdays, jpayne@68: self.year, jpayne@68: self.month, jpayne@68: self.day, jpayne@68: self.hour, jpayne@68: self.minute, jpayne@68: self.second, jpayne@68: self.microsecond, jpayne@68: )) jpayne@68: jpayne@68: def __ne__(self, other): jpayne@68: return not self.__eq__(other) jpayne@68: jpayne@68: def __div__(self, other): jpayne@68: try: jpayne@68: reciprocal = 1 / float(other) jpayne@68: except TypeError: jpayne@68: return NotImplemented jpayne@68: jpayne@68: return self.__mul__(reciprocal) jpayne@68: jpayne@68: __truediv__ = __div__ jpayne@68: jpayne@68: def __repr__(self): jpayne@68: l = [] jpayne@68: for attr in ["years", "months", "days", "leapdays", jpayne@68: "hours", "minutes", "seconds", "microseconds"]: jpayne@68: value = getattr(self, attr) jpayne@68: if value: jpayne@68: l.append("{attr}={value:+g}".format(attr=attr, value=value)) jpayne@68: for attr in ["year", "month", "day", "weekday", jpayne@68: "hour", "minute", "second", "microsecond"]: jpayne@68: value = getattr(self, attr) jpayne@68: if value is not None: jpayne@68: l.append("{attr}={value}".format(attr=attr, value=repr(value))) jpayne@68: return "{classname}({attrs})".format(classname=self.__class__.__name__, jpayne@68: attrs=", ".join(l)) jpayne@68: jpayne@68: jpayne@68: def _sign(x): jpayne@68: return int(copysign(1, x)) jpayne@68: jpayne@68: # vim:ts=4:sw=4:et