模块-时间模块(new)
模块-时间模块
- 导入:
import time |
- 方法:
_STRUCT_TM_ITEMS __doc__ __loader__ __name__ __package__ __spec__ altzone asctime clock ctime daylight get_clock_info gmtime localtime mktime monotonic perf_counter process_time sleep strftime strptime struct_time time timezone tzname |
- 常用方法:
- time.time()获得时间戳
In [3]: time.time() Out[3]: 1508852319.6068738 In [4]: help(time.time) Help on built-in function time in module time: time(...) time() -> floating point number
Return the current time in seconds since the Epoch. Fractions of a second may be present if the system clock provides them. |
- time.clock() 返回处理器时间
In [6]: time.clock() Out[6]: 4.105489737712577e-07 |
- time.gmtime()结构化时间,BUT,这个时间是标准世界时间
In [8]: time.gmtime() Out[8]: time.struct_time(tm_year=2017, tm_mon=10, tm_mday=24, tm_hour=13, tm_min=44, tm_sec=15, tm_wday=1, tm_yday=297, tm_isdst=0) |
- time.localtime() ps当前时间2017年10月24日21:49:49
In [9]: time.localtime() Out[9]: time.struct_time(tm_year=2017, tm_mon=10, tm_mday=24, tm_hour=21, tm_min=47, tm_sec=24, tm_wday=1, tm_yday=297, tm_isdst=0) In [11]: time.localtime().tm_mon Out[11]: 10
In [12]: time.localtime().tm_mday Out[12]: 24 |
- strftime() 可以只传入一个参数
In [16]: time.strftime("%Y-%m-%d %H:%M:%S",time.localtime()) Out[16]: '2017-10-24 21:59:02' In [17]: time.strftime("%Y-%m-%d %H:%M:%S") Out[17]: '2017-10-24 22:00:21' |
- strptime()将格式化时间转化为结构化时间
In [18]: time.strptime('2017-10-24 22:00:21',"%Y-%m-%d %H:%M:%S") Out[18]: time.struct_time(tm_year=2017, tm_mon=10, tm_mday=24, tm_hour=22, tm_min=0, tm_sec=21, tm_wday=1, tm_yday=297, tm_isdst=-1) |
- 将时间戳转换为时间
In [23]: time.ctime(time.time()+1000) Out[23]: 'Tue Oct 24 22:24:26 2017' |
- 将结构化时间转化成时间戳
In [25]: time.mktime(time.localtime()) Out[25]: 1508854287.0 |
- 方法总结
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 datetime
datetime.datetime.now()
datetime.datetime.today()
Tip1:时间戳转时间
In [47]: time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(123456789)) Out[47]: '1973-11-30 05:33:09' |
Tip2:时间转时间戳:
In [50]: time.mktime(time.strptime('1973-11-30 05:33:09',"%Y-%m-%d %H:%M:%S")) Out[50]: 123456789.0 |
time.time() |
return current time in seconds since the Epoch as a float |
In [52]: time.time() Out[52]: 1508856310.5973513 |
time.clock() |
return CPU time since process start as a float |
In [54]: time.clock() Out[54]: 3867.513597997835 |
time.sleep(seconds) |
delay for a number of seconds given as a float |
In [56]: time.sleep(1) |
time.gmtime(seconds=None) |
convert seconds since Epoch to UTC tuple |
In [57]: time.gmtime(123456) Out[57]: time.struct_time(tm_year=1970, tm_mon=1, tm_mday=2, tm_hour=10, tm_min=17, tm_sec=36, tm_wday=4, tm_yday=2, tm_isdst=0) |
time.localtime(seconds=None) |
convert seconds since Epoch to local time tuple |
In [60]: time.localtime(123456) Out[60]: time.struct_time(tm_year=1970, tm_mon=1, tm_mday=2, tm_hour=18, tm_min=17, tm_sec=36, tm_wday=4, tm_yday=2, tm_isdst=0) |
time.ctime(seconds=None) |
convert time in seconds to string |
In [61]: time.ctime() Out[61]: 'Tue Oct 24 22:52:54 2017' |
time.mktime(p_tuple) |
convert local time tuple to seconds since Epoch |
In [63]: time.mktime(time. localtime (123456)) Out[63]: 123456.0 |
time.strftime(format, p_tuple=None) |
convert time tuple to string according to format specification |
In[64]: time.strftime("%y-%m-%d") Out[64]: '17-10-24' |
time.strptime(string, format) |
parse string to time tuple according to format specification |
In [65]: time.strptime('17-10-24',"%y-%m-%d") Out[65]: time.struct_time(tm_year=2017, tm_mon=10, tm_mday=24, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=1, tm_yday=297, tm_isdst=-1) |
最新文章
- Android中Fragment与Activity之间的交互(两种实现方式)
- 51nod1088(最长回文子串)
- HTML5新增元素
- netcore 控制台中文乱码
- Java数据结构的特点
- 新建jsp报错“The superclass ";javax.servlet.http.HttpServlet"; was not found on the Java Build Path”
- R 语言实现牛顿下降法
- 【CodeForces 599A】D - 特别水的题4- Patrick and Shopping
- My second ";last working day";
- linux源代码阅读笔记 高速缓冲区管理
- MFC通过ADO操作Access数据库
- js 完成单继承
- html5实现拖拽文件上传
- js实现最短时间走完不同速度的路程
- 从此不再担心键盘遮住输入框OC(一)
- Freemarker的基本语法及入门基础
- 软件设计之Deep Module(深模块)
- JS实现随机背景图片与图片大小变换的效果
- python简说(一)if,for等
- Netty 源码(二)NioEventLoop 之 Channel 注册