笔记-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. iphone 微信下浏览器中数字去除下划线

    在开发iphone应用程序的时候,safari下手机号码默认是有下划线的,通过下面的方法就可以去掉: <meta name="format-detection" conten ...

  2. 关于如何等待一个元素的出现而不用一些笨拙粗暴的time.sleep()方法

    我相信这是一个非常大众化的需求,我们需要等待某一个元素的出现以此来让我们的脚本进入到下一个Step,这个等待方法最好能够设置超时时间,然后找到后迅速callback.我们也很幸运!如果你仔细看Sele ...

  3. mysql-5.7安装配置指导

    mysql 安装 yum 安装mysql 源码编译安装mysql 通过yum安装 下载yum仓库配置安装包 MySQL Yum Repository http://dev.mysql.com/down ...

  4. K星异客

    http://baike.baidu.com/view/222058.htm 这部改编自基恩·布汝尔1995年出版的同名小说的电影在当年的十月档票房榜上称冠.本来这部电影的外星人主人公属意于威尔.史密 ...

  5. 笨办法学Python(二十八)

    习题 28: 布尔表达式练习 上一节你学到的逻辑组合的正式名称是“布尔逻辑表达式(boolean logic expression)”.在编程中,布尔逻辑可以说是无处不在.它们是计算机运算的基础和重要 ...

  6. 【js基础修炼之路】- 微任务,宏任务和Event-Loop

    一段代码让你了解Event-Loop console.log(1); setTimeout(() => { console.log(2); }, 0); new Promise((resolve ...

  7. IOS Git源代码管理工具

    .新建一个“本地仓库” $ git init .配置仓库 >告诉git你是谁 git config user.name lnj >告诉git怎么联系你 git config user.em ...

  8. Performing User-Managed Database-18.5、Restoring Control Files

    版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/offbeatmine/article/details/28429339 18.5.Restoring ...

  9. 使用MongoDB 2.6 C++驱动中的连接池

    .post p{text-indent: 2em;} MongoDB2.6的CXX驱动(mongo-cxx-driver-26compat),内置包含了数据库连接池,方便管理数据库连接,但是官方文档说 ...

  10. jQuery deferred 使用心得

    因为项目的原因,我接触到了jQuery deferred 的这个神奇的工具,下面我用几个例子,与大家分享我的感悟. 我们有5个很耗时的函数 分别为fA.fB.fC.fD.fE  我们的需求是fA和fB ...