第十章、time模块
第十章、模块
time模块
import time
时间戳
表示:是从1970年1月1日00:00:00开始按秒计算的偏移量。
time_stamp = time.time()
print(time_stamp, type(time_stamp)
1566196770.3494985 <class 'float'>
格式化时间
表示:格式化的时间字符串
format_time=time.strftime("%Y-%m-%d %X")
print(format_time))
2019-08-19 14:43:50
结构化时间
表示:struct_time元组共有9个元素共九个元素,分别为(年,月,日,时,分,秒,一年中第几周,一年中第几天,夏令时)
print('本地时区struct_time:\n{}'.format(time.localtime()))
print(time.localtime(0))#基准时间
time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=8, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=1, tm_isdst=0)
# 结构化时间的基准时间上增加一年时间
print(time.localtime(3600*24*365))
time.struct_time(tm_year=1971, tm_mon=1, tm_mday=1, tm_hour=8, tm_min=0, tm_sec=0, tm_wday=4, tm_yday=1, tm_isdst=0)
不同格式时间的转换
表示:结构化时间-格式化-时间戳之间可以相互转化

# 结构化时间
now_time = time.localtime()
print(now_time)
# 把结构化时间转换为时间戳格式
print(time.mktime(now_time))
# 把结构化时间转换为格式化时间
# %Y年-%m月-%d天 %X时分秒=%H时:%M分:%S秒
print(time.strftime("%Y-%m-%d %X", now_time))
# 把格式化时间化为结构化时间,它和strftime()是逆操作
print(time.strptime('2013-05-20 13:14:52', '%Y-%m-%d %X'))
# 把结构化时间表示为这种形式:'Sun Jun 20 23:21:05 1993'。
print(time.asctime())#Thu Mar 7 16:22:11 2019
# 如果没有参数,将会将time.localtime()作为参数传入。
print(time.asctime(time.localtime()))#Thu Mar 7 16:22:11 2019
# 把一个时间戳转化为time.asctime()的形式。
print(time.ctime())#Thu Mar 7 16:22:11 2019
# 如果参数未给或者为None的时候,将会默认time.time()为参数。它的作用相当于time.asctime(time.localtime(secs))。
print(time.ctime(time.time()))
其他用法
#推迟时间,单位为秒
start = time.time()
time.sleep(3)
end = time.time()
print(end-start)
3.0005428791046143
第十章、time模块的更多相关文章
- Python通用编程
本文是Python通用编程系列教程,已全部更新完成,实现的目标是从零基础开始到精通Python编程语言.本教程不是对Python的内容进行泛泛而谈,而是精细化,深入化的讲解,共5个阶段,25章内容.所 ...
- 第十章、numpy模块
目录 第十章.numpy模块 一.导入方式 二.作用 三.通过函数创建numpy数组 四. numpy数组运算 五.重点 第十章.numpy模块 一.导入方式 import numpy as np#约 ...
- 第十章、logging模块
目录 第十章.logging模块 一.logging模块及日志框架 第十章.logging模块 一.logging模块及日志框架 导入方式 import logging 作用 写日志 模块功能 # V ...
- 第十章、typing模块
目录 第十章.typing模块 一.说明 二.typing模块的作用 三.使用typing模块 四.typing常用类型 第十章.typing模块 一.说明 二.typing模块的作用 类型检查,防止 ...
- 第十章、hashlib模块和hmac模块
目录 第十章.hashlib模块和hmac模块 一.hashlib模块 二.hash模块 第十章.hashlib模块和hmac模块 一.hashlib模块 hash是一种算法,接收传入的内容,经过运算 ...
- 第十章、json和pickle模块
目录 第十章.json和pickle模块 一.序列化 二.json 三.pickle模块 第十章.json和pickle模块 一.序列化 把对象(变量)从内存中变成可存储或传输的过程称之为序列化, 序 ...
- 第十章、sys模块
目录 第十章.sys模块 第十章.sys模块 方法 详解 sys.argv 命令行参数List,第一个元素是程序本身路径 sys.modules.keys() 返回所有已经导入的模块列表 sys.ex ...
- 第十章、os模块
目录 第十章.os模块 一.os模块 第十章.os模块 一.os模块 方法 详解 os.getcwd() 获取当前工作目录,即当前python脚本工作的目录路径 os.chdir("dirn ...
- 第十章、random模块
目录 第十章.random模块 第十章.random模块 #随机生成0-1之间的小数 import random print(random.random()) print(random.randint ...
随机推荐
- openstack compute service list Unable to establish connection to http://controller:8774/v2.1/os-services: ('Connection aborted.', BadStatusLine("''",))
8774是nova的端口号,所以我就逐一查看nova的日志文件. tail -f /var/log/nova/nova-conductor.log 2019-06-13 08:24:53.559 44 ...
- memcached-slab内存管理
一.Memcache内存分配机制 关于这个机制网上有很多解释的,我个人的总结如下. Page为内存分配的最小单位. Memcached 的内存分配以page为单位,默认情况下一个page是1M,可以通 ...
- C# 加解密工具类
using System; using System.IO; using System.Security.Cryptography; using System.Text; namespace Clov ...
- Flask之上下文管理机制
前引 在了解flask上下文管理机制之前,先来一波必知必会的知识点. 面向对象双下方法 首先,先来聊一聊面向对象中的一些特殊的双下划线方法,比如__call__.__getattr__系列.__get ...
- SpringCloud简历模板
项目三:智慧生鲜(SpringCloud) 开发周期:5个月项目介绍:该项目是一个基于微服分布式的生鲜电商网站,主要模块有前台:用户模块.商品模块.购物车模块.订单模块.支付模块.抢购模块.后台:权限 ...
- PYTHON 100days学习笔记007-2:python数据类型补充(2)
目录 day007:python数据类型补充(2) 1.Python3 元组 1.1 访问元组 1.2 删除元组 1.3 元组运算符 1.4 元组索引,截取 1.5 元组内置函数 2.python3 ...
- Linux 下使用 rar 进行压缩和解压缩
1. 下载安装文件 https://www.rarlab.com/download.htm 注意下载 64位的 2. 2019.8 时的下载命令为: wget https://www.rarlab. ...
- Less学习(1)
写在开头的话: 月余前被问起会不会Less,当时就有想学这个css框架的念头,而在昨天,在前端乱炖上看到一篇LessCss的开篇介绍,忽然就有了一股立马去学的冲动,回到家后找了几篇文章看了下,初感觉比 ...
- JDK1.8 新特性(全)
原文链接:https://blog.csdn.net/qq_29411737/article/details/80835658
- 处理idea加载不到Spring的xml或者properties配置文件
在pom文件的 标签钱添加以下代码 <!--防止ieda加载不到xml或者properties文件开始--> <resources> <resource> < ...