Python3学习之路~5.2 time & datetime模块
time模块
时间相关的操作,时间有三种表示方式:
- 时间戳 1970年1月1日之后的秒,即:time.time()
- 格式化的字符串 2014-11-11 11:11, 即:time.strftime('%Y-%m-%d')
- 结构化时间 元组包含了:年、日、星期(取值0-6, Monday is 0)等... time.struct_time 即:time.localtime()
time模块下常用方法:
asctime([tuple]) -> string(e.g. 'Sat Jun 06 16:26:11 1998')
ctime(seconds) -> string
gmtime([seconds]) -> (tm_year, tm_mon, tm_mday, tm_hour, tm_min, tm_sec, tm_wday, tm_yday, tm_isdst)
localtime([seconds]) -> (tm_year,tm_mon,tm_mday,tm_hour,tm_min,tm_sec,tm_wday,tm_yday,tm_isdst)
mktime(tuple) -> floating point number
sleep(seconds)
strftime(format[, tuple]) -> string
strptime(string, format) -> struct_time
time() -> floating point number
常用的格式代码:
格式参照:
%a 本地(Locale)简化星期名称
%A 本地完整星期名称
%b 本地简化月份名称
%B 本地完整月份名称
%c 本地相应的日期和时间表示
%d 一个月中的第几天(01-31)
%H 一天中的第几个小时(24小时制,00-23)
%I 第几个小时(12小时制,01-12)
%j 一年中的第几天(001-366)
%m 月份(01-12)
%M 分钟数(00-59)
%p 本地AM或PM的相应符
%S 秒(01-61)
%U 一年中的星期数(00-53,星期天是一个星期的开始)第一个星期天之前的所有天数都放在第0周
%w 一个星期中的第几天(0-6,0是星期天)
%W 和%U基本相同,不同的是%W以星期一为一个星期的开始
%x 本地相应日期
%X 本地相应时间
%y 去掉世纪的年份(00-99)
%Y 完整的年份
%z 时区偏移量,指示格式为+ HHMM或-HHMM的UTC / GMT的正负时差,其中H表示小时数,M表示分钟数(-23:59 - +23:59)
%Z 时区的名字(如果不存在则为空字符)
%% ‘%’字符
代码举例:
import time
# print(help(time)) #查看time的帮助文档
print(time.timezone) # -28800,time模块下的变量,返回difference in seconds between UTC and local standard time,-28800s=-28800/3600=-8h,即东八区,比UTC早8小时
print(time.altzone) # --32400,time模块下的变量,返回difference in seconds between UTC and local DST time,-32400s=--32400/3600=-9h,即比标准时间早9小时,
# 所谓的DST(夏令时),就是利用夏季天亮得早这一自然现象,人为地将时间提前一小时,即比当地标准时间(东八区)早1小时 print(time.time()) #当前时间戳,是一个整数或浮点数,单位秒,如1529976123.6539726
# time.sleep(3) # 当前程序睡眠3秒
print(time.gmtime()) # 返回当前时间的元组,可加时间戳参数
print(time.gmtime(0)) # 返回utc时间的struct time对象格式,即一个代表1970年1月1日的元组:
# time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=1, tm_isdst=0)
print(time.gmtime(1529976123.6539726)) # gmtime([seconds]) -> time tuple
print(time.localtime()) # 返回本地时间的struct time对象格式, localtime([seconds]) -> time tuple
print(time.clock()) # 返回处理器时间,3.3开始已废弃 , 改成了time.process_time()测量处理器运算时间,不包括sleep时间,不稳定,mac上测不出来
print(time.asctime()) # asctime([tuple]) -> string(e.g."Tue Jun 26 09:54:52 2018")
print(time.asctime(time.localtime())) # 同上,返回时间格式"Tue Jun 26 09:54:52 2018"
print(time.ctime()) # ctime(seconds) -> string(e.g."Tue Jun 26 09:54:52 2018") # 日期字符串 转成 时间戳
string_struct_time = time.strptime("2018/06/26","%Y/%m/%d") #将 日期字符串 转成 struct时间对象格式
print(string_struct_time) # time.struct_time(tm_year=2018, tm_mon=6, tm_mday=26, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=1, tm_yday=177, tm_isdst=-1) struct_stamp_time = time.mktime(string_struct_time) #将struct时间对象转成时间戳 mktime(tuple) -> floating point number
print(struct_stamp_time) # 1529942400.0 # 时间戳 转为 字符串格式
print(time.gmtime(time.time()-86640)) #将utc时间戳转换成struct_time格式
print(time.strftime("%Y-%m-%d %H:%M:%S",time.gmtime()) ) # 将utc struct_time格式转成指定的字符串格式
print(time.strftime('%Y-%m-%d') ) # 默认当前时间,2018-06-26
datetime模块
import datetime
import time
'''
datetime.date:表示日期的类。常用的属性有year, month, day
datetime.time:表示时间的类。常用的属性有hour, minute, second, microsecond
datetime.datetime:表示日期时间
datetime.timedelta:表示时间间隔,即两个时间点之间的长度
timedelta([days[, seconds[, microseconds[, milliseconds[, minutes[, hours[, weeks]]]]]]])
strftime("%Y-%m-%d")
''' print(datetime.datetime.now()) # 当前时间,2018-06-26 10:40:16.553391
#时间加减
print(datetime.datetime.now() + datetime.timedelta(3)) #当前时间+3天
print(datetime.datetime.now() + datetime.timedelta(-3)) #当前时间-3天
print(datetime.datetime.now() - datetime.timedelta(days=5)) # 当前时间-5天
print(datetime.datetime.now() + datetime.timedelta(hours=3)) #当前时间+3小时
print(datetime.datetime.now() + datetime.timedelta(minutes=30)) #当前时间+30分 print(datetime.date.fromtimestamp(time.time()) ) # 时间戳直接转成日期格式 2018-06-26
c_time = datetime.datetime.now()
print(c_time.replace(minute=3,hour=2)) #时间替换,2018-06-26 02:03:27.844764
Python3学习之路~5.2 time & datetime模块的更多相关文章
- Python3学习之路~5.7 Json & pickle 模块
用于序列化的两个模块 json,用于字符串 和 python数据类型间进行转换 pickle,用于python特有的类型 和 python的数据类型间进行转换 Json模块提供了四个功能:dumps. ...
- Python3学习之路~7.4 动态导入模块
动态导入模块就是只知道str类型的模块名字符串,通过这个字符串导入模块. 准备: 首先创建一个模块目录lib,然后在目录内创建一个模块 aa.py: # aa.pyclass C: def __ini ...
- Python3学习之路~5.9 xml处理模块
xml是实现不同语言或程序之间进行数据交换的协议,跟json差不多,但json使用起来更简单,不过,以前在json还没诞生的时候,大家只能选择用xml,至今很多传统公司如金融行业的很多系统的接口还主要 ...
- Python3学习之路~0 目录
目录 Python3学习之路~2.1 列表.元组操作 Python3学习之路~2.2 简单的购物车程序 Python3学习之路~2.3 字符串操作 Python3学习之路~2.4 字典操作 Pytho ...
- Python3学习之路~8.5 SocketServer实现多并发
前面几节我们写的socket都只能实现服务端与一个客户端通信,并不能实现服务端与多客户端同时通信.接下来我们就来学习一下如何实现服务端同时与多个客户端通信,即并发. Socket Server soc ...
- Python3学习之路~9.4 队列、生产者消费者模型
一 队列queue 当必须在多个线程之间安全地交换信息时,队列在线程编程中特别有用. 队列的作用:1.解耦,使程序直接实现松耦合 2.提高处理效率 列表与队列都是有顺序的,但是他们之间有一个很大的区别 ...
- Python3学习之路
python基础知识点 1.python基础知识点汇总 2.python常用数据类型 3.python之列表 4.python之字符串 5.python常用数据运算符 6.python之字典 7.py ...
- Python3学习之路~9.1 paramiko模块:实现ssh执行命令以及传输文件
我们一般使用linux的时候,都是在Windows上安装一个ssh客户端连接上去.那么从一台linux如何连接到另一条linux呢?使用ssh命令即可,因为每台linux机器自己都有一个ssh客户端. ...
- Python3学习之路~8.6 开发一个支持多用户在线的FTP程序-代码实现
作业: 开发一个支持多用户在线的FTP程序 要求: 用户加密认证 允许同时多用户登录 每个用户有自己的家目录 ,且只能访问自己的家目录 对用户进行磁盘配额,每个用户的可用空间不同 允许用户在ftp s ...
随机推荐
- [Math] Unconstrained & Constrained Optimization
粘贴两个典型的例子,只是基础内容,帮助理解. (1) Solution: (2) Solution:
- JavaScript 之 function函数及参数arguments
JavaScript用function关键字声明函数,可以用return返回值,也可以没有返回值. 建议:要么统一有返回值,要么统一都没有返回值,这样调试代码方便. 函数定义格式: function ...
- 关于Kafka Fetch Session的讨论
Kafka在1.1.0版本引入了fetch session的概念,旨在降低“无效”FETCH请求对集群带宽资源的占用.故事的背景是这样的: 众所周知,Kafka的broker和consumer都会定期 ...
- 十一、K3 WISE 开发插件《VB插件开发如何代码调试 - 步骤讲解》
=================================== 目录: 1.配置代码调试启动程序kdmain.exe 2.设置断点 3.触发调试 4.变量跟踪 ================ ...
- mysqldump命令的安装
author:headsen chen date:2019-03-14 11:31:00 安装:yum -y install mysql-client / apt-get install mys ...
- Python数据结构———栈
线性数据结构 当添加一个项目时,它就被放在这样一个位置:在之前存在的项与后来要加入的项之间.像这样的数据集合常被称为线性数据结构. 栈 栈是一个项的有序集合.添加项和移除项都发生在同一“端”,这一端通 ...
- POJ 1451 - T9 - [字典树]
题目链接:http://bailian.openjudge.cn/practice/1451/ 总时间限制: 1000ms 内存限制: 65536kB 描述 Background A while ag ...
- 安装ReactNative开发IDE
https://blog.csdn.net/u014484863/article/details/51554428 https://github.com/reactnativecn/react-nat ...
- 给AFNetworking添加请求缓存功能实现在没有网络的情况下返回缓存数据
原理:先给NSURLSession地Configuration设置一个内存和本地代理,原来的网络请求结束后会查找缓存的代理字典,并执行代理对象对应的操作方法,需要做的就是拦截错误的方法,返回缓存的数据 ...
- 如何写好.babelrc?Babel的presets和plugins配置解析
什么是Babel The compiler for writing next generation JavaScript. 官网是这么说的,翻译一下就是下一代JavaScript 语法的编译器. 作为 ...