笔记-python-standard library-16.3 time
笔记-python-standard library-16.3 time
1. time
1.1. 开始
time模块中时间表现的格式主要有三种:
- timestamp时间戳,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量
- struct_time时间元组,共有九个元素组。
- format time 格式化时间,已格式化的结构使时间更具可读性。包括自定义格式和固定格式。
时间戳单位最适于做日期运算。但是1970年之前的日期就无法以此表示了。太遥远的日期也不行,UNIX和Windows只支持到2038年。
1.2. 时间格式转换图

1.3. 常用时间格式和转换方法
import time
# timestamp
t = time.time()
# 从时间组转换为时间戳
t_m = time.mktime(time.localtime())
#print(t)
#print(t_m)
# 时间戳转本地时间,可带参数可不带,不带就是当前时间了
t1 = time.localtime(t)
#print(t1)
# 时间戳转格林尼治时间,同上一函数
t2 = time.gmtime(t)
#print(t2)
# 格式化输出
# %b 表示英文缩写月份,还有很多格式
# 常规的就下面一行了,更多格式化内容查看文档
t_s1 = time.strftime('%Y-%m-%d %H:%M:%S',t1)
# 另一种格式化,是一种特定格式
t_s2 = time.asctime(t1)
#print(t_s1)
#print(t_s2)
# 字符串转时间
# time.strftime(fmt[,tupletime])
# 接收以时间元组,并返回以可读字符串表示的当地时间,格式由fmt决定。
print (time.strftime("%Y-%m-%d %H:%M:%S",
time.localtime()))
# time.strptime(str,fmt='%a %b %d %H:%M:%S %Y')
# 根据fmt的格式把一个时间字符串解析为时间元组。
struct_time = time.strptime("30 Nov 00", "%d
%b %y")
print("返回元组: ", struct_time)
# 休眠,单位秒,支持浮点数
# 注意:一般会比给定时间长,因为还有系统调用的影响。
time.sleep()
笔记-python-standard library-16.3 time的更多相关文章
- Python Standard Library
Python Standard Library "We'd like to pretend that 'Fredrik' is a role, but even hundreds of vo ...
- The Python Standard Library
The Python Standard Library¶ While The Python Language Reference describes the exact syntax and sema ...
- Python语言中对于json数据的编解码——Usage of json a Python standard library
一.概述 1.1 关于JSON数据格式 JSON (JavaScript Object Notation), specified by RFC 7159 (which obsoletes RFC 46 ...
- 《The Python Standard Library》——http模块阅读笔记1
官方文档:https://docs.python.org/3.5/library/http.html 偷个懒,截图如下: 即,http客户端编程一般用urllib.request库(主要用于“在这复杂 ...
- 《The Python Standard Library》——http模块阅读笔记2
http.server是用来构建HTTP服务器(web服务器)的模块,定义了许多相关的类. 创建及运行服务器的代码一般为: def run(server_class=HTTPServer, handl ...
- 《The Python Standard Library》——http模块阅读笔记3
http.cookies — HTTP state management http.cookies模块定义了一系列类来抽象cookies这个概念,一个HTTP状态管理机制.该模块支持string-on ...
- Python Standard Library 学习(一) -- Built-in Functions 内建函数
内建函数列表 Built-in Functions abs() divmod() input() open() staticmethod() all() enumerate() int() ord() ...
- [译]The Python Tutorial#11. Brief Tour of the Standard Library — Part II
[译]The Python Tutorial#Brief Tour of the Standard Library - Part II 第二部分介绍更多满足专业编程需求的高级模块,这些模块在小型脚本中 ...
- [译]The Python Tutorial#10. Brief Tour of the Standard Library
[译]The Python Tutorial#Brief Tour of the Standard Library 10.1 Operating System Interface os模块为与操作系统 ...
- C++11新特性——The C++ standard library, 2nd Edition 笔记(一)
前言 这是我阅读<The C++ standard library, 2nd Edition>所做读书笔记的第一篇.这个系列基本上会以一章一篇的节奏来写,少数以C++03为主的章节会和其它 ...
随机推荐
- React 表单组件
诸如 <input>.<textarea>.<option> 这样的表单组件不同于其他组件,因为他们可以通过用户交互发生变化.这些组件提供的界面使响应用户交互的表单 ...
- input type="file"获取文件名方法
文件上传比较丑,样式调整时会有一个获取文件名,或者包含文件路径的文件名的方法 html代码 <div class="file-box"> <form id=&qu ...
- 模拟Chrome皮肤
话不多说,先验货: (原始状态) (最大化状态) (对比图) 为自己鼓掌!!! 哈哈,捣鼓2天终于把这个搞出来了!虽然代码一团糟,但是不难理解! 要实现这个功能需要几个组件:DWM,GDI+ 在实现这 ...
- ubuntu linux安装octave
sudo apt-add-repository ppa:octave/stable sudo apt-get update sudo apt-get install octave 安装完成后,在终端中 ...
- 入坑Ubuntu手记-系统安装和简单配置
对于开发者而言,Linux的环境帮助是非常大的.同样的,Linux对很多Windows下的软件,尤其是游戏不支持,这也是一个非常重要的生产力的因素.嗯…我可能就是为了控制自己少玩游戏,直接上一个Ubu ...
- url获取MVC域,action,controller的方法
域:filterContext.RequestContext.RouteData.DataTokens["area"] 控制器:filterContext.RequestConte ...
- http主要内容流程介绍
从这张图可以看出,从输入网址,按下回车的那一刻起,就开始了Redirect,那么为什么一开始就redirect呢?因为我们的浏览器可能记录了你的这个地址,以及永久跳转成一个新的地址了,所以一开始浏览器 ...
- libtool: Version mismatch error. 解决方法
在编译一个软件的时候,在 ./configure 和 make 之后可能会出现如下错误: libtool: Version mismatch error. This is libtool 2.4. ...
- 正则表达式 /i /g /m /ig /gi
正则表达式中/i,/g,/ig,/gi,/m的区别和含义 /i (忽略大小写) /g (全文查找出现的所有匹配字符) /m (多行查找) / /ig(全文查找.忽略大小写)
- Node.js 的初体验
例子1: 1.首先第一步 :要 下载 node.js. 官网 上可以下载 下载完后,是这个玩意. 2. 打开 node.js ,然后输入 // 引入http模块 var http = require( ...