forked from ninas/umonya_notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatetime.html
More file actions
334 lines (236 loc) · 8.21 KB
/
datetime.html
File metadata and controls
334 lines (236 loc) · 8.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Introductory Programming in Python: Handling Dates and Times</title>
<link rel='stylesheet' type='text/css' href='style.css' />
<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />
<script src="animation.js" type="text/javascript">
</script>
</head>
<body onload="animate_loop()">
<div class="page">
<h1>Introductory Programming in Python: Lesson 22<br />
Handling Dates and Times</h1>
<div class="centered">
[<a href="os.html">Prev: Operating System Functionality</a>] [<a href="index.html">Course Outline</a>] [<a href="writing_errors.html">Next: Writing Meaningful Error Messages</a>]
</div>
<h2>The Unix Epoch</h2>
<p>Dates and times have been traditionally tricky for computers to
handle, because of the complexities of the Gregorian calendar, let
alone taking into account the existence of non-western cultural
calendars. The solution was to find something standard, common to every
calendar, and easy to manipulate. The second was chosen as the standard
unit of time in programming, and in the UNIX world a fixed standard
time was named, the UNIX epoch, being 00:00am 1<sup>st</sup> January,
1970. (The year UNIX was first released). Other operating systems use a
different epoch. The reason for this, is that practically every
application of arithmetic to times or dates involves breaking down the
date or time in question to a value in seconds, and then converting it
back. Both conversion to and from seconds, and finally into a nice
human readable representation are common practice in programming,
tedious, and thus already solved by someone else in the form of the <a
class='doclink' href=
'http://docs.python.org/lib/module-time.html'>time module</a>.</p>
<h2>Using the time Module</h2>
<p>Whilst there are many functions available in the time modules, three
are of primary importance, ignoring the complexities of locales and
timezones. Obviously the first thing we need to do is to import the
module...</p>
<pre class='listing'>
Python 2.6.4 (r264:75706, Dec 7 2009, 18:43:55) [MSC v.1310 32 bit (Intel)] on win32
[GCC 3.4.6 (Gentoo 3.4.6-r1, ssp-3.4.5-1.0, pie-8.7.9)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import time
>>>
</pre>
<ul>
<li><code>time.time()</code> returns the number of seconds since
the epoch, where the epoch is system dependant. On some systems, a
floating point number is returned, in which the non-integer portion
of the number represents sub-second precision.</li>
<li>
<code>time.localtime([seconds])</code> converts 'seconds' if
given, otherwise the value returned by time.time(), into a
tuple of 9 values, broken down as follows. Note that the tuple
returned can be accessed using dot notation and the attribute
name, as well as by index subscription.<br />
<br />
<table cellspacing='0'>
<caption>Contents of time.localtime() tuple.</caption>
<thead>
<tr>
<th>Index</th>
<th>Attribute</th>
<th>Values</th>
</tr>
</thead>
<tbody>
<tr>
<td><tt>0</tt></td>
<td><tt>tm_year</tt></td>
<td>(for example, 1993)</td>
</tr>
<tr>
<td><tt>1</tt></td>
<td><tt>tm_mon</tt></td>
<td>range [1,12]</td>
</tr>
<tr>
<td><tt>2</tt></td>
<td><tt>tm_mday</tt></td>
<td>range [1,31]</td>
</tr>
<tr>
<td><tt>3</tt></td>
<td><tt>tm_hour</tt></td>
<td>range [0,23]</td>
</tr>
<tr>
<td><tt>4</tt></td>
<td><tt>tm_min</tt></td>
<td>range [0,59]</td>
</tr>
<tr>
<td><tt>5</tt></td>
<td><tt>tm_sec</tt></td>
<td>range [0,61]; see <strong>(1)</strong> in <tt>strftime()</tt> description</td>
</tr>
<tr>
<td><tt>6</tt></td>
<td><tt>tm_wday</tt></td>
<td>range [0,6], Monday is 0</td>
</tr>
<tr>
<td><tt>7</tt></td>
<td><tt>tm_yday</tt></td>
<td>range [1,366]</td>
</tr>
<tr>
<td><tt>8</tt></td>
<td><tt>tm_isdst</tt></td>
<td>0, 1 or -1; see below</td>
</tr>
</tbody>
</table><br />
</li>
<li>
<code>time.strftime(format[, t])</code> returns a string that
formats the tuple given in 't', or the tuple returned by
time.localtime() if 't' is not given, as per the string
'format'. 't', if given, must be a tuple matching the structure
of one returned by time.localtime(). The 'format' string will
be returned as is, except that '%' specifiers will be replaced
as indicated by the following table.<br />
<br />
<table cellspacing='0'>
<caption>Contents of time.localtime() tuple.</caption>
<thead>
<tr>
<th>Directive</th>
<th>Meaning</th>
</tr>
</thead>
<tbody>
<tr>
<td><tt>%a</tt></td>
<td>Locale's abbreviated weekday name.</td>
</tr>
<tr>
<td><tt>%A</tt></td>
<td>Locale's full weekday name.</td>
</tr>
<tr>
<td><tt>%b</tt></td>
<td>Locale's abbreviated month name.</td>
</tr>
<tr>
<td><tt>%B</tt></td>
<td>Locale's full month name.</td>
</tr>
<tr>
<td><tt>%c</tt></td>
<td>Locale's appropriate date and time representation.</td>
</tr>
<tr>
<td><tt>%d</tt></td>
<td>Day of the month as a decimal number [01,31].</td>
</tr>
<tr>
<td><tt>%H</tt></td>
<td>Hour (24-hour clock) as a decimal number [00,23].</td>
</tr>
<tr>
<td><tt>%I</tt></td>
<td>Hour (12-hour clock) as a decimal number [01,12].</td>
</tr>
<tr>
<td><tt>%j</tt></td>
<td>Day of the year as a decimal number [001,366].</td>
</tr>
<tr>
<td><tt>%m</tt></td>
<td>Month as a decimal number [01,12].</td>
</tr>
<tr>
<td><tt>%M</tt></td>
<td>Minute as a decimal number [00,59].</td>
</tr>
<tr>
<td><tt>%p</tt></td>
<td>Locale's equivalent of either AM or PM.</td>
</tr>
<tr>
<td><tt>%S</tt></td>
<td>Second as a decimal number [00,61].</td>
</tr>
<tr>
<td><tt>%U</tt></td>
<td>Week number of the year (Sunday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Sunday are considered to be in week 0.</td>
</tr>
<tr>
<td><tt>%w</tt></td>
<td>Weekday as a decimal number [0(Sunday),6].</td>
</tr>
<tr>
<td><tt>%W</tt></td>
<td>Week number of the year (Monday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Monday are considered to be in week 0.</td>
</tr>
<tr>
<td><tt>%x</tt></td>
<td>Locale's appropriate date representation.</td>
</tr>
<tr>
<td><tt>%X</tt></td>
<td>Locale's appropriate time representation.</td>
</tr>
<tr>
<td><tt>%y</tt></td>
<td>Year without century as a decimal number [00,99].</td>
</tr>
<tr>
<td><tt>%Y</tt></td>
<td>Year with century as a decimal number.</td>
</tr>
<tr>
<td><tt>%Z</tt></td>
<td>Time zone name (no characters if no time zone exists).</td>
</tr>
<tr>
<td><tt>%%</tt></td>
<td>A literal "<tt class="character">%</tt>" character.</td>
</tr>
</tbody>
</table>
</li>
</ul>
<h2>Exercises</h2>
<div class="centered">
[<a href="os.html">Prev: Operating System Functionality</a>] [<a href="index.html">Course Outline</a>] [<a href="writing_errors.html">Next: Writing Meaningful Error Messages</a>]
</div>
</div>
<div class="pagefooter">
Copyright © James Dominy 2007-2008; Released under the <a href="http://www.gnu.org/copyleft/fdl.html">GNU Free Documentation License</a><br />
<a href="intropython.tar.gz">Download the tarball</a>
</div>
</body>
</html>