4
4
5
5
“一寸光阴一寸金,寸金难买寸光阴”,时间是宝贵的。
6
6
7
- 在日常生活中,“时间”这个属于是比较笼统和含糊的 。在物理学中,“时间”是一个非常明确的概念。在python中 ,“时间”可以通过相关模块实现。
7
+ 在日常生活中,“时间”这个术语是比较笼统和含糊的 。在物理学中,“时间”是一个非常明确的概念。在Python中 ,“时间”可以通过相关模块实现。
8
8
9
9
##calendar
10
10
19
19
19 20 21 22 23 24 25
20
20
26 27 28 29 30 31
21
21
22
- 轻而易举得到了2015年1月的日历,并且排列的还那么整齐。这就是calendar模块。读者可以用dir() 去查看这个模块下的所有内容。为了让读者阅读方便,将常用的整理如下:
22
+ 轻而易举得到了2015年1月的日历,并且排列的还那么整齐。这就是 ` calendar ` 模块。读者可以用 ` dir() ` 去查看这个模块下的所有内容。为了让读者阅读方便,将常用的整理如下:
23
23
24
24
** calendar(year,w=2,l=1,c=6)**
25
25
26
- 返回year年年历 ,3个月一行,间隔距离为c。 每日宽度间隔为w字符。每行长度为21 * W +18+2* C 。l是每星期行数。
26
+ 返回year年的年历 ,3个月一行,间隔距离为c。 每日宽度间隔为w字符。每行长度为 ` 21* w +18+2* c ` 。l是每星期行数。
27
27
28
28
>>> year = calendar.calendar(2015)
29
29
>>> print year
64
64
26 27 28 29 30 31 23 24 25 26 27 28 29 28 29 30 31
65
65
30
66
66
67
+ 其它部分就是按照上面的样式,将2015年度的各个月份日历完全显示出来。
68
+
67
69
** isleap(year)**
68
70
69
71
判断是否为闰年,是则返回true,否则false.
75
77
76
78
怎么判断一年是闰年,常常见诸于一些编程语言的练习题,现在用一个方法搞定。
77
79
78
- ** leapdays(y1,y2)**
80
+ ** leapdays(y1, y2)**
79
81
80
- 返回在Y1,Y2两年之间的闰年总数 ,包括y1,但不包括y2,这有点如同序列的切片一样。
82
+ 返回在y1,y2两年之间的闰年总数 ,包括y1,但不包括y2,这有点如同序列的切片一样。
81
83
82
- >>> calendar.leapdays(2000,2004)
84
+ >>> calendar.leapdays(2000, 2004)
83
85
1
84
- >>> calendar.leapdays(2000,2003)
86
+ >>> calendar.leapdays(2000, 2003)
85
87
1
86
88
87
- ** month(year,month,w=2,l=1)**
89
+ ** month(year, month, w=2, l=1)**
88
90
89
- 返回year年month月日历,两行标题,一周一行。每日宽度间隔为w字符。每行的长度为7* w+6。 l是每星期的行数。
91
+ 返回year年month月日历,两行标题,一周一行。每日宽度间隔为w字符。每行的长度为7* w+6, l是每星期的行数。
90
92
91
93
>>> print calendar.month(2015, 5)
92
94
May 2015
99
101
100
102
** monthcalendar(year,month)**
101
103
102
- 返回一个列表,列表内的元素还是列表,这叫做嵌套列表 。每个子列表代表一个星期,都是从星期一到星期日,如果没有本月的日期,则为0。
104
+ 返回一个列表,列表内的元素还是列表。每个子列表代表一个星期,都是从星期一到星期日,如果没有本月的日期,则为0。
103
105
104
106
>>> calendar.monthcalendar(2015, 5)
105
107
[[0, 0, 0, 0, 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]]
106
108
107
109
读者可以将这个结果和` calendar.month(2015, 5) ` 去对照理解。
108
110
109
- ** monthrange(year,month)**
111
+ ** monthrange(year, month)**
110
112
111
113
返回一个元组,里面有两个整数。第一个整数代表着该月的第一天从星期几是(从0开始,依次为星期一、星期二,直到6代表星期日)。第二个整数是该月一共多少天。
112
114
128
130
129
131
** time()**
130
132
131
- time模块是常用的。
133
+ ` time ` 模块很常用,比如记录某个程序运行时间的长短等,下面一一道来其中的方法。
134
+
135
+ - ` time() `
132
136
133
137
>>> import time
134
138
>>> time.time()
135
139
1430745298.391026
136
140
137
- ` time.time() ` 获得的是当前时间(严格说是时间戳),只不过这个时间对人不友好,它是以1970年1月1日0时0分0秒为计时起点,到当前的时间长度(不考虑闰秒)
141
+ ` time.time() ` 获得的是当前时间(严格说是时间戳),只不过这个时间对人不友好,它是以1970年1月1日0时0分0秒为计时起点,到当前的时间长度(不考虑闰秒)。
138
142
139
143
> UNIX時間,或稱POSIX時間是UNIX或類UNIX系統使用的時間表示方式:從協調世界時1970年1月1日0時0分0秒起至現在的總秒數,不考慮閏秒
140
144
141
145
> 現時大部分使用UNIX的系統都是32位元的,即它們會以32位二進制數字表示時間。但是它們最多只能表示至協調世界時間2038年1月19日3時14分07秒(二進制:01111111 11111111 11111111 11111111,0x7FFF: FFFF ),在下一秒二進制數字會是10000000 00000000 00000000 00000000,(0x8000:0000),這是負數,因此各系統會把時間誤解作1901年12月13日20時45分52秒(亦有說回歸到1970年)。這時可能會令軟體發生問題,導致系統癱瘓。
142
146
143
-
144
147
> 目前的解決方案是把系統由32位元轉為64位元系統。在64位系統下,此時間最多可以表示到292,277,026,596年12月4日15時30分08秒。
145
148
146
149
有没有对人友好一点的时间显示呢?
@@ -168,7 +171,7 @@ time模块是常用的。
168
171
>>> t[1]
169
172
5
170
173
171
- 通过索引,能够得到相应的属性 ,上面的例子中就得到了当前时间的月份。
174
+ 通过索引能够得到相应的属性 ,上面的例子中就得到了当前时间的月份。
172
175
173
176
其实,` time.localtime() ` 不是没有参数,它在默认情况下,以` time.time() ` 的时间戳为参数。言外之意就是说可以自己输入一个时间戳,返回那个时间戳所对应的时间(按照公元和时分秒计时)。例如:
174
177
@@ -185,7 +188,7 @@ localtime()得到的是本地时间,如果要国际化,就最好使用格林
185
188
186
189
> 格林威治標準時間(中國大陸翻譯:格林尼治平均時間或格林尼治標準時間,台、港、澳翻譯:格林威治標準時間;英语:Greenwich Mean Time,GMT)是指位於英國倫敦郊區的皇家格林威治天文台的標準時間,因為本初子午線被定義在通過那裡的經線。
187
190
188
- 还有更友好的:
191
+ 还有更友好的,请继续阅读。
189
192
190
193
** asctime()**
191
194
@@ -200,7 +203,7 @@ localtime()得到的是本地时间,如果要国际化,就最好使用格林
200
203
>>> time.asctime(h)
201
204
'Mon Jan 12 21:46:40 1970'
202
205
203
- 注意,` time.asctime() ` 的参数必须是时间元组,类似上面那种。不是时间戳,通过` time.time() ` 得到的时间戳,也可以转化为上面形式:
206
+ 注意,` time.asctime() ` 的参数必须是时间元组,类似上面那种。不是时间戳,通过` time.time() ` 得到的时间戳也可以转化为上面形式。
204
207
205
208
** ctime()**
206
209
@@ -212,9 +215,9 @@ localtime()得到的是本地时间,如果要国际化,就最好使用格林
212
215
>>> time.ctime(1000000)
213
216
'Mon Jan 12 21:46:40 1970'
214
217
215
- 跟前面得到的结果是一样的。只不过是用了时间戳作为参数 。
218
+ 跟前面得到的结果是一样的,只不过用了时间戳作为参数 。
216
219
217
- 在前述函数中,通过localtime()、 gmtime()得到的是时间元组,通过time() 得到的是时间戳。有的函数如asctime() 是以时间元组为参数,有的如ctime()是以时间戳为函数。 这样做的目的是为了满足编程中多样化的需要。
220
+ 在前述函数中,通过 ` localtime() ` 、 ` gmtime() ` 得到的是时间元组,通过 ` time() ` 得到的是时间戳。有的函数如 ` asctime() ` 是以时间元组为参数,有的如 ` ctime() ` 是以时间戳为参数, 这样做的目的是为了满足编程中多样化的需要。
218
221
219
222
** mktime()**
220
223
@@ -226,24 +229,24 @@ mktime()也是以时间元组为参数,但是它返回的不是可读性更好
226
229
>>> time.mktime(lt)
227
230
1430783729.0
228
231
229
- 返回了时间戳。就类似于localtime() 的逆过程(localtime()是以时间戳为参数)。
232
+ 返回了时间戳。就类似于 ` localtime() ` 的逆过程(` localtime() ` 是以时间戳为参数)。
230
233
231
- 以上基本能够满足编程需要了吗? 好像还缺点什么,因为在编程中,用的比较多的是“字符串”,似乎还没有将时间转化为字符串的函数。这个应该有。
234
+ 好像还缺点什么,因为在编程中,用的比较多的是“字符串”,似乎还没有将时间转化为字符串的函数。这个应该有。
232
235
233
236
** strftime()**
234
237
235
238
函数格式稍微复杂一些。
236
239
237
- > Help on built-in function strftime in module time:
238
- >
239
- > strftime(...)
240
- > strftime(format[ , tuple] ) -> string
241
- >
242
- > Convert a time tuple to a string according to a format specification.
243
- > See the library reference manual for formatting codes. When the time tuple
244
- > is not present, current time as returned by localtime() is used.
240
+ Help on built-in function strftime in module time:
241
+
242
+ strftime(...)
243
+ strftime(format[, tuple]) -> string
244
+
245
+ Convert a time tuple to a string according to a format specification.
246
+ See the library reference manual for formatting codes. When the time tuple
247
+ is not present, current time as returned by localtime() is used.
245
248
246
- 将时间元组按照指定格式要求转化为字符串。如果不指定时间元组,就默认为localtime()值。我说复杂 ,是在于其format,需要用到下面的东西。
249
+ 将时间元组按照指定格式要求转化为字符串。如果不指定时间元组,就默认为 ` localtime() ` 值。说复杂 ,是在于其format,需要用到下面的东西。
247
250
248
251
| 格式| 含义| 取值范围(格式)|
249
252
| ------| -------| -------------------|
@@ -279,15 +282,17 @@ mktime()也是以时间元组为参数,但是它返回的不是可读性更好
279
282
280
283
** strptime()**
281
284
282
- > Help on built-in function strptime in module time:
283
- >
284
- > strptime(...)
285
- > strptime(string, format) -> struct_time
286
- >
287
- > Parse a string to a time tuple according to a format specification.
288
- > See the library reference manual for formatting codes (same as strftime()).
285
+ Help on built-in function strptime in module time:
286
+
287
+ strptime(...)
288
+ strptime(string, format) -> struct_time
289
+
290
+ Parse a string to a time tuple according to a format specification.
291
+ See the library reference manual for formatting codes (same as strftime()).
292
+
293
+ ` strptime() ` 的作用是将字符串转化为时间元组。
289
294
290
- strptime()的作用是将字符串转化为时间元组。 请注意的是,其参数要指定两个,一个是时间字符串,另外一个是时间字符串所对应的格式,格式符号用上表中的。例如:
295
+ 请注意的是,其参数要指定两个,一个是时间字符串,另外一个是时间字符串所对应的格式,格式符号用上表中的。例如:
291
296
292
297
>>> today = time.strftime("%y/%m/%d")
293
298
>>> today
@@ -297,17 +302,17 @@ strptime()的作用是将字符串转化为时间元组。请注意的是,其
297
302
298
303
##datetime
299
304
300
- 虽然time模块已经能够把有关时间方面的东西搞定了 ,但是,在某些调用的时候,还感觉不是很直接,于是又出来了一个datetime模块 ,供程序猿和程序媛们选择使用。
305
+ 虽然 ` time ` 模块已经能够把有关时间方面的东西搞定了 ,但是,有时调用起来感觉不是很直接,于是又出来了一个 ` datetime ` 模块 ,供程序猿和程序媛们选择使用。
301
306
302
- datetime模块中有几个类 :
307
+ ` datetime ` 模块中有几个类 :
303
308
304
309
- datetime.date:日期类,常用的属性有year/month/day
305
310
- datetime.time:时间类,常用的有hour/minute/second/microsecond
306
311
- datetime.datetime:日期时间类
307
312
- datetime.timedelta:时间间隔,即两个时间点之间的时间长度
308
313
- datetime.tzinfo:时区类
309
314
310
- ###date类
315
+ ###` date ` 类
311
316
312
317
通过实例了解常用的属性:
313
318
@@ -316,15 +321,15 @@ datetime模块中有几个类:
316
321
>>> today
317
322
datetime.date(2015, 5, 5)
318
323
319
- 这里其实生成了一个日期对象 ,然后操作这个对象的各种属性。用print语句,可以是视觉更佳 :
324
+ 其实这里生成了一个日期对象 ,然后操作这个对象的各种属性。可以用 ` print ` ,以获得更佳视觉 :
320
325
321
- >>> print today
326
+ >>> print today #Python 3: print(today)
322
327
2015-05-05
323
- >>> print today.ctime()
328
+ >>> print today.ctime() #Python 3: print(today.ctime())
324
329
Tue May 5 00:00:00 2015
325
- >>> print today.timetuple()
330
+ >>> print today.timetuple() #Python 3: print(today.timetuple())
326
331
time.struct_time(tm_year=2015, tm_mon=5, tm_mday=5, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=1, tm_yday=125, tm_isdst=-1)
327
- >>> print today.toordinal()
332
+ >>> print today.toordinal() #Python 3: print(today.toordinal())
328
333
735723
329
334
330
335
特别注意,如果你妄图用` datetime.date.year() ` ,是会报错的,因为year不是一个方法,必须这样行:
@@ -386,27 +391,27 @@ datetime模块中有几个类:
386
391
主要用来做时间的运算。比如:
387
392
388
393
>>> now = datetime.datetime.now()
389
- >>> print now
394
+ >>> print now #Python 3: print(now)
390
395
2015-05-05 09:22:43.142520
391
396
392
- 没有讲述datetime类 ,因为在有了date和time类知识之后,这个类比较简单,我最喜欢这个now方法了 。
397
+ 没有讲述 ` datetime ` 类 ,因为在有了date和time类知识之后,这个类比较简单。
393
398
394
- 对now增加5个小时
399
+ 对 ` now ` 增加5个小时;
395
400
396
401
>>> b = now + datetime.timedelta(hours=5)
397
- >>> print b
402
+ >>> print b #Python 3: print(b)
398
403
2015-05-05 14:22:43.142520
399
404
400
- 增加两周
405
+ 增加两周;
401
406
402
407
>>> c = now + datetime.timedelta(weeks=2)
403
- >>> print c
408
+ >>> print c #Python 3: print(c)
404
409
2015-05-19 09:22:43.142520
405
410
406
411
计算时间差:
407
412
408
413
>>> d = c - b
409
- >>> print d
414
+ >>> print d #Python 3: print(d)
410
415
13 days, 19:00:00
411
416
412
417
------
0 commit comments