术语
- UTC is Coordinated Universal Time (formerly known as Greenwich Mean Time, or GMT). The acronym UTC is not a mistake but a compromise between English and French.
- DST is Daylight Saving Time, an adjustment of the timezone by (usually) one hour during part of the year. DST rules are magic (determined by local law) and can change from year to year. The C library has a table containing the local rules (often it is read from a system file for flexibility) and is the only source of True Wisdom in this respect.
- epoch
January 1, 1970, 00:00:00 (UTC)
用法
-
1.time.sleep()
阻塞当前线程
>>>time.sleep(10)
-
2.time.time()
返回epoch到当前的秒数
>>>time.time()
1489194406.9268734
-
3.time.clock()
返回CPU时间, 用于测试, 但不推荐
>>>time.clock()
0.12254
-
4.time.gmtime(), time.localtime()
返回struct_time, 相差8h
tm_sec取值范围为[0,61], 60表示闰秒, 61是历史保留
tm_wday取值范围[0,6], 0表示星期一
>>>time.gmtime()
time.struct_time(tm_year=2017, tm_mon=3, tm_mday=11, tm_hour=1, tm_min=7, tm_sec=44, tm_wday=5, tm_yday=70, tm_isdst=0)
>>>time.localtime()
time.struct_time(tm_year=2017, tm_mon=3, tm_mday=11, tm_hour=9, tm_min=8, tm_sec=49, tm_wday=5, tm_yday=70, tm_isdst=0)
-
5.time.ctime()
>>> time.ctime()
'Sat Mar 11 09:46:52 2017'
-
6.time.strptime()
str -> struct_time
>>>time.strptime(time.ctime())
time.struct_time(tm_year=2017, tm_mon=3, tm_mday=11, tm_hour=9, tm_min=48, tm_sec=33, tm_wday=5, tm_yday=70, tm_isdst=-1)
-
7.time.strftime()
struct_time -> str
>>> time.strftime("%Y-%b-%d %H:%M:%S",time.localtime())
'2017-Mar-11 09:56:20'
-
8.time.mktime()
struct_time -> float
>>> time.mktime(time.localtime())
1489197669.0
-
9.time.timezone
时区
>>> time.timezone/3600
-8.0