1. 简介与说明
- 在time模块中,时间的表示有四种方式:
- 时间戳形式:以Unix纪元(1970年1月1日0点)作为原点,按与之相差的秒数表示;
- 24位字符串形式:包含年月日,时分秒以及星期几。如’Sun Nov 13 10:01:31 2016’;
- struct_time形式:一个九元素的数组结构,包含年月日,时分秒,一周的第几天,一年中的第几天以及是否是夏令时。如time.struct_time(tm_year=2016, tm_mon=11, tm_mday=13,tm_hour=2, tm_min=1, tm_sec=27, tm_wday=6, tm_yday=318, tm_isdst=0);
- 自定义形式:这是经过strftime函数转化后的一种自定义的时间模式,如’2016-11-13 02:39:55’。
2. 相关函数
获取当前时间
时间戳形式:time()
24位字符串形式: asctime() 和 ctime()
struct_time形式:gmtime() 和 localtime()
时间表示形式的转换
- 时间戳转24位字符串:ctime(timestamp)
- 时间戳转struct_time:gmtime(timestamp)和localtime(timestamp)
- struct_time转24位字符串:asctime(struct_time)
- struct_time转自定义格式:strftime(format[,t])
- 自定义格式转struct_time格式:strptime(string[,format])
- 时间戳转24位字符串:ctime(timestamp)
3. 总结
用dia画了幅图,保存的时候乱码。结果只能呈现出这个样子了。
这样就什么都看不出来了。只能在dia中截图了。下面是上面的截图样子。
关于time模块获取当前时间以及不同形式之间的转换就用这幅图做总结了。