笔记-python-standard library-16.3 time

1.      time

1.1.    开始

time模块中时间表现的格式主要有三种:

  1. timestamp时间戳,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量
  2. struct_time时间元组,共有九个元素组。
  3. 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的更多相关文章

  1. Python Standard Library

    Python Standard Library "We'd like to pretend that 'Fredrik' is a role, but even hundreds of vo ...

  2. The Python Standard Library

    The Python Standard Library¶ While The Python Language Reference describes the exact syntax and sema ...

  3. Python语言中对于json数据的编解码——Usage of json a Python standard library

    一.概述 1.1 关于JSON数据格式 JSON (JavaScript Object Notation), specified by RFC 7159 (which obsoletes RFC 46 ...

  4. 《The Python Standard Library》——http模块阅读笔记1

    官方文档:https://docs.python.org/3.5/library/http.html 偷个懒,截图如下: 即,http客户端编程一般用urllib.request库(主要用于“在这复杂 ...

  5. 《The Python Standard Library》——http模块阅读笔记2

    http.server是用来构建HTTP服务器(web服务器)的模块,定义了许多相关的类. 创建及运行服务器的代码一般为: def run(server_class=HTTPServer, handl ...

  6. 《The Python Standard Library》——http模块阅读笔记3

    http.cookies — HTTP state management http.cookies模块定义了一系列类来抽象cookies这个概念,一个HTTP状态管理机制.该模块支持string-on ...

  7. Python Standard Library 学习(一) -- Built-in Functions 内建函数

    内建函数列表 Built-in Functions abs() divmod() input() open() staticmethod() all() enumerate() int() ord() ...

  8. [译]The Python Tutorial#11. Brief Tour of the Standard Library — Part II

    [译]The Python Tutorial#Brief Tour of the Standard Library - Part II 第二部分介绍更多满足专业编程需求的高级模块,这些模块在小型脚本中 ...

  9. [译]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模块为与操作系统 ...

  10. C++11新特性——The C++ standard library, 2nd Edition 笔记(一)

    前言 这是我阅读<The C++ standard library, 2nd Edition>所做读书笔记的第一篇.这个系列基本上会以一章一篇的节奏来写,少数以C++03为主的章节会和其它 ...

随机推荐

  1. oracle笔记1:-安装-体系结构-基本查询

    一.Oracle数据库的安装(安装在虚拟机的xp系统里)     1.安装虚拟机     2.虚拟机内安装xp系统,以下步骤在虚拟机内进行     3.双击setup.exe,在出现的界面中设置口令, ...

  2. Android 监听APP进入后台或切换到前台方案对比

    在我们开发的过程中,经常会遇到需要我们判断app进入后台,或者切换到前台的情况.比如我们想判断app切换到前台时,显示一个解锁界面,要求用户输入解锁密码才能继续进行操作:我们想判断app切换到后台,记 ...

  3. scss牛刀小试:解决css中适配浏览器前缀问题

    在css中为适配浏览器,新特性总加 -webkit,-o, -moz 来适配浏览器,写的烦心,看着也臃肿,让css可读性降低,下面以阴影为例,如何使用scss让我们的css看起来更简洁. 本人使用的I ...

  4. WiFi调试手机

     转自http://blog.csdn.net/Yejianyun1/article/details/55511726 使用场景: 1.多设备执行测试用例 2.数据线无法满足使用 电脑与手机的网络需要 ...

  5. HCNA配置接口IP地址

    1.拓扑图 2.R1配置 The device is running! <Huawei>sys <Huawei>system-view Enter system view, r ...

  6. PHP 简单调用rest WebServices

    <?php $ch = curl_init("http://api.cachk.com:8185/Tech-Trans.esPOS4.PosService.UAT/rest/enqui ...

  7. phpStudy:使用localhost无法访问的解决方案

    今天遇到新的问题,很有意思,当使用Localhost时,发现报403错误: 百度找到问题所在:没有权限问题 所以我们打开phpStudy,找到配置文件“vhosts-conf”,看到的情况是这样的 接 ...

  8. hdu-2838 Cow Sorting---逆序对的花费

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2838 题目大意: 就是求将之前的排列变成一个递增的排列,每交换两个数的代价为两个数的和,求变成递增的 ...

  9. HashMap扩容

    前言:当您在读该文章的时候,我认为您已经知道HashMap的底层实现原理,如果您还不清楚HashMap是如何实现的,请先去了解,再回来看本文章. 1.HashMap什么时候扩容? HashMap的容量 ...

  10. npy数据的保存与读取

    保存 利用这种方法,保存文件的后缀名字一定会被置为.npy x = numpy.save("data_x.npy",x) 读取 data = numpy.load("da ...