Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / sam-t2 / devtools / v9 / html / python / lib / datetime-date.html
CommitLineData
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-datetime.html" />
13<link rel="prev" href="datetime-timedelta.html" />
14<link rel="parent" href="module-datetime.html" />
15<link rel="next" href="datetime-datetime.html" />
16<meta name='aesop' content='information' />
17<title>6.10.3 date 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.2 timedelta Objects"
25 href="datetime-timedelta.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.4 datetime Objects"
31 href="datetime-datetime.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="datetime-timedelta.html">6.10.2 timedelta Objects</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-datetime.html">6.10.4 datetime Objects</A>
50</div>
51<hr /></div>
52</DIV>
53<!--End of Navigation Panel-->
54
55<H2><A NAME="SECTION0081030000000000000000"></A><A NAME="datetime-date"></A>
56<BR>
576.10.3 <tt class="class">date</tt> Objects
58</H2>
59
60<P>
61A <tt class="class">date</tt> object represents a date (year, month and day) in an idealized
62calendar, the current Gregorian calendar indefinitely extended in both
63directions. January 1 of year 1 is called day number 1, January 2 of year
641 is called day number 2, and so on. This matches the definition of the
65"proleptic Gregorian" calendar in Dershowitz and Reingold's book
66<em class="citetitle"
67 >Calendrical Calculations</em>, where it's the base calendar for all
68computations. See the book for algorithms for converting between
69proleptic Gregorian ordinals and many other calendar systems.
70
71<P>
72<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
73 <td><nobr><b><span class="typelabel">class</span>&nbsp;<tt id='l2h-1867' xml:id='l2h-1867' class="class">date</tt></b>(</nobr></td>
74 <td><var>year, month, day</var>)</td></tr></table></dt>
75<dd>
76 All arguments are required. Arguments may be ints or longs, in the
77 following ranges:
78
79<P>
80
81<UL>
82<LI><code>MINYEAR &lt;= <var>year</var> &lt;= MAXYEAR</code>
83</LI>
84<LI><code>1 &lt;= <var>month</var> &lt;= 12</code>
85</LI>
86<LI><code>1 &lt;= <var>day</var> &lt;= number of days in the given month and year</code>
87
88</LI>
89</UL>
90
91<P>
92If an argument outside those ranges is given, <tt class="exception">ValueError</tt>
93 is raised.
94</dl>
95
96<P>
97Other constructors, all class methods:
98
99<P>
100<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
101 <td><nobr><b><tt id='l2h-1868' xml:id='l2h-1868' class="method">today</tt></b>(</nobr></td>
102 <td><var></var>)</td></tr></table></dt>
103<dd>
104 Return the current local date. This is equivalent to
105 <code>date.fromtimestamp(time.time())</code>.
106</dl>
107
108<P>
109<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
110 <td><nobr><b><tt id='l2h-1869' xml:id='l2h-1869' class="method">fromtimestamp</tt></b>(</nobr></td>
111 <td><var>timestamp</var>)</td></tr></table></dt>
112<dd>
113 Return the local date corresponding to the POSIX timestamp, such
114 as is returned by <tt class="function">time.time()</tt>. This may raise
115 <tt class="exception">ValueError</tt>, if the timestamp is out of the range of
116 values supported by the platform C <tt class="cfunction">localtime()</tt>
117 function. It's common for this to be restricted to years from 1970
118 through 2038. Note that on non-POSIX systems that include leap
119 seconds in their notion of a timestamp, leap seconds are ignored by
120 <tt class="method">fromtimestamp()</tt>.
121</dl>
122
123<P>
124<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
125 <td><nobr><b><tt id='l2h-1870' xml:id='l2h-1870' class="method">fromordinal</tt></b>(</nobr></td>
126 <td><var>ordinal</var>)</td></tr></table></dt>
127<dd>
128 Return the date corresponding to the proleptic Gregorian ordinal,
129 where January 1 of year 1 has ordinal 1. <tt class="exception">ValueError</tt> is
130 raised unless <code>1 &lt;= <var>ordinal</var> &lt;= date.max.toordinal()</code>.
131 For any date <var>d</var>, <code>date.fromordinal(<var>d</var>.toordinal()) ==
132 <var>d</var></code>.
133</dl>
134
135<P>
136Class attributes:
137
138<P>
139<dl><dt><b><tt id='l2h-1871' xml:id='l2h-1871' class="member">min</tt></b></dt>
140<dd>
141 The earliest representable date, <code>date(MINYEAR, 1, 1)</code>.
142</dl>
143
144<P>
145<dl><dt><b><tt id='l2h-1872' xml:id='l2h-1872' class="member">max</tt></b></dt>
146<dd>
147 The latest representable date, <code>date(MAXYEAR, 12, 31)</code>.
148</dl>
149
150<P>
151<dl><dt><b><tt id='l2h-1873' xml:id='l2h-1873' class="member">resolution</tt></b></dt>
152<dd>
153 The smallest possible difference between non-equal date
154 objects, <code>timedelta(days=1)</code>.
155</dl>
156
157<P>
158Instance attributes (read-only):
159
160<P>
161<dl><dt><b><tt id='l2h-1874' xml:id='l2h-1874' class="member">year</tt></b></dt>
162<dd>
163 Between <tt class="constant">MINYEAR</tt> and <tt class="constant">MAXYEAR</tt> inclusive.
164</dl>
165
166<P>
167<dl><dt><b><tt id='l2h-1875' xml:id='l2h-1875' class="member">month</tt></b></dt>
168<dd>
169 Between 1 and 12 inclusive.
170</dl>
171
172<P>
173<dl><dt><b><tt id='l2h-1876' xml:id='l2h-1876' class="member">day</tt></b></dt>
174<dd>
175 Between 1 and the number of days in the given month of the given
176 year.
177</dl>
178
179<P>
180Supported 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>date2</var> = <var>date1</var> + <var>timedelta</var></code></td>
192 <td class="left" ><var>date2</var> is <code><var>timedelta</var>.days</code> days removed from
193 <var>date1</var>. (1)</td></tr><P>
194
195 <tr><td class="center" valign="baseline"><code><var>date2</var> = <var>date1</var> - <var>timedelta</var></code></td>
196 <td class="left" >Computes <var>date2</var> such that <code><var>date2</var> + <var>timedelta</var>
197 == <var>date1</var></code>. (2)</td></tr><P>
198
199 <tr><td class="center" valign="baseline"><code><var>timedelta</var> = <var>date1</var> - <var>date2</var></code></td>
200 <td class="left" >(3)</td></tr><P>
201
202 <tr><td class="center" valign="baseline"><code><var>date1</var> &lt; <var>date2</var></code></td>
203 <td class="left" ><var>date1</var> is considered less than <var>date2</var> when <var>date1</var>
204 precedes <var>date2</var> in time. (4)</td></tr><P>
205
206 </tbody>
207</table></div>
208
209<P>
210Notes:
211<DL>
212<DT><STRONG>(1)</STRONG></DT>
213<DD><var>date2</var> is moved forward in time if <code><var>timedelta</var>.days
214 &gt; 0</code>, or backward if <code><var>timedelta</var>.days &lt; 0</code>. Afterward
215 <code><var>date2</var> - <var>date1</var> == <var>timedelta</var>.days</code>.
216 <code><var>timedelta</var>.seconds</code> and
217 <code><var>timedelta</var>.microseconds</code> are ignored.
218 <tt class="exception">OverflowError</tt> is raised if <code><var>date2</var>.year</code>
219 would be smaller than <tt class="constant">MINYEAR</tt> or larger than
220 <tt class="constant">MAXYEAR</tt>.
221
222<P>
223</DD>
224<DT><STRONG>(2)</STRONG></DT>
225<DD>This isn't quite equivalent to date1 +
226 (-timedelta), because -timedelta in isolation can overflow in cases
227 where date1 - timedelta does not. <code><var>timedelta</var>.seconds</code>
228 and <code><var>timedelta</var>.microseconds</code> are ignored.
229
230<P>
231</DD>
232<DT><STRONG>(3)</STRONG></DT>
233<DD>This is exact, and cannot overflow. timedelta.seconds and
234 timedelta.microseconds are 0, and date2 + timedelta == date1
235 after.
236
237<P>
238</DD>
239<DT><STRONG>(4)</STRONG></DT>
240<DD>In other words, <code>date1 &lt; date2</code>
241 if and only if <code><var>date1</var>.toordinal() &lt;
242 <var>date2</var>.toordinal()</code>.
243In order to stop comparison from falling back to the default
244scheme of comparing object addresses, date comparison
245normally raises <tt class="exception">TypeError</tt> if the other comparand
246isn't also a <tt class="class">date</tt> object. However, <code>NotImplemented</code>
247is returned instead if the other comparand has a
248<tt class="method">timetuple</tt> attribute. This hook gives other kinds of
249date objects a chance at implementing mixed-type comparison.
250If not, when a <tt class="class">date</tt> object is
251compared to an object of a different type, <tt class="exception">TypeError</tt> is
252raised unless the comparison is <code>==</code> or <code>!=</code>. The latter
253cases return <tt class="constant">False</tt> or <tt class="constant">True</tt>, respectively.
254
255<P>
256</DD>
257</DL>
258
259<P>
260Dates can be used as dictionary keys. In Boolean contexts, all
261<tt class="class">date</tt> objects are considered to be true.
262
263<P>
264Instance methods:
265
266<P>
267<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
268 <td><nobr><b><tt id='l2h-1877' xml:id='l2h-1877' class="method">replace</tt></b>(</nobr></td>
269 <td><var>year, month, day</var>)</td></tr></table></dt>
270<dd>
271 Return a date with the same value, except for those members given
272 new values by whichever keyword arguments are specified. For
273 example, if <code>d == date(2002, 12, 31)</code>, then
274 <code>d.replace(day=26) == date(2002, 12, 26)</code>.
275</dl>
276
277<P>
278<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
279 <td><nobr><b><tt id='l2h-1878' xml:id='l2h-1878' class="method">timetuple</tt></b>(</nobr></td>
280 <td><var></var>)</td></tr></table></dt>
281<dd>
282 Return a <tt class="class">time.struct_time</tt> such as returned by
283 <tt class="function">time.localtime()</tt>. The hours, minutes and seconds are
284 0, and the DST flag is -1.
285 <code><var>d</var>.timetuple()</code> is equivalent to
286 <code>time.struct_time((<var>d</var>.year, <var>d</var>.month, <var>d</var>.day,
287 0, 0, 0,
288 <var>d</var>.weekday(),
289 <var>d</var>.toordinal() - date(<var>d</var>.year, 1, 1).toordinal() + 1,
290 -1))</code>
291</dl>
292
293<P>
294<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
295 <td><nobr><b><tt id='l2h-1879' xml:id='l2h-1879' class="method">toordinal</tt></b>(</nobr></td>
296 <td><var></var>)</td></tr></table></dt>
297<dd>
298 Return the proleptic Gregorian ordinal of the date, where January 1
299 of year 1 has ordinal 1. For any <tt class="class">date</tt> object <var>d</var>,
300 <code>date.fromordinal(<var>d</var>.toordinal()) == <var>d</var></code>.
301</dl>
302
303<P>
304<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
305 <td><nobr><b><tt id='l2h-1880' xml:id='l2h-1880' class="method">weekday</tt></b>(</nobr></td>
306 <td><var></var>)</td></tr></table></dt>
307<dd>
308 Return the day of the week as an integer, where Monday is 0 and
309 Sunday is 6. For example, <code>date(2002, 12, 4).weekday() == 2</code>, a
310 Wednesday.
311 See also <tt class="method">isoweekday()</tt>.
312</dl>
313
314<P>
315<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
316 <td><nobr><b><tt id='l2h-1881' xml:id='l2h-1881' class="method">isoweekday</tt></b>(</nobr></td>
317 <td><var></var>)</td></tr></table></dt>
318<dd>
319 Return the day of the week as an integer, where Monday is 1 and
320 Sunday is 7. For example, <code>date(2002, 12, 4).isoweekday() == 3</code>, a
321 Wednesday.
322 See also <tt class="method">weekday()</tt>, <tt class="method">isocalendar()</tt>.
323</dl>
324
325<P>
326<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
327 <td><nobr><b><tt id='l2h-1882' xml:id='l2h-1882' class="method">isocalendar</tt></b>(</nobr></td>
328 <td><var></var>)</td></tr></table></dt>
329<dd>
330 Return a 3-tuple, (ISO year, ISO week number, ISO weekday).
331
332<P>
333The ISO calendar is a widely used variant of the Gregorian calendar.
334 See <a class="url" href="http://www.phys.uu.nl/~vgent/calendar/isocalendar.htm">http://www.phys.uu.nl/~vgent/calendar/isocalendar.htm</a> for a good explanation.
335
336<P>
337The ISO year consists of 52 or 53 full weeks, and where a week starts
338 on a Monday and ends on a Sunday. The first week of an ISO year is
339 the first (Gregorian) calendar week of a year containing a Thursday.
340 This is called week number 1, and the ISO year of that Thursday is
341 the same as its Gregorian year.
342
343<P>
344For example, 2004 begins on a Thursday, so the first week of ISO
345 year 2004 begins on Monday, 29 Dec 2003 and ends on Sunday, 4 Jan
346 2004, so that
347 <code>date(2003, 12, 29).isocalendar() == (2004, 1, 1)</code>
348 and
349 <code>date(2004, 1, 4).isocalendar() == (2004, 1, 7)</code>.
350</dl>
351
352<P>
353<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
354 <td><nobr><b><tt id='l2h-1883' xml:id='l2h-1883' class="method">isoformat</tt></b>(</nobr></td>
355 <td><var></var>)</td></tr></table></dt>
356<dd>
357 Return a string representing the date in ISO 8601 format,
358 'YYYY-MM-DD'. For example,
359 <code>date(2002, 12, 4).isoformat() == '2002-12-04'</code>.
360</dl>
361
362<P>
363<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
364 <td><nobr><b><tt id='l2h-1884' xml:id='l2h-1884' class="method">__str__</tt></b>(</nobr></td>
365 <td><var></var>)</td></tr></table></dt>
366<dd>
367 For a date <var>d</var>, <code>str(<var>d</var>)</code> is equivalent to
368 <code><var>d</var>.isoformat()</code>.
369</dl>
370
371<P>
372<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
373 <td><nobr><b><tt id='l2h-1885' xml:id='l2h-1885' class="method">ctime</tt></b>(</nobr></td>
374 <td><var></var>)</td></tr></table></dt>
375<dd>
376 Return a string representing the date, for example
377 date(2002, 12, 4).ctime() == 'Wed Dec 4 00:00:00 2002'.
378 <code><var>d</var>.ctime()</code> is equivalent to
379 <code>time.ctime(time.mktime(<var>d</var>.timetuple()))</code>
380 on platforms where the native C <tt class="cfunction">ctime()</tt> function
381 (which <tt class="function">time.ctime()</tt> invokes, but which
382 <tt class="method">date.ctime()</tt> does not invoke) conforms to the C standard.
383</dl>
384
385<P>
386<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
387 <td><nobr><b><tt id='l2h-1886' xml:id='l2h-1886' class="method">strftime</tt></b>(</nobr></td>
388 <td><var>format</var>)</td></tr></table></dt>
389<dd>
390 Return a string representing the date, controlled by an explicit
391 format string. Format codes referring to hours, minutes or seconds
392 will see 0 values.
393 See the section on <tt class="method">strftime()</tt> behavior.
394</dl>
395
396<P>
397
398<DIV CLASS="navigation">
399<div class='online-navigation'>
400<p></p><hr />
401<table align="center" width="100%" cellpadding="0" cellspacing="2">
402<tr>
403<td class='online-navigation'><a rel="prev" title="6.10.2 timedelta Objects"
404 href="datetime-timedelta.html"><img src='../icons/previous.png'
405 border='0' height='32' alt='Previous Page' width='32' /></A></td>
406<td class='online-navigation'><a rel="parent" title="6.10 datetime "
407 href="module-datetime.html"><img src='../icons/up.png'
408 border='0' height='32' alt='Up One Level' width='32' /></A></td>
409<td class='online-navigation'><a rel="next" title="6.10.4 datetime Objects"
410 href="datetime-datetime.html"><img src='../icons/next.png'
411 border='0' height='32' alt='Next Page' width='32' /></A></td>
412<td align="center" width="100%">Python Library Reference</td>
413<td class='online-navigation'><a rel="contents" title="Table of Contents"
414 href="contents.html"><img src='../icons/contents.png'
415 border='0' height='32' alt='Contents' width='32' /></A></td>
416<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
417 border='0' height='32' alt='Module Index' width='32' /></a></td>
418<td class='online-navigation'><a rel="index" title="Index"
419 href="genindex.html"><img src='../icons/index.png'
420 border='0' height='32' alt='Index' width='32' /></A></td>
421</tr></table>
422<div class='online-navigation'>
423<b class="navlabel">Previous:</b>
424<a class="sectref" rel="prev" href="datetime-timedelta.html">6.10.2 timedelta Objects</A>
425<b class="navlabel">Up:</b>
426<a class="sectref" rel="parent" href="module-datetime.html">6.10 datetime </A>
427<b class="navlabel">Next:</b>
428<a class="sectref" rel="next" href="datetime-datetime.html">6.10.4 datetime Objects</A>
429</div>
430</div>
431<hr />
432<span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span>
433</DIV>
434<!--End of Navigation Panel-->
435<ADDRESS>
436See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
437</ADDRESS>
438</BODY>
439</HTML>