44
55“一寸光阴一寸金,寸金难买寸光阴”,时间是宝贵的。
66
7- 在日常生活中,“时间”这个属于是比较笼统和含糊的 。在物理学中,“时间”是一个非常明确的概念。在python中 ,“时间”可以通过相关模块实现。
7+ 在日常生活中,“时间”这个术语是比较笼统和含糊的 。在物理学中,“时间”是一个非常明确的概念。在Python中 ,“时间”可以通过相关模块实现。
88
99##calendar
1010
1919 19 20 21 22 23 24 25
2020 26 27 28 29 30 31
2121
22- 轻而易举得到了2015年1月的日历,并且排列的还那么整齐。这就是calendar模块。读者可以用dir() 去查看这个模块下的所有内容。为了让读者阅读方便,将常用的整理如下:
22+ 轻而易举得到了2015年1月的日历,并且排列的还那么整齐。这就是 ` calendar ` 模块。读者可以用 ` dir() ` 去查看这个模块下的所有内容。为了让读者阅读方便,将常用的整理如下:
2323
2424** calendar(year,w=2,l=1,c=6)**
2525
26- 返回year年年历 ,3个月一行,间隔距离为c。 每日宽度间隔为w字符。每行长度为21 * W +18+2* C 。l是每星期行数。
26+ 返回year年的年历 ,3个月一行,间隔距离为c。 每日宽度间隔为w字符。每行长度为 ` 21* w +18+2* c ` 。l是每星期行数。
2727
2828 >>> year = calendar.calendar(2015)
2929 >>> print year
6464 26 27 28 29 30 31 23 24 25 26 27 28 29 28 29 30 31
6565 30
6666
67+ 其它部分就是按照上面的样式,将2015年度的各个月份日历完全显示出来。
68+
6769** isleap(year)**
6870
6971判断是否为闰年,是则返回true,否则false.
7577
7678怎么判断一年是闰年,常常见诸于一些编程语言的练习题,现在用一个方法搞定。
7779
78- ** leapdays(y1,y2)**
80+ ** leapdays(y1, y2)**
7981
80- 返回在Y1,Y2两年之间的闰年总数 ,包括y1,但不包括y2,这有点如同序列的切片一样。
82+ 返回在y1,y2两年之间的闰年总数 ,包括y1,但不包括y2,这有点如同序列的切片一样。
8183
82- >>> calendar.leapdays(2000,2004)
84+ >>> calendar.leapdays(2000, 2004)
8385 1
84- >>> calendar.leapdays(2000,2003)
86+ >>> calendar.leapdays(2000, 2003)
8587 1
8688
87- ** month(year,month,w=2,l=1)**
89+ ** month(year, month, w=2, l=1)**
8890
89- 返回year年month月日历,两行标题,一周一行。每日宽度间隔为w字符。每行的长度为7* w+6。 l是每星期的行数。
91+ 返回year年month月日历,两行标题,一周一行。每日宽度间隔为w字符。每行的长度为7* w+6, l是每星期的行数。
9092
9193 >>> print calendar.month(2015, 5)
9294 May 2015
99101
100102** monthcalendar(year,month)**
101103
102- 返回一个列表,列表内的元素还是列表,这叫做嵌套列表 。每个子列表代表一个星期,都是从星期一到星期日,如果没有本月的日期,则为0。
104+ 返回一个列表,列表内的元素还是列表。每个子列表代表一个星期,都是从星期一到星期日,如果没有本月的日期,则为0。
103105
104106 >>> calendar.monthcalendar(2015, 5)
105107 [[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]]
106108
107109读者可以将这个结果和` calendar.month(2015, 5) ` 去对照理解。
108110
109- ** monthrange(year,month)**
111+ ** monthrange(year, month)**
110112
111113返回一个元组,里面有两个整数。第一个整数代表着该月的第一天从星期几是(从0开始,依次为星期一、星期二,直到6代表星期日)。第二个整数是该月一共多少天。
112114
128130
129131** time()**
130132
131- time模块是常用的。
133+ ` time ` 模块很常用,比如记录某个程序运行时间的长短等,下面一一道来其中的方法。
134+
135+ - ` time() `
132136
133137 >>> import time
134138 >>> time.time()
135139 1430745298.391026
136140
137- ` time.time() ` 获得的是当前时间(严格说是时间戳),只不过这个时间对人不友好,它是以1970年1月1日0时0分0秒为计时起点,到当前的时间长度(不考虑闰秒)
141+ ` time.time() ` 获得的是当前时间(严格说是时间戳),只不过这个时间对人不友好,它是以1970年1月1日0时0分0秒为计时起点,到当前的时间长度(不考虑闰秒)。
138142
139143> UNIX時間,或稱POSIX時間是UNIX或類UNIX系統使用的時間表示方式:從協調世界時1970年1月1日0時0分0秒起至現在的總秒數,不考慮閏秒
140144
141145> 現時大部分使用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年)。這時可能會令軟體發生問題,導致系統癱瘓。
142146
143-
144147> 目前的解決方案是把系統由32位元轉為64位元系統。在64位系統下,此時間最多可以表示到292,277,026,596年12月4日15時30分08秒。
145148
146149有没有对人友好一点的时间显示呢?
@@ -168,7 +171,7 @@ time模块是常用的。
168171 >>> t[1]
169172 5
170173
171- 通过索引,能够得到相应的属性 ,上面的例子中就得到了当前时间的月份。
174+ 通过索引能够得到相应的属性 ,上面的例子中就得到了当前时间的月份。
172175
173176其实,` time.localtime() ` 不是没有参数,它在默认情况下,以` time.time() ` 的时间戳为参数。言外之意就是说可以自己输入一个时间戳,返回那个时间戳所对应的时间(按照公元和时分秒计时)。例如:
174177
@@ -185,7 +188,7 @@ localtime()得到的是本地时间,如果要国际化,就最好使用格林
185188
186189> 格林威治標準時間(中國大陸翻譯:格林尼治平均時間或格林尼治標準時間,台、港、澳翻譯:格林威治標準時間;英语:Greenwich Mean Time,GMT)是指位於英國倫敦郊區的皇家格林威治天文台的標準時間,因為本初子午線被定義在通過那裡的經線。
187190
188- 还有更友好的:
191+ 还有更友好的,请继续阅读。
189192
190193** asctime()**
191194
@@ -200,7 +203,7 @@ localtime()得到的是本地时间,如果要国际化,就最好使用格林
200203 >>> time.asctime(h)
201204 'Mon Jan 12 21:46:40 1970'
202205
203- 注意,` time.asctime() ` 的参数必须是时间元组,类似上面那种。不是时间戳,通过` time.time() ` 得到的时间戳,也可以转化为上面形式:
206+ 注意,` time.asctime() ` 的参数必须是时间元组,类似上面那种。不是时间戳,通过` time.time() ` 得到的时间戳也可以转化为上面形式。
204207
205208** ctime()**
206209
@@ -212,9 +215,9 @@ localtime()得到的是本地时间,如果要国际化,就最好使用格林
212215 >>> time.ctime(1000000)
213216 'Mon Jan 12 21:46:40 1970'
214217
215- 跟前面得到的结果是一样的。只不过是用了时间戳作为参数 。
218+ 跟前面得到的结果是一样的,只不过用了时间戳作为参数 。
216219
217- 在前述函数中,通过localtime()、 gmtime()得到的是时间元组,通过time() 得到的是时间戳。有的函数如asctime() 是以时间元组为参数,有的如ctime()是以时间戳为函数。 这样做的目的是为了满足编程中多样化的需要。
220+ 在前述函数中,通过 ` localtime() ` 、 ` gmtime() ` 得到的是时间元组,通过 ` time() ` 得到的是时间戳。有的函数如 ` asctime() ` 是以时间元组为参数,有的如 ` ctime() ` 是以时间戳为参数, 这样做的目的是为了满足编程中多样化的需要。
218221
219222** mktime()**
220223
@@ -226,24 +229,24 @@ mktime()也是以时间元组为参数,但是它返回的不是可读性更好
226229 >>> time.mktime(lt)
227230 1430783729.0
228231
229- 返回了时间戳。就类似于localtime() 的逆过程(localtime()是以时间戳为参数)。
232+ 返回了时间戳。就类似于 ` localtime() ` 的逆过程(` localtime() ` 是以时间戳为参数)。
230233
231- 以上基本能够满足编程需要了吗? 好像还缺点什么,因为在编程中,用的比较多的是“字符串”,似乎还没有将时间转化为字符串的函数。这个应该有。
234+ 好像还缺点什么,因为在编程中,用的比较多的是“字符串”,似乎还没有将时间转化为字符串的函数。这个应该有。
232235
233236** strftime()**
234237
235238函数格式稍微复杂一些。
236239
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.
245248
246- 将时间元组按照指定格式要求转化为字符串。如果不指定时间元组,就默认为localtime()值。我说复杂 ,是在于其format,需要用到下面的东西。
249+ 将时间元组按照指定格式要求转化为字符串。如果不指定时间元组,就默认为 ` localtime() ` 值。说复杂 ,是在于其format,需要用到下面的东西。
247250
248251| 格式| 含义| 取值范围(格式)|
249252| ------| -------| -------------------|
@@ -279,15 +282,17 @@ mktime()也是以时间元组为参数,但是它返回的不是可读性更好
279282
280283** strptime()**
281284
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() ` 的作用是将字符串转化为时间元组。
289294
290- strptime()的作用是将字符串转化为时间元组。 请注意的是,其参数要指定两个,一个是时间字符串,另外一个是时间字符串所对应的格式,格式符号用上表中的。例如:
295+ 请注意的是,其参数要指定两个,一个是时间字符串,另外一个是时间字符串所对应的格式,格式符号用上表中的。例如:
291296
292297 >>> today = time.strftime("%y/%m/%d")
293298 >>> today
@@ -297,17 +302,17 @@ strptime()的作用是将字符串转化为时间元组。请注意的是,其
297302
298303##datetime
299304
300- 虽然time模块已经能够把有关时间方面的东西搞定了 ,但是,在某些调用的时候,还感觉不是很直接,于是又出来了一个datetime模块 ,供程序猿和程序媛们选择使用。
305+ 虽然 ` time ` 模块已经能够把有关时间方面的东西搞定了 ,但是,有时调用起来感觉不是很直接,于是又出来了一个 ` datetime ` 模块 ,供程序猿和程序媛们选择使用。
301306
302- datetime模块中有几个类 :
307+ ` datetime ` 模块中有几个类 :
303308
304309- datetime.date:日期类,常用的属性有year/month/day
305310- datetime.time:时间类,常用的有hour/minute/second/microsecond
306311- datetime.datetime:日期时间类
307312- datetime.timedelta:时间间隔,即两个时间点之间的时间长度
308313- datetime.tzinfo:时区类
309314
310- ###date类
315+ ###` date ` 类
311316
312317通过实例了解常用的属性:
313318
@@ -316,15 +321,15 @@ datetime模块中有几个类:
316321 >>> today
317322 datetime.date(2015, 5, 5)
318323
319- 这里其实生成了一个日期对象 ,然后操作这个对象的各种属性。用print语句,可以是视觉更佳 :
324+ 其实这里生成了一个日期对象 ,然后操作这个对象的各种属性。可以用 ` print ` ,以获得更佳视觉 :
320325
321- >>> print today
326+ >>> print today #Python 3: print(today)
322327 2015-05-05
323- >>> print today.ctime()
328+ >>> print today.ctime() #Python 3: print(today.ctime())
324329 Tue May 5 00:00:00 2015
325- >>> print today.timetuple()
330+ >>> print today.timetuple() #Python 3: print(today.timetuple())
326331 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())
328333 735723
329334
330335特别注意,如果你妄图用` datetime.date.year() ` ,是会报错的,因为year不是一个方法,必须这样行:
@@ -386,27 +391,27 @@ datetime模块中有几个类:
386391主要用来做时间的运算。比如:
387392
388393 >>> now = datetime.datetime.now()
389- >>> print now
394+ >>> print now #Python 3: print(now)
390395 2015-05-05 09:22:43.142520
391396
392- 没有讲述datetime类 ,因为在有了date和time类知识之后,这个类比较简单,我最喜欢这个now方法了 。
397+ 没有讲述 ` datetime ` 类 ,因为在有了date和time类知识之后,这个类比较简单。
393398
394- 对now增加5个小时
399+ 对 ` now ` 增加5个小时;
395400
396401 >>> b = now + datetime.timedelta(hours=5)
397- >>> print b
402+ >>> print b #Python 3: print(b)
398403 2015-05-05 14:22:43.142520
399404
400- 增加两周
405+ 增加两周;
401406
402407 >>> c = now + datetime.timedelta(weeks=2)
403- >>> print c
408+ >>> print c #Python 3: print(c)
404409 2015-05-19 09:22:43.142520
405410
406411计算时间差:
407412
408413 >>> d = c - b
409- >>> print d
414+ >>> print d #Python 3: print(d)
410415 13 days, 19:00:00
411416
412417------
0 commit comments