一、查看帮助文档
1.time帮助文档行
http://docs.python.org/library/time
2.>>>import time
>>>help(time)
time() -- return current time in seconds since the Epoch as a float
clock() -- return CPU time since process start as a float
sleep() -- delay for a number of seconds given as a float
gmtime() -- convert seconds since Epoch to UTC tuple
localtime() -- convert seconds since Epoch to local time tuple
asctime() -- convert time tuple to string
ctime() -- convert time in seconds to string
mktime() -- convert local time tuple to seconds since Epoch
strftime() -- convert time tuple to string according to format specification
strptime() -- parse string to time tuple according to format specification
tzset() -- change the local timezone
二、实例讲解
1.[localtime] 利用localtime()转换为时间数组,然后格式化为需要的格式
>>> time_second = 1428394912
>>> time.localtime(time_second)
time.struct_time(tm_year=2015, tm_mon=4, tm_mday=7, tm_hour=16, tm_min=21, tm_sec=52, tm_wday=1, tm_yday=97, tm_isdst=0)
>>> time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time_second))
'2015-04-07 16:21:52'
2.[mktime] 将字符串的时间转换为时间戳
>>> str_time = '2015-04-07 16:21:52'
>>> time.strptime(str_time,'%Y-%m-%d %H:%M:%S')
time.struct_time(tm_year=2015, tm_mon=4, tm_mday=7, tm_hour=16, tm_min=21, tm_sec=52, tm_wday=1, tm_yday=97, tm_isdst=-1)
>>>
>>> time.mktime(time.strptime(str_time,'%Y-%m-%d %H:%M:%S'))
1428394912.0
>>> int(time.mktime(time.strptime(str_time,'%Y-%m-%d %H:%M:%S')))
1428394912
3.[strftime,strptime] 格式更改函数,日期和时间的格式化参数请参考帮助文章
4.[sleep]简单不做实例
1.time帮助文档行
http://docs.python.org/library/time
2.>>>import time
>>>help(time)
time() -- return current time in seconds since the Epoch as a float
clock() -- return CPU time since process start as a float
sleep() -- delay for a number of seconds given as a float
gmtime() -- convert seconds since Epoch to UTC tuple
localtime() -- convert seconds since Epoch to local time tuple
asctime() -- convert time tuple to string
ctime() -- convert time in seconds to string
mktime() -- convert local time tuple to seconds since Epoch
strftime() -- convert time tuple to string according to format specification
strptime() -- parse string to time tuple according to format specification
tzset() -- change the local timezone
二、实例讲解
1.[localtime] 利用localtime()转换为时间数组,然后格式化为需要的格式
>>> time_second = 1428394912
>>> time.localtime(time_second)
time.struct_time(tm_year=2015, tm_mon=4, tm_mday=7, tm_hour=16, tm_min=21, tm_sec=52, tm_wday=1, tm_yday=97, tm_isdst=0)
>>> time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time_second))
'2015-04-07 16:21:52'
2.[mktime] 将字符串的时间转换为时间戳
>>> str_time = '2015-04-07 16:21:52'
>>> time.strptime(str_time,'%Y-%m-%d %H:%M:%S')
time.struct_time(tm_year=2015, tm_mon=4, tm_mday=7, tm_hour=16, tm_min=21, tm_sec=52, tm_wday=1, tm_yday=97, tm_isdst=-1)
>>>
>>> time.mktime(time.strptime(str_time,'%Y-%m-%d %H:%M:%S'))
1428394912.0
>>> int(time.mktime(time.strptime(str_time,'%Y-%m-%d %H:%M:%S')))
1428394912
3.[strftime,strptime] 格式更改函数,日期和时间的格式化参数请参考帮助文章
4.[sleep]简单不做实例