Commit | Line | Data |
---|---|---|
920dae64 AT |
1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
2 | <html> | |
3 | <head> | |
4 | <link rel="STYLESHEET" href="lib.css" type='text/css' /> | |
5 | <link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" /> | |
6 | <link rel='start' href='../index.html' title='Python Documentation Index' /> | |
7 | <link rel="first" href="lib.html" title='Python Library Reference' /> | |
8 | <link rel='contents' href='contents.html' title="Contents" /> | |
9 | <link rel='index' href='genindex.html' title='Index' /> | |
10 | <link rel='last' href='about.html' title='About this document...' /> | |
11 | <link rel='help' href='about.html' title='About this document...' /> | |
12 | <link rel="next" href="datetime-date.html" /> | |
13 | <link rel="prev" href="node251.html" /> | |
14 | <link rel="parent" href="module-datetime.html" /> | |
15 | <link rel="next" href="datetime-date.html" /> | |
16 | <meta name='aesop' content='information' /> | |
17 | <title>6.10.2 timedelta Objects </title> | |
18 | </head> | |
19 | <body> | |
20 | <DIV CLASS="navigation"> | |
21 | <div id='top-navigation-panel' xml:id='top-navigation-panel'> | |
22 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
23 | <tr> | |
24 | <td class='online-navigation'><a rel="prev" title="6.10.1 Available Types" | |
25 | href="node251.html"><img src='../icons/previous.png' | |
26 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
27 | <td class='online-navigation'><a rel="parent" title="6.10 datetime " | |
28 | href="module-datetime.html"><img src='../icons/up.png' | |
29 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
30 | <td class='online-navigation'><a rel="next" title="6.10.3 date Objects" | |
31 | href="datetime-date.html"><img src='../icons/next.png' | |
32 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
33 | <td align="center" width="100%">Python Library Reference</td> | |
34 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
35 | href="contents.html"><img src='../icons/contents.png' | |
36 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
37 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' | |
38 | border='0' height='32' alt='Module Index' width='32' /></a></td> | |
39 | <td class='online-navigation'><a rel="index" title="Index" | |
40 | href="genindex.html"><img src='../icons/index.png' | |
41 | border='0' height='32' alt='Index' width='32' /></A></td> | |
42 | </tr></table> | |
43 | <div class='online-navigation'> | |
44 | <b class="navlabel">Previous:</b> | |
45 | <a class="sectref" rel="prev" href="node251.html">6.10.1 Available Types</A> | |
46 | <b class="navlabel">Up:</b> | |
47 | <a class="sectref" rel="parent" href="module-datetime.html">6.10 datetime </A> | |
48 | <b class="navlabel">Next:</b> | |
49 | <a class="sectref" rel="next" href="datetime-date.html">6.10.3 date Objects</A> | |
50 | </div> | |
51 | <hr /></div> | |
52 | </DIV> | |
53 | <!--End of Navigation Panel--> | |
54 | ||
55 | <H2><A NAME="SECTION0081020000000000000000"></A><A NAME="datetime-timedelta"></A> | |
56 | <BR> | |
57 | 6.10.2 <tt class="class">timedelta</tt> Objects | |
58 | </H2> | |
59 | ||
60 | <P> | |
61 | A <tt class="class">timedelta</tt> object represents a duration, the difference | |
62 | between two dates or times. | |
63 | ||
64 | <P> | |
65 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
66 | <td><nobr><b><span class="typelabel">class</span> <tt id='l2h-1863' xml:id='l2h-1863' class="class">timedelta</tt></b>(</nobr></td> | |
67 | <td><var></var><big>[</big><var>days</var><big>[</big><var>, seconds</var><big>[</big><var>, | |
68 | microseconds</var><big>[</big><var>, milliseconds</var><big>[</big><var>, | |
69 | 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> | |
70 | <dd> | |
71 | All arguments are optional and default to <code>0</code>. Arguments may | |
72 | be ints, longs, or floats, and may be positive or negative. | |
73 | ||
74 | <P> | |
75 | Only <var>days</var>, <var>seconds</var> and <var>microseconds</var> are stored | |
76 | internally. Arguments are converted to those units: | |
77 | ||
78 | <P> | |
79 | ||
80 | <UL> | |
81 | <LI>A millisecond is converted to 1000 microseconds. | |
82 | </LI> | |
83 | <LI>A minute is converted to 60 seconds. | |
84 | </LI> | |
85 | <LI>An hour is converted to 3600 seconds. | |
86 | </LI> | |
87 | <LI>A week is converted to 7 days. | |
88 | </LI> | |
89 | </UL> | |
90 | ||
91 | <P> | |
92 | and days, seconds and microseconds are then normalized so that the | |
93 | representation is unique, with | |
94 | ||
95 | <P> | |
96 | ||
97 | <UL> | |
98 | <LI><code>0 <= <var>microseconds</var> < 1000000</code> | |
99 | </LI> | |
100 | <LI><code>0 <= <var>seconds</var> < 3600*24</code> (the number of seconds in one day) | |
101 | </LI> | |
102 | <LI><code>-999999999 <= <var>days</var> <= 999999999</code> | |
103 | </LI> | |
104 | </UL> | |
105 | ||
106 | <P> | |
107 | If any argument is a float and there are fractional microseconds, | |
108 | the fractional microseconds left over from all arguments are combined | |
109 | and their sum is rounded to the nearest microsecond. If no | |
110 | argument is a float, the conversion and normalization processes | |
111 | are exact (no information is lost). | |
112 | ||
113 | <P> | |
114 | If the normalized value of days lies outside the indicated range, | |
115 | <tt class="exception">OverflowError</tt> is raised. | |
116 | ||
117 | <P> | |
118 | Note that normalization of negative values may be surprising at first. | |
119 | For example, | |
120 | ||
121 | <P> | |
122 | <div class="verbatim"><pre> | |
123 | >>> d = timedelta(microseconds=-1) | |
124 | >>> (d.days, d.seconds, d.microseconds) | |
125 | (-1, 86399, 999999) | |
126 | </pre></div> | |
127 | </dl> | |
128 | ||
129 | <P> | |
130 | Class attributes are: | |
131 | ||
132 | <P> | |
133 | <dl><dt><b><tt id='l2h-1864' xml:id='l2h-1864' class="member">min</tt></b></dt> | |
134 | <dd> | |
135 | The most negative <tt class="class">timedelta</tt> object, | |
136 | <code>timedelta(-999999999)</code>. | |
137 | </dl> | |
138 | ||
139 | <P> | |
140 | <dl><dt><b><tt id='l2h-1865' xml:id='l2h-1865' class="member">max</tt></b></dt> | |
141 | <dd> | |
142 | The most positive <tt class="class">timedelta</tt> object, | |
143 | <code>timedelta(days=999999999, hours=23, minutes=59, seconds=59, | |
144 | microseconds=999999)</code>. | |
145 | </dl> | |
146 | ||
147 | <P> | |
148 | <dl><dt><b><tt id='l2h-1866' xml:id='l2h-1866' class="member">resolution</tt></b></dt> | |
149 | <dd> | |
150 | The smallest possible difference between non-equal | |
151 | <tt class="class">timedelta</tt> objects, <code>timedelta(microseconds=1)</code>. | |
152 | </dl> | |
153 | ||
154 | <P> | |
155 | Note that, because of normalization, <code>timedelta.max</code> > | |
156 | <code>-timedelta.min</code>. <code>-timedelta.max</code> is not representable as | |
157 | a <tt class="class">timedelta</tt> object. | |
158 | ||
159 | <P> | |
160 | Instance attributes (read-only): | |
161 | ||
162 | <P> | |
163 | <div class="center"><table class="realtable"> | |
164 | <thead> | |
165 | <tr> | |
166 | <th class="center">Attribute</th> | |
167 | <th class="left" >Value</th> | |
168 | </tr> | |
169 | </thead> | |
170 | <tbody> | |
171 | <tr><td class="center" valign="baseline"><code>days</code></td> | |
172 | <td class="left" >Between -999999999 and 999999999 inclusive</td></tr> | |
173 | <tr><td class="center" valign="baseline"><code>seconds</code></td> | |
174 | <td class="left" >Between 0 and 86399 inclusive</td></tr> | |
175 | <tr><td class="center" valign="baseline"><code>microseconds</code></td> | |
176 | <td class="left" >Between 0 and 999999 inclusive</td></tr></tbody> | |
177 | </table></div> | |
178 | ||
179 | <P> | |
180 | Supported operations: | |
181 | ||
182 | <P> | |
183 | <div class="center"><table class="realtable"> | |
184 | <thead> | |
185 | <tr> | |
186 | <th class="center">Operation</th> | |
187 | <th class="left" >Result</th> | |
188 | </tr> | |
189 | </thead> | |
190 | <tbody> | |
191 | <tr><td class="center" valign="baseline"><code><var>t1</var> = <var>t2</var> + <var>t3</var></code></td> | |
192 | <td class="left" >Sum of <var>t2</var> and <var>t3</var>. | |
193 | Afterwards <var>t1</var>-<var>t2</var> == <var>t3</var> and <var>t1</var>-<var>t3</var> | |
194 | == <var>t2</var> are true. | |
195 | (1)</td></tr> | |
196 | <tr><td class="center" valign="baseline"><code><var>t1</var> = <var>t2</var> - <var>t3</var></code></td> | |
197 | <td class="left" >Difference of <var>t2</var> and <var>t3</var>. | |
198 | Afterwards <var>t1</var> == <var>t2</var> - <var>t3</var> and | |
199 | <var>t2</var> == <var>t1</var> + <var>t3</var> are true. | |
200 | (1)</td></tr> | |
201 | <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> | |
202 | <td class="left" >Delta multiplied by an integer or long. | |
203 | Afterwards <var>t1</var> // i == <var>t2</var> is true, | |
204 | provided <code>i != 0</code>.</td></tr> | |
205 | <tr><td class="center" valign="baseline"><code></code></td> | |
206 | <td class="left" >In general, <var>t1</var> * i == <var>t1</var> * (i-1) + <var>t1</var> is true. | |
207 | (1)</td></tr> | |
208 | <tr><td class="center" valign="baseline"><code><var>t1</var> = <var>t2</var> // <var>i</var></code></td> | |
209 | <td class="left" >The floor is computed and the remainder (if any) is thrown away. | |
210 | (3)</td></tr> | |
211 | <tr><td class="center" valign="baseline"><code>+<var>t1</var></code></td> | |
212 | <td class="left" >Returns a <tt class="class">timedelta</tt> object with the same value. | |
213 | (2)</td></tr> | |
214 | <tr><td class="center" valign="baseline"><code>-<var>t1</var></code></td> | |
215 | <td class="left" >equivalent to <tt class="class">timedelta</tt>(-<var>t1.days</var>, -<var>t1.seconds</var>, | |
216 | -<var>t1.microseconds</var>), and to <var>t1</var>* -1. | |
217 | (1)(4)</td></tr> | |
218 | <tr><td class="center" valign="baseline"><code>abs(<var>t</var>)</code></td> | |
219 | <td class="left" >equivalent to +<var>t</var> when <code>t.days >= 0</code>, and to | |
220 | -<var>t</var> when <code>t.days < 0</code>. | |
221 | (2)</td></tr></tbody> | |
222 | </table></div> | |
223 | ||
224 | Notes: | |
225 | ||
226 | <P> | |
227 | <DL> | |
228 | <DT><STRONG>(1)</STRONG></DT> | |
229 | <DD>This is exact, but may overflow. | |
230 | ||
231 | <P> | |
232 | </DD> | |
233 | <DT><STRONG>(2)</STRONG></DT> | |
234 | <DD>This is exact, and cannot overflow. | |
235 | ||
236 | <P> | |
237 | </DD> | |
238 | <DT><STRONG>(3)</STRONG></DT> | |
239 | <DD>Division by 0 raises <tt class="exception">ZeroDivisionError</tt>. | |
240 | ||
241 | <P> | |
242 | </DD> | |
243 | <DT><STRONG>(4)</STRONG></DT> | |
244 | <DD>-<var>timedelta.max</var> is not representable as a <tt class="class">timedelta</tt> object. | |
245 | </DD> | |
246 | </DL> | |
247 | ||
248 | <P> | |
249 | In addition to the operations listed above <tt class="class">timedelta</tt> objects | |
250 | support certain additions and subtractions with <tt class="class">date</tt> and | |
251 | <tt class="class">datetime</tt> objects (see below). | |
252 | ||
253 | <P> | |
254 | Comparisons of <tt class="class">timedelta</tt> objects are supported with the | |
255 | <tt class="class">timedelta</tt> object representing the smaller duration considered | |
256 | to be the smaller timedelta. | |
257 | In order to stop mixed-type comparisons from falling back to the | |
258 | default comparison by object address, when a <tt class="class">timedelta</tt> object is | |
259 | compared to an object of a different type, <tt class="exception">TypeError</tt> is | |
260 | raised unless the comparison is <code>==</code> or <code>!=</code>. The latter | |
261 | cases return <tt class="constant">False</tt> or <tt class="constant">True</tt>, respectively. | |
262 | ||
263 | <P> | |
264 | <tt class="class">timedelta</tt> objects are hashable (usable as dictionary keys), | |
265 | support efficient pickling, and in Boolean contexts, a <tt class="class">timedelta</tt> | |
266 | object is considered to be true if and only if it isn't equal to | |
267 | <code>timedelta(0)</code>. | |
268 | ||
269 | <P> | |
270 | ||
271 | <DIV CLASS="navigation"> | |
272 | <div class='online-navigation'> | |
273 | <p></p><hr /> | |
274 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
275 | <tr> | |
276 | <td class='online-navigation'><a rel="prev" title="6.10.1 Available Types" | |
277 | href="node251.html"><img src='../icons/previous.png' | |
278 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
279 | <td class='online-navigation'><a rel="parent" title="6.10 datetime " | |
280 | href="module-datetime.html"><img src='../icons/up.png' | |
281 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
282 | <td class='online-navigation'><a rel="next" title="6.10.3 date Objects" | |
283 | href="datetime-date.html"><img src='../icons/next.png' | |
284 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
285 | <td align="center" width="100%">Python Library Reference</td> | |
286 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
287 | href="contents.html"><img src='../icons/contents.png' | |
288 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
289 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' | |
290 | border='0' height='32' alt='Module Index' width='32' /></a></td> | |
291 | <td class='online-navigation'><a rel="index" title="Index" | |
292 | href="genindex.html"><img src='../icons/index.png' | |
293 | border='0' height='32' alt='Index' width='32' /></A></td> | |
294 | </tr></table> | |
295 | <div class='online-navigation'> | |
296 | <b class="navlabel">Previous:</b> | |
297 | <a class="sectref" rel="prev" href="node251.html">6.10.1 Available Types</A> | |
298 | <b class="navlabel">Up:</b> | |
299 | <a class="sectref" rel="parent" href="module-datetime.html">6.10 datetime </A> | |
300 | <b class="navlabel">Next:</b> | |
301 | <a class="sectref" rel="next" href="datetime-date.html">6.10.3 date Objects</A> | |
302 | </div> | |
303 | </div> | |
304 | <hr /> | |
305 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> | |
306 | </DIV> | |
307 | <!--End of Navigation Panel--> | |
308 | <ADDRESS> | |
309 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. | |
310 | </ADDRESS> | |
311 | </BODY> | |
312 | </HTML> |