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
import time
time.time():获取当前时间的时间戳
time.localtime():接受一个时间戳,并把它转化为一个当前时间的元组。不给参数的话就会默认将time.time()作为参数传入
>>> time.localtime()
time.struct_time(tm_year=2015, tm_mon=4, tm_mday=1, tm_hour=16, tm_min=48, tm_se
c=47, tm_wday=2, tm_yday=91, tm_isdst=0)
time.localtime(): | ||
索引 | 属性 | 含义 |
0 | tm_year | 年 |
1 | tm_mon | 月 |
2 | tm_mday | 日 |
3 | tm_hour | 时 |
4 | tm_min | 分 |
5 | tm_sec | 秒 |
6 | tm_wday | 一周中的第几天 |
7 | tm_yday | 一年中的第几天 |
8 | tm_isdst | 夏令时 |
time.mktime():和time.localtime()相反,它把一个时间元组转换成时间戳(这个必须要给一个参数)
time.asctime():把一个时间元组表示为:“Sun Jul 28 03:35:26 2013”这种格式,不给参数的话就会默认将time.localtime()作为参数传入
time.ctime():把一个时间戳转换为time.asctime()的表达格式,不给参数的话就会默认将time.time()作为参数传入
time.gmtime():将一个时间戳转换为UTC+0时区(中国应该是+8时区,相差8个小时)的时间元组,不给参数的话就会默认将time.time()作为参数传入
time.strftime(format,time.localtime()):将一个时间元组转换为格式化的时间字符,不给时间元组参数的话就会默认将time.localtime()作为参数传入
例如web日志里面的时间格式就是time.strftime('%d/%b/%Y:%X')
返回结果:Sun Jul 28 04:37:38 2013
format:
属性 | 格式 | 含义 | 取值范围(格式) |
年份 | %y | 去掉世纪的年份 | 00-99 |
%Y | 完整的年份 | ||
%j | 一年中的第几天 | 001-366 | |
月份 | %m | 月份 | 1月12日 |
%b | 本地简化月份的名称 | 简写英文月份 | |
%B | 本地完整月份的名称 | 完整英文月份 | |
日期 | %d | 一个月中的第几天 | 1月31日 |
小时 | %H | 一天中的第几个小时(24小时制) | 00-23 |
%l | 第几个小时(12小时制) | “01-12” | |
分钟 | %M | 分钟数 | 00-59 |
秒 | %S | 秒 | 00-59 |
星期 | %U | 一年中的星期数(从星期天开始算) | 00-53 |
%W | 一年中的星期数(从星期一开始算) | ||
%w | 一个星期的第几天 | 0-6 | |
时区 | %Z | 中国:应该是GMT+8(中国标准时间) | 求大神扫盲 |
其他 | %x | 本地相应日期 | 日/月/年 |
%X | 本地相印时间 | 时:分:秒 | |
%c | 详细日期时间 | 日/月/年 时:分:秒 | |
%% | ‘%’字符 | ‘%’字符 | |
%p | 本地am或者pm的相应符 | AM or PM |
time.strptime(stringtime,format):将时间字符串根据指定的格式化符转换成数组形式的时间,
例如:time.strptime('28/Jul/2013:04:33:29', '%d/%b/%Y:%X')
返回结果:time.struct_time(tm_year=2013, tm_mon=7, tm_mday=28, tm_hour=4, tm_min=33, tm_sec=29, tm_wday=6, tm_yday=209, tm_isdst=-1)
time.clock():返回处理器时钟时间,一般用于性能测试和基准测试等,因为他们反映了程序使用的实际时间,平常用不到这个。
time.sleep():推迟指定的时间运行,单位为秒
转载于:https://blog.51cto.com/iamxiao/1627378