<!DOCTYPE html PUBLIC
"-//W3C//DTD HTML 4.0 Transitional//EN">
<link rel=
"STYLESHEET" href=
"lib.css" type='text/css'
/>
<link rel=
"SHORTCUT ICON" href=
"../icons/pyfav.png" type=
"image/png" />
<link rel='start' href='../index.html' title='Python Documentation Index'
/>
<link rel=
"first" href=
"lib.html" title='Python Library Reference'
/>
<link rel='contents' href='contents.html'
title=
"Contents" />
<link rel='index' href='genindex.html' title='Index'
/>
<link rel='last' href='about.html' title='About this document...'
/>
<link rel='help' href='about.html' title='About this document...'
/>
<link rel=
"next" href=
"datetime-date.html" />
<link rel=
"prev" href=
"node251.html" />
<link rel=
"parent" href=
"module-datetime.html" />
<link rel=
"next" href=
"datetime-date.html" />
<meta name='aesop' content='information'
/>
<title>6.10.2 timedelta Objects
</title>
<div id='top-navigation-panel' xml:id='top-navigation-panel'
>
<table align=
"center" width=
"100%" cellpadding=
"0" cellspacing=
"2">
<td class='online-navigation'
><a rel=
"prev" title=
"6.10.1 Available Types"
href=
"node251.html"><img src='../icons/previous.png'
border='
0' height='
32' alt='Previous Page' width='
32'
/></A></td>
<td class='online-navigation'
><a rel=
"parent" title=
"6.10 datetime "
href=
"module-datetime.html"><img src='../icons/up.png'
border='
0' height='
32' alt='Up One Level' width='
32'
/></A></td>
<td class='online-navigation'
><a rel=
"next" title=
"6.10.3 date Objects"
href=
"datetime-date.html"><img src='../icons/next.png'
border='
0' height='
32' alt='Next Page' width='
32'
/></A></td>
<td align=
"center" width=
"100%">Python Library Reference
</td>
<td class='online-navigation'
><a rel=
"contents" title=
"Table of Contents"
href=
"contents.html"><img src='../icons/contents.png'
border='
0' height='
32' alt='Contents' width='
32'
/></A></td>
<td class='online-navigation'
><a href=
"modindex.html" title=
"Module Index"><img src='../icons/modules.png'
border='
0' height='
32' alt='Module Index' width='
32'
/></a></td>
<td class='online-navigation'
><a rel=
"index" title=
"Index"
href=
"genindex.html"><img src='../icons/index.png'
border='
0' height='
32' alt='Index' width='
32'
/></A></td>
<div class='online-navigation'
>
<b class=
"navlabel">Previous:
</b>
<a class=
"sectref" rel=
"prev" href=
"node251.html">6.10.1 Available Types
</A>
<b class=
"navlabel">Up:
</b>
<a class=
"sectref" rel=
"parent" href=
"module-datetime.html">6.10 datetime
</A>
<b class=
"navlabel">Next:
</b>
<a class=
"sectref" rel=
"next" href=
"datetime-date.html">6.10.3 date Objects
</A>
<!--End of Navigation Panel-->
<H2><A NAME=
"SECTION0081020000000000000000"></A><A NAME=
"datetime-timedelta"></A>
6.10.2 <tt class=
"class">timedelta
</tt> Objects
A
<tt class=
"class">timedelta
</tt> object represents a duration, the difference
between two dates or times.
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><span class=
"typelabel">class
</span> <tt id='l2h-
1863' xml:id='l2h-
1863'
class=
"class">timedelta
</tt></b>(
</nobr></td>
<td><var></var><big>[
</big><var>days
</var><big>[
</big><var>, seconds
</var><big>[
</big><var>,
microseconds
</var><big>[
</big><var>, milliseconds
</var><big>[
</big><var>,
minutes
</var><big>[
</big><var>, hours
</var><big>[
</big><var>, weeks
</var><big>]
</big><var></var><big>]
</big><var></var><big>]
</big><var></var><big>]
</big><var></var><big>]
</big><var></var><big>]
</big><var></var><big>]
</big><var></var>)
</td></tr></table></dt>
All arguments are optional and default to
<code>0</code>. Arguments may
be ints, longs, or floats, and may be positive or negative.
Only
<var>days
</var>,
<var>seconds
</var> and
<var>microseconds
</var> are stored
internally. Arguments are converted to those units:
<LI>A millisecond is converted to
1000 microseconds.
<LI>A minute is converted to
60 seconds.
<LI>An hour is converted to
3600 seconds.
<LI>A week is converted to
7 days.
and days, seconds and microseconds are then normalized so that the
representation is unique, with
<LI><code>0 <=
<var>microseconds
</var> < 1000000</code>
<LI><code>0 <=
<var>seconds
</var> < 3600*
24</code> (the number of seconds in one day)
<LI><code>-
999999999 <=
<var>days
</var> <=
999999999</code>
If any argument is a float and there are fractional microseconds,
the fractional microseconds left over from all arguments are combined
and their sum is rounded to the nearest microsecond. If no
argument is a float, the conversion and normalization processes
are exact (no information is lost).
If the normalized value of days lies outside the indicated range,
<tt class=
"exception">OverflowError
</tt> is raised.
Note that normalization of negative values may be surprising at first.
<div class=
"verbatim"><pre>
>>> d = timedelta(microseconds=-
1)
>>> (d.days, d.seconds, d.microseconds)
<dl><dt><b><tt id='l2h-
1864' xml:id='l2h-
1864'
class=
"member">min
</tt></b></dt>
The most negative
<tt class=
"class">timedelta
</tt> object,
<code>timedelta(-
999999999)
</code>.
<dl><dt><b><tt id='l2h-
1865' xml:id='l2h-
1865'
class=
"member">max
</tt></b></dt>
The most positive
<tt class=
"class">timedelta
</tt> object,
<code>timedelta(days=
999999999, hours=
23, minutes=
59, seconds=
59,
microseconds=
999999)
</code>.
<dl><dt><b><tt id='l2h-
1866' xml:id='l2h-
1866'
class=
"member">resolution
</tt></b></dt>
The smallest possible difference between non-equal
<tt class=
"class">timedelta
</tt> objects,
<code>timedelta(microseconds=
1)
</code>.
Note that, because of normalization,
<code>timedelta.max
</code> >
<code>-timedelta.min
</code>.
<code>-timedelta.max
</code> is not representable as
a
<tt class=
"class">timedelta
</tt> object.
Instance attributes (read-only):
<div class=
"center"><table class=
"realtable">
<th class=
"center">Attribute
</th>
<th class=
"left" >Value
</th>
<tr><td class=
"center" valign=
"baseline"><code>days
</code></td>
<td class=
"left" >Between -
999999999 and
999999999 inclusive
</td></tr>
<tr><td class=
"center" valign=
"baseline"><code>seconds
</code></td>
<td class=
"left" >Between
0 and
86399 inclusive
</td></tr>
<tr><td class=
"center" valign=
"baseline"><code>microseconds
</code></td>
<td class=
"left" >Between
0 and
999999 inclusive
</td></tr></tbody>
<div class=
"center"><table class=
"realtable">
<th class=
"center">Operation
</th>
<th class=
"left" >Result
</th>
<tr><td class=
"center" valign=
"baseline"><code><var>t1
</var> =
<var>t2
</var> +
<var>t3
</var></code></td>
<td class=
"left" >Sum of
<var>t2
</var> and
<var>t3
</var>.
Afterwards
<var>t1
</var>-
<var>t2
</var> ==
<var>t3
</var> and
<var>t1
</var>-
<var>t3
</var>
==
<var>t2
</var> are true.
<tr><td class=
"center" valign=
"baseline"><code><var>t1
</var> =
<var>t2
</var> -
<var>t3
</var></code></td>
<td class=
"left" >Difference of
<var>t2
</var> and
<var>t3
</var>.
Afterwards
<var>t1
</var> ==
<var>t2
</var> -
<var>t3
</var> and
<var>t2
</var> ==
<var>t1
</var> +
<var>t3
</var> are true.
<tr><td class=
"center" valign=
"baseline"><code><var>t1
</var> =
<var>t2
</var> *
<var>i
</var> or
<var>t1
</var> =
<var>i
</var> *
<var>t2
</var></code></td>
<td class=
"left" >Delta multiplied by an integer or long.
Afterwards
<var>t1
</var> // i ==
<var>t2
</var> is true,
provided
<code>i !=
0</code>.
</td></tr>
<tr><td class=
"center" valign=
"baseline"><code></code></td>
<td class=
"left" >In general,
<var>t1
</var> * i ==
<var>t1
</var> * (i-
1) +
<var>t1
</var> is true.
<tr><td class=
"center" valign=
"baseline"><code><var>t1
</var> =
<var>t2
</var> //
<var>i
</var></code></td>
<td class=
"left" >The floor is computed and the remainder (if any) is thrown away.
<tr><td class=
"center" valign=
"baseline"><code>+
<var>t1
</var></code></td>
<td class=
"left" >Returns a
<tt class=
"class">timedelta
</tt> object with the same value.
<tr><td class=
"center" valign=
"baseline"><code>-
<var>t1
</var></code></td>
<td class=
"left" >equivalent to
<tt class=
"class">timedelta
</tt>(-
<var>t1.days
</var>, -
<var>t1.seconds
</var>,
-
<var>t1.microseconds
</var>), and to
<var>t1
</var>* -
1.
<tr><td class=
"center" valign=
"baseline"><code>abs(
<var>t
</var>)
</code></td>
<td class=
"left" >equivalent to +
<var>t
</var> when
<code>t.days
>=
0</code>, and to
-
<var>t
</var> when
<code>t.days
< 0</code>.
<DT><STRONG>(
1)
</STRONG></DT>
<DD>This is exact, but may overflow.
<DT><STRONG>(
2)
</STRONG></DT>
<DD>This is exact, and cannot overflow.
<DT><STRONG>(
3)
</STRONG></DT>
<DD>Division by
0 raises
<tt class=
"exception">ZeroDivisionError
</tt>.
<DT><STRONG>(
4)
</STRONG></DT>
<DD>-
<var>timedelta.max
</var> is not representable as a
<tt class=
"class">timedelta
</tt> object.
In addition to the operations listed above
<tt class=
"class">timedelta
</tt> objects
support certain additions and subtractions with
<tt class=
"class">date
</tt> and
<tt class=
"class">datetime
</tt> objects (see below).
Comparisons of
<tt class=
"class">timedelta
</tt> objects are supported with the
<tt class=
"class">timedelta
</tt> object representing the smaller duration considered
to be the smaller timedelta.
In order to stop mixed-type comparisons from falling back to the
default comparison by object address, when a
<tt class=
"class">timedelta
</tt> object is
compared to an object of a different type,
<tt class=
"exception">TypeError
</tt> is
raised unless the comparison is
<code>==
</code> or
<code>!=
</code>. The latter
cases return
<tt class=
"constant">False
</tt> or
<tt class=
"constant">True
</tt>, respectively.
<tt class=
"class">timedelta
</tt> objects are hashable (usable as dictionary keys),
support efficient pickling, and in Boolean contexts, a
<tt class=
"class">timedelta
</tt>
object is considered to be true if and only if it isn't equal to
<code>timedelta(
0)
</code>.
<div class='online-navigation'
>
<table align=
"center" width=
"100%" cellpadding=
"0" cellspacing=
"2">
<td class='online-navigation'
><a rel=
"prev" title=
"6.10.1 Available Types"
href=
"node251.html"><img src='../icons/previous.png'
border='
0' height='
32' alt='Previous Page' width='
32'
/></A></td>
<td class='online-navigation'
><a rel=
"parent" title=
"6.10 datetime "
href=
"module-datetime.html"><img src='../icons/up.png'
border='
0' height='
32' alt='Up One Level' width='
32'
/></A></td>
<td class='online-navigation'
><a rel=
"next" title=
"6.10.3 date Objects"
href=
"datetime-date.html"><img src='../icons/next.png'
border='
0' height='
32' alt='Next Page' width='
32'
/></A></td>
<td align=
"center" width=
"100%">Python Library Reference
</td>
<td class='online-navigation'
><a rel=
"contents" title=
"Table of Contents"
href=
"contents.html"><img src='../icons/contents.png'
border='
0' height='
32' alt='Contents' width='
32'
/></A></td>
<td class='online-navigation'
><a href=
"modindex.html" title=
"Module Index"><img src='../icons/modules.png'
border='
0' height='
32' alt='Module Index' width='
32'
/></a></td>
<td class='online-navigation'
><a rel=
"index" title=
"Index"
href=
"genindex.html"><img src='../icons/index.png'
border='
0' height='
32' alt='Index' width='
32'
/></A></td>
<div class='online-navigation'
>
<b class=
"navlabel">Previous:
</b>
<a class=
"sectref" rel=
"prev" href=
"node251.html">6.10.1 Available Types
</A>
<b class=
"navlabel">Up:
</b>
<a class=
"sectref" rel=
"parent" href=
"module-datetime.html">6.10 datetime
</A>
<b class=
"navlabel">Next:
</b>
<a class=
"sectref" rel=
"next" href=
"datetime-date.html">6.10.3 date Objects
</A>
<span class=
"release-info">Release
2.4.2, documentation updated on
28 September
2005.
</span>
<!--End of Navigation Panel-->
See
<i><a href=
"about.html">About this document...
</a></i> for information on suggesting changes.