模块-时间模块(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) |
模块-时间模块(new)的更多相关文章
- Python常用模块-时间模块(time&datetime)
Python常用模块-时间模块(time & datetime) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.初始time模块 #!/usr/bin/env pyth ...
- Python常用模块-时间模块
在写代码的过程中,我们常常需要与时间打交道,在python中,与时间处理有关的模块有time,datetime和calendar.,这里主要介绍time和datetime模块 在python中,表示时 ...
- random 模块 时间模块(time) sys模块 os模块
random 模块 1.随机小数 random.random() 0-1内的随机小数 random.uniform(1,5) 1-5范围内的随机小数 2.随机整数 random.randint( ...
- day 18 random模块 时间模块 sys模块 os模块
import random 利用random模块可以进行从一个列表或者数字范围之间随机取出一个数字 # 取随机小数 : 数学计算 print(random.random()) # 取0-1之间的小数 ...
- 常用模块(collections模块,时间模块,random模块,os模块,sys模块,序列化模块,re模块,hashlib模块,configparser模块,logging模块)
认识模块 什么是模块? 常见的场景:一个模块就是一个包含了python定义和声明的文件,文件名就是模块名字加上.py的后缀. 但其实import加载的模块分为四个通用类别: 1 使用python编写的 ...
- Python——模块——时间模块
1.time模块 (1)时间戳 >>> time.time() 1472016249.393169 (2)将时间戳转换成当前时间元祖 time.localtime()time.gmt ...
- re模块 时间模块
# 正则模块'''正则就是用一些具有特殊含义的符号组合到一起用来描述字符或字符串的方法或者说,正则就是用来描述一类事物的规则它内嵌在python中,并通过re模块实现正则表达式模式被编译成一系列的字节 ...
- random随机模块,time时间模块
random /随机模块: 作用: 在某个范围内取到每一个值得概率是相通的. 一.随机小数 random.random() import random print(random.random()) ...
- Python_Mix*random模块,time模块,sys模块,os模块
random模块 作用: 生成随机数(整数,小数,从列表中随机抽值,打乱列表顺序) 常用函数: random.random( )生成随机小数 random.uniform( )取一个范围之间的小数 r ...
随机推荐
- Spring Boot 20170913
SpringBoot 是做微服务的,比如只用来发邮件,只用来上载文件等等.优点是开发极其简单,约定大于俗成,缺点是不适合小型项目.通常用来分解大型项目,做成多个微服务. 参考: http://www. ...
- 堆————数据流的第k个大的元素
解题思路 一般地,堆和堆排序——解决 "贪心算法及其类似问题" 的利器. # 思路:我们可以用一个小根堆来做,并且限制堆的大小为k,初始化时把nums的每个数都push到堆中,如果 ...
- luogu P2584 [ZJOI2006]GameZ游戏排名系统 Splay
实在不想调了QAQ... Code: #include <cstdio> #include <algorithm> #include <cstring> #incl ...
- ESM定义模块部分export用法
//定义一个函数和变量 fonction myFunc(){}; const My_CONST=''; export {My_CONST AS THE_COMST,myFunc as THE_FUNC ...
- Numpy的使用规则
之前安装的python版本是3.7 各种库都是自己一个一个下载安装的 很操心 各种缺功能 后来发现了anaconda 啊 真是一个好东西 简单来说 它就是一个涵盖大部分常用库的python包 一次安装 ...
- -bash: wget 未找到命令的解决办法
在Linux操作系统中,我们会经常要用到wget下载文件.wget非常稳定,它在带宽很窄的情况下和不稳定网络中有很强的适应性. 在linux中使用wget时,若报-bash: wget: comman ...
- Ubuntu17.04安装WineQQ7.8及微信
安装qq2012成功,但是提示版本过低,qq登录失败. 安装WineQQ WineQQ7.8下载 安装依赖软件,方法来源网上 32位ubuntu:sudo apt install libgtk-3-0 ...
- 【MFC设置静态文本框背景为透明】
视图类中加入OnCtlColor()函数: IDC_STATIC1为静态文本框ID HBRUSH CAngleView::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT n ...
- Extjs4.2 ajax请求url中传中文參数乱码问题
今天有个需求须要在url中传入中文參数.结果在后台取得时出现乱码,怀疑可能是编码问题.上网查询了资料,试了几种办法.发现有一种可行,记录在此,以便查阅. url中用encodeURI 进行2次编码: ...
- _00017 Kafka的体系结构介绍以及Kafka入门案例(0基础案例+Java API的使用)
博文作者:妳那伊抹微笑 itdog8 地址链接 : http://www.itdog8.com(个人链接) 博客地址:http://blog.csdn.net/u012185296 博文标题:_000 ...