Back to index

python3.2  3.2.2
Public Member Functions | Private Member Functions | Private Attributes | Static Private Attributes
datetime.timedelta Class Reference
Collaboration diagram for datetime.timedelta:
Collaboration graph
[legend]

List of all members.

Public Member Functions

def __new__
def __repr__
def __str__
def total_seconds
def days
def seconds
def microseconds
def __add__
def __sub__
def __rsub__
def __neg__
def __pos__
def __abs__
def __mul__
def __floordiv__
def __truediv__
def __mod__
def __divmod__
def __eq__
def __ne__
def __le__
def __lt__
def __ge__
def __gt__
def __hash__
def __bool__
def __reduce__

Private Member Functions

def _to_microseconds
def _cmp
def _getstate

Private Attributes

 _days
 _seconds
 _microseconds

Static Private Attributes

string __slots__ = '_days'
 __radd__ = __add__
 __rmul__ = __mul__

Detailed Description

Represent the difference between two datetime objects.

Supported operators:

- add, subtract timedelta
- unary plus, minus, abs
- compare to timedelta
- multiply, divide by int/long

In addition, datetime supports subtraction of two datetime objects
returning a timedelta, and addition or subtraction of a datetime
and a timedelta giving a datetime.

Representation: (days, seconds, microseconds).  Why?  Because I
felt like it.

Definition at line 298 of file datetime.py.


Member Function Documentation

Definition at line 510 of file datetime.py.

00510 
00511     def __abs__(self):
00512         if self._days < 0:
00513             return -self
00514         else:
00515             return self

def datetime.timedelta.__add__ (   self,
  other 
)

Definition at line 475 of file datetime.py.

00475 
00476     def __add__(self, other):
00477         if isinstance(other, timedelta):
00478             # for CPython compatibility, we cannot use
00479             # our __class__ here, but need a real timedelta
00480             return timedelta(self._days + other._days,
00481                              self._seconds + other._seconds,
00482                              self._microseconds + other._microseconds)
00483         return NotImplemented

Here is the caller graph for this function:

Definition at line 613 of file datetime.py.

00613 
00614     def __bool__(self):
00615         return (self._days != 0 or
00616                 self._seconds != 0 or
00617                 self._microseconds != 0)

def datetime.timedelta.__divmod__ (   self,
  other 
)

Definition at line 561 of file datetime.py.

00561 
00562     def __divmod__(self, other):
00563         if isinstance(other, timedelta):
00564             q, r = divmod(self._to_microseconds(),
00565                           other._to_microseconds())
00566             return q, timedelta(0, 0, r)
00567         return NotImplemented

Here is the call graph for this function:

def datetime.timedelta.__eq__ (   self,
  other 
)

Definition at line 570 of file datetime.py.

00570 
00571     def __eq__(self, other):
00572         if isinstance(other, timedelta):
00573             return self._cmp(other) == 0
00574         else:
00575             return False

Here is the call graph for this function:

Here is the caller graph for this function:

def datetime.timedelta.__floordiv__ (   self,
  other 
)

Definition at line 534 of file datetime.py.

00534 
00535     def __floordiv__(self, other):
00536         if not isinstance(other, (int, timedelta)):
00537             return NotImplemented
00538         usec = self._to_microseconds()
00539         if isinstance(other, timedelta):
00540             return usec // other._to_microseconds()
00541         if isinstance(other, int):
00542             return timedelta(0, 0, usec // other)

Here is the call graph for this function:

def datetime.timedelta.__ge__ (   self,
  other 
)

Definition at line 594 of file datetime.py.

00594 
00595     def __ge__(self, other):
00596         if isinstance(other, timedelta):
00597             return self._cmp(other) >= 0
00598         else:
00599             _cmperror(self, other)

Here is the call graph for this function:

def datetime.timedelta.__gt__ (   self,
  other 
)

Definition at line 600 of file datetime.py.

00600 
00601     def __gt__(self, other):
00602         if isinstance(other, timedelta):
00603             return self._cmp(other) > 0
00604         else:
00605             _cmperror(self, other)

Here is the call graph for this function:

Definition at line 610 of file datetime.py.

00610 
00611     def __hash__(self):
00612         return hash(self._getstate())

Here is the call graph for this function:

def datetime.timedelta.__le__ (   self,
  other 
)

Definition at line 582 of file datetime.py.

00582 
00583     def __le__(self, other):
00584         if isinstance(other, timedelta):
00585             return self._cmp(other) <= 0
00586         else:
00587             _cmperror(self, other)

Here is the call graph for this function:

def datetime.timedelta.__lt__ (   self,
  other 
)

Definition at line 588 of file datetime.py.

00588 
00589     def __lt__(self, other):
00590         if isinstance(other, timedelta):
00591             return self._cmp(other) < 0
00592         else:
00593             _cmperror(self, other)

Here is the call graph for this function:

def datetime.timedelta.__mod__ (   self,
  other 
)

Definition at line 555 of file datetime.py.

00555 
00556     def __mod__(self, other):
00557         if isinstance(other, timedelta):
00558             r = self._to_microseconds() % other._to_microseconds()
00559             return timedelta(0, 0, r)
00560         return NotImplemented

Here is the call graph for this function:

def datetime.timedelta.__mul__ (   self,
  other 
)

Definition at line 516 of file datetime.py.

00516 
00517     def __mul__(self, other):
00518         if isinstance(other, int):
00519             # for CPython compatibility, we cannot use
00520             # our __class__ here, but need a real timedelta
00521             return timedelta(self._days * other,
00522                              self._seconds * other,
00523                              self._microseconds * other)
00524         if isinstance(other, float):
00525             a, b = other.as_integer_ratio()
00526             return self * a / b
00527         return NotImplemented

def datetime.timedelta.__ne__ (   self,
  other 
)

Definition at line 576 of file datetime.py.

00576 
00577     def __ne__(self, other):
00578         if isinstance(other, timedelta):
00579             return self._cmp(other) != 0
00580         else:
00581             return True

Here is the call graph for this function:

Definition at line 500 of file datetime.py.

00500 
00501     def __neg__(self):
00502         # for CPython compatibility, we cannot use
00503         # our __class__ here, but need a real timedelta
00504         return timedelta(-self._days,
00505                          -self._seconds,
00506                          -self._microseconds)

Here is the caller graph for this function:

def datetime.timedelta.__new__ (   cls,
  days = 0,
  seconds = 0,
  microseconds = 0,
  milliseconds = 0,
  minutes = 0,
  hours = 0,
  weeks = 0 
)

Definition at line 318 of file datetime.py.

00318 
00319                 milliseconds=0, minutes=0, hours=0, weeks=0):
00320         # Doing this efficiently and accurately in C is going to be difficult
00321         # and error-prone, due to ubiquitous overflow possibilities, and that
00322         # C double doesn't have enough bits of precision to represent
00323         # microseconds over 10K years faithfully.  The code here tries to make
00324         # explicit where go-fast assumptions can be relied on, in order to
00325         # guide the C implementation; it's way more convoluted than speed-
00326         # ignoring auto-overflow-to-long idiomatic Python could be.
00327 
00328         # XXX Check that all inputs are ints or floats.
00329 
00330         # Final values, all integer.
00331         # s and us fit in 32-bit signed ints; d isn't bounded.
00332         d = s = us = 0
00333 
00334         # Normalize everything to days, seconds, microseconds.
00335         days += weeks*7
00336         seconds += minutes*60 + hours*3600
00337         microseconds += milliseconds*1000
00338 
00339         # Get rid of all fractions, and normalize s and us.
00340         # Take a deep breath <wink>.
00341         if isinstance(days, float):
00342             dayfrac, days = _math.modf(days)
00343             daysecondsfrac, daysecondswhole = _math.modf(dayfrac * (24.*3600.))
00344             assert daysecondswhole == int(daysecondswhole)  # can't overflow
00345             s = int(daysecondswhole)
00346             assert days == int(days)
00347             d = int(days)
00348         else:
00349             daysecondsfrac = 0.0
00350             d = days
00351         assert isinstance(daysecondsfrac, float)
00352         assert abs(daysecondsfrac) <= 1.0
00353         assert isinstance(d, int)
00354         assert abs(s) <= 24 * 3600
00355         # days isn't referenced again before redefinition
00356 
00357         if isinstance(seconds, float):
00358             secondsfrac, seconds = _math.modf(seconds)
00359             assert seconds == int(seconds)
00360             seconds = int(seconds)
00361             secondsfrac += daysecondsfrac
00362             assert abs(secondsfrac) <= 2.0
00363         else:
00364             secondsfrac = daysecondsfrac
00365         # daysecondsfrac isn't referenced again
00366         assert isinstance(secondsfrac, float)
00367         assert abs(secondsfrac) <= 2.0
00368 
00369         assert isinstance(seconds, int)
00370         days, seconds = divmod(seconds, 24*3600)
00371         d += days
00372         s += int(seconds)    # can't overflow
00373         assert isinstance(s, int)
00374         assert abs(s) <= 2 * 24 * 3600
00375         # seconds isn't referenced again before redefinition
00376 
00377         usdouble = secondsfrac * 1e6
00378         assert abs(usdouble) < 2.1e6    # exact value not critical
00379         # secondsfrac isn't referenced again
00380 
00381         if isinstance(microseconds, float):
00382             microseconds += usdouble
00383             microseconds = round(microseconds, 0)
00384             seconds, microseconds = divmod(microseconds, 1e6)
00385             assert microseconds == int(microseconds)
00386             assert seconds == int(seconds)
00387             days, seconds = divmod(seconds, 24.*3600.)
00388             assert days == int(days)
00389             assert seconds == int(seconds)
00390             d += int(days)
00391             s += int(seconds)   # can't overflow
00392             assert isinstance(s, int)
00393             assert abs(s) <= 3 * 24 * 3600
00394         else:
00395             seconds, microseconds = divmod(microseconds, 1000000)
00396             days, seconds = divmod(seconds, 24*3600)
00397             d += days
00398             s += int(seconds)    # can't overflow
00399             assert isinstance(s, int)
00400             assert abs(s) <= 3 * 24 * 3600
00401             microseconds = float(microseconds)
00402             microseconds += usdouble
00403             microseconds = round(microseconds, 0)
00404         assert abs(s) <= 3 * 24 * 3600
00405         assert abs(microseconds) < 3.1e6
00406 
00407         # Just a little bit of carrying possible for microseconds and seconds.
00408         assert isinstance(microseconds, float)
00409         assert int(microseconds) == microseconds
00410         us = int(microseconds)
00411         seconds, us = divmod(us, 1000000)
00412         s += seconds    # cant't overflow
00413         assert isinstance(s, int)
00414         days, s = divmod(s, 24*3600)
00415         d += days
00416 
00417         assert isinstance(d, int)
00418         assert isinstance(s, int) and 0 <= s < 24*3600
00419         assert isinstance(us, int) and 0 <= us < 1000000
00420 
00421         self = object.__new__(cls)
00422 
00423         self._days = d
00424         self._seconds = s
00425         self._microseconds = us
00426         if abs(d) > 999999999:
00427             raise OverflowError("timedelta # of days is too large: %d" % d)
00428 
00429         return self

Here is the call graph for this function:

Definition at line 507 of file datetime.py.

00507 
00508     def __pos__(self):
00509         return self

Here is the caller graph for this function:

Definition at line 623 of file datetime.py.

00623 
00624     def __reduce__(self):
00625         return (self.__class__, self._getstate())
00626 
00627 timedelta.min = timedelta(-999999999)
00628 timedelta.max = timedelta(days=999999999, hours=23, minutes=59, seconds=59,
00629                           microseconds=999999)
00630 timedelta.resolution = timedelta(microseconds=1)

Here is the call graph for this function:

Definition at line 430 of file datetime.py.

00430 
00431     def __repr__(self):
00432         if self._microseconds:
00433             return "%s(%d, %d, %d)" % ('datetime.' + self.__class__.__name__,
00434                                        self._days,
00435                                        self._seconds,
00436                                        self._microseconds)
00437         if self._seconds:
00438             return "%s(%d, %d)" % ('datetime.' + self.__class__.__name__,
00439                                    self._days,
00440                                    self._seconds)
00441         return "%s(%d)" % ('datetime.' + self.__class__.__name__, self._days)

def datetime.timedelta.__rsub__ (   self,
  other 
)

Definition at line 495 of file datetime.py.

00495 
00496     def __rsub__(self, other):
00497         if isinstance(other, timedelta):
00498             return -self + other
00499         return NotImplemented

Definition at line 442 of file datetime.py.

00442 
00443     def __str__(self):
00444         mm, ss = divmod(self._seconds, 60)
00445         hh, mm = divmod(mm, 60)
00446         s = "%d:%02d:%02d" % (hh, mm, ss)
00447         if self._days:
00448             def plural(n):
00449                 return n, abs(n) != 1 and "s" or ""
00450             s = ("%d day%s, " % plural(self._days)) + s
00451         if self._microseconds:
00452             s = s + ".%06d" % self._microseconds
00453         return s

Here is the call graph for this function:

Here is the caller graph for this function:

def datetime.timedelta.__sub__ (   self,
  other 
)

Definition at line 486 of file datetime.py.

00486 
00487     def __sub__(self, other):
00488         if isinstance(other, timedelta):
00489             # for CPython compatibility, we cannot use
00490             # our __class__ here, but need a real timedelta
00491             return timedelta(self._days - other._days,
00492                              self._seconds - other._seconds,
00493                              self._microseconds - other._microseconds)
00494         return NotImplemented

Here is the caller graph for this function:

def datetime.timedelta.__truediv__ (   self,
  other 
)

Definition at line 543 of file datetime.py.

00543 
00544     def __truediv__(self, other):
00545         if not isinstance(other, (int, float, timedelta)):
00546             return NotImplemented
00547         usec = self._to_microseconds()
00548         if isinstance(other, timedelta):
00549             return usec / other._to_microseconds()
00550         if isinstance(other, int):
00551             return timedelta(0, 0, usec / other)
00552         if isinstance(other, float):
00553             a, b = other.as_integer_ratio()
00554             return timedelta(0, 0, b * usec / a)

Here is the call graph for this function:

def datetime.timedelta._cmp (   self,
  other 
) [private]

Definition at line 606 of file datetime.py.

00606 
00607     def _cmp(self, other):
00608         assert isinstance(other, timedelta)
00609         return _cmp(self._getstate(), other._getstate())

Here is the call graph for this function:

def datetime.timedelta._getstate (   self) [private]

Definition at line 620 of file datetime.py.

00620 
00621     def _getstate(self):
00622         return (self._days, self._seconds, self._microseconds)

Here is the caller graph for this function:

def datetime.timedelta._to_microseconds (   self) [private]

Definition at line 530 of file datetime.py.

00530 
00531     def _to_microseconds(self):
00532         return ((self._days * (24*3600) + self._seconds) * 1000000 +
00533                 self._microseconds)

Here is the caller graph for this function:

def datetime.timedelta.days (   self)
days

Definition at line 461 of file datetime.py.

00461 
00462     def days(self):
00463         """days"""
00464         return self._days

Here is the caller graph for this function:

microseconds

Definition at line 471 of file datetime.py.

00471 
00472     def microseconds(self):
00473         """microseconds"""
00474         return self._microseconds

Here is the caller graph for this function:

seconds

Definition at line 466 of file datetime.py.

00466 
00467     def seconds(self):
00468         """seconds"""
00469         return self._seconds

Here is the caller graph for this function:

Total seconds in the duration.

Definition at line 454 of file datetime.py.

00454 
00455     def total_seconds(self):
00456         """Total seconds in the duration."""
00457         return ((self.days * 86400 + self.seconds)*10**6 +
00458                 self.microseconds) / 10**6

Here is the call graph for this function:


Member Data Documentation

datetime.timedelta.__radd__ = __add__ [static, private]

Definition at line 484 of file datetime.py.

datetime.timedelta.__rmul__ = __mul__ [static, private]

Definition at line 528 of file datetime.py.

string datetime.timedelta.__slots__ = '_days' [static, private]

Definition at line 315 of file datetime.py.

Definition at line 422 of file datetime.py.

Definition at line 424 of file datetime.py.

Definition at line 423 of file datetime.py.


The documentation for this class was generated from the following file: