笔记-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为主的章节会和其它 ...
随机推荐
- Unified Service Desk Overview
As we implement CRM in enterprise, we usually integrate with many other information system such as E ...
- Developer - 如何自我保证Node.js模块质量
组里正在做SaaS产品,其中一些模块(Module)是Node.js实现,这里我们主要使用Node.js实现Web Server来提供服务. 在做SaaS项目之前,组里的开发模式是传统的Deverlo ...
- Unity利用AnimationCurve做物体的各种运动
之前一直都是自己学习Unity各种做Demo,最近开始正式使用Unity来做一个款2d的游戏. 其中在做一个类似小球弹跳运动的时候遇到了点问题,查找了很多资料,无意间发现AnimationCurve ...
- 只为更快、更省、更安全的 Azure CDN
来来来!小编今天要公布一件大事啦: 经过最近一次更新,Azure CDN 高级版服务 HTTPS SSL 证书的申请方式有所改进啦,除了现有的 Azure CDN 代为申请证书外,还支持用户自己申请的 ...
- 酷狗音乐PC端怎么使用听歌识曲功能?
生活中很多时候会听到一些美妙的音乐,耳熟或者动听却不知道它的名字.就像第一眼看到你心动的那个她却不知她叫什么.移动端有酷狗音乐的听歌识曲.现在PC端也有了相同的功能,每当我们看到一部精彩影视剧听到美妙 ...
- Thymeleaf 随记
一.基础写法: th:text='${数据} ,其中text可以修改成其他,如href,value,class....看需求 <p th:text='${后台返回的数据}'>静态文本&l ...
- April 6 2017 Week 14 Thursday
If you smile when no one else is around, you really mean it. 独处时的微笑,才是发自内心的. Recently I found I seld ...
- 使用selenium grid与BrowserMobProxyServer联合使用
背景:项目主要是做埋点数据,要使用 BrowserMobProxyServer,它相当于做一个代理,在你访问一个网页时,通过代理,获取打开网页的数据,对比你需要对比数据,所以这个工具提供获取页面请求的 ...
- eclipse 集成jdk
最近想整合一个工具,eplise中包含了 pc 自动化可用的一套环境,让其他测试人员,下载下来就可以用,不需要在进行安装其他东西,jdk安装也不需要,这事可有些犯难,eplise集成了svn和test ...
- python读取mat文件
一.mat文件 mat数据格式是Matlab的数据存储的标准格式.在Matlab中主要使用load()函数导入一个mat文件,使用save()函数保存一个mat文件.对于文件 load('data.m ...