Python中处理时间 —— time模块

news/2024/7/3 6:28:22

time模块

逝去的秒数

逝去的秒数表示从某个时间(Python中是“Thu Jan 1 07:00:00 1970”)开始到现在所经过的秒数。

使用 time.time() 函数可以获得逝去的秒数:

>>time.time()
1388330058.8643

time.time()返回一个浮点数,可用于计算,比较,存储时间时间。

输出可读性强的时间字符串

要输出像“Thu Jan 1 07:00:00 1970”这种可读性强的时间字符串,我们可以使用 time.ctime() 函数,默认返回当前的时间:

>>> time.ctime()
'Sun Dec 29 23:17:44 2013'

也可以传入一个逝去的秒数,返回对应的时间字符串:

>>> time.ctime(0)
'Thu Jan  1 07:00:00 1970'

获取时间的各个部分信息

我们经常要分别获取一个时间的年、月、日、时、分、秒等信息,time模块定义了一个 struct_time 类型,用来存储时间的各个部分信息。该类型实现了元组协议,所以可以当作元组使用。

struct_time 的结构如下:

IndexAttributeValues
0tm_year(for example, 1993)
1tm_monrange [1, 12]
2tm_mdayrange [1, 31]
3tm_hourrange [0, 23]
4tm_minrange [0, 59]
5tm_secrange [0, 61];
6tm_wdayrange [0, 6], Monday is 0
7tm_ydayrange [1, 366]
8tm_isdst0, 1 or -1; see below

有几个函数都可以返回struct_time, gmtime() 返回当前的UTC时间,localtime() 返回当前时间域的当前时间:

>>> time.gmtime()
time.struct_time(tm_year=2013, tm_mon=12, tm_mday=29, tm_hour=15, tm_min=35, tm_sec=57, tm_wday=6, tm_yday=363, tm_isdst=0)
>>> time.localtime()
time.struct_time(tm_year=2013, tm_mon=12, tm_mday=29, tm_hour=23, tm_min=36, tm_sec=6, tm_wday=6, tm_yday=363, tm_isdst=0)

gmtime() 和 localtime() 也接受一个逝去的秒数作为参数,并转换成struct_time:

>>> time.localtime(0)
time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=7, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=1, tm_isdst=0)

mktime() 接收 struct_time 参数并将其转化为逝去的秒数:

>>> time.mktime(time.localtime())
1388331386.0

解析和格式化时间

如何将类似”Sun Dec 29 23:17:44 2013“的字符串解析成 struct_time?

time模块提供了两个函数来处理这方面的工作。 函数 strptime() 用于将时间字符串解析成 struct_time, 函数 strftime() 则将 struct_time 格式化成可读性强的时间字符串

import timenow = time.ctime()
print now
parsed = time.strptime(now)
print parsed
print time.strftime("%a %b %d %H:%M:%S %Y", parsed)# output =>
# Sun Mar  9 13:01:19 2008
# (2008, 3, 9, 13, 1, 19, 6, 69, -1)
# Sun Mar 09 13:01:19 2008

这两个函数都依赖特定的格式说明信息,默认的格式说明为”%a %b %d %H:%M:%S %Y“。完整的格式列表可以在https://docs.python.org/2/library/time.html#time.strftime找到。

DirectiveMeaningNotes
%aLocale’s abbreviated weekday name. 
%ALocale’s full weekday name. 
%bLocale’s abbreviated month name. 
%BLocale’s full month name. 
%cLocale’s appropriate date and time representation. 
%dDay of the month as a decimal number [01,31]. 
%HHour (24-hour clock) as a decimal number [00,23]. 
%IHour (12-hour clock) as a decimal number [01,12]. 
%jDay of the year as a decimal number [001,366]. 
%mMonth as a decimal number [01,12]. 
%MMinute as a decimal number [00,59]. 
%pLocale’s equivalent of either AM or PM.(1)
%SSecond as a decimal number [00,61].(2)
%UWeek number of the year (Sunday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Sunday are considered to be in week 0.(3)
%wWeekday as a decimal number [0(Sunday),6]. 
%WWeek number of the year (Monday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Monday are considered to be in week 0.(3)
%xLocale’s appropriate date representation. 
%XLocale’s appropriate time representation. 
%yYear without century as a decimal number [00,99]. 
%YYear with century as a decimal number. 
%ZTime zone name (no characters if no time zone exists). 
%%A literal '%' character. 

转载于:https://www.cnblogs.com/Hessen/p/8961217.html


http://lihuaxi.xjx100.cn/news/237010.html

相关文章

C#版 - Leetcode49 - 字母异位词分组 - 题解

C#版 - Leetcode49 - 字母异位词分组 - 题解 Leetcode49.Group Anagrams 在线提交:https://leetcode.com/problems/group-anagrams/ 题目描述 给定一个字符串数组,将字母异位词组合在一起。字母异位词指字母相同,但排列不同的字符串。 示例: 输入: [&quo…

区块链有什么优势可以如此火爆?

链客,专为开发者而生,有问必答! 此文章来自区块链技术社区,未经允许拒绝转载。 如果说2016年是区块链元年,那么2017年则是其破冰之年,综合相关报道可知,当前区块链已经在很多领域率先完成突破&…

朱晔的互联网架构实践心得S1E9:架构评审一百问和设计文档五要素

朱晔的互联网架构实践心得S1E9:架构评审一百问和设计文档五要素 【下载文本PDF进行阅读】 本文我会来说说我认为架构评审中应该看的一些点,以及我写设计文档的一些心得。助你在架构评审中过五关斩六将,助你写出能让人收藏点赞的设计文档。 技…

关于事务隔离级别

2019独角兽企业重金招聘Python工程师标准>>> 第一种 read uncommitted 此状态下脏读,不可重复读,虚读都有可能发生。此状态下两个人同时操作一个数据库表一边开启事务没有提交,另一边也开启事物开始更改数据但是也没有提交&#x…

通俗易懂,到底什么是区块链?

链客,专为开发者而生,有问必答! 此文章来自区块链技术社区,未经允许拒绝转载。 2017年9月4日,中国政府正式明令禁止ICO和数字货币交易行为,随即关闭了多个数字货币交易所。同时政府也多次声明&#xff0…

后台服务项目的白盒测试之旅

本文来自阿网易云社区作者:孙婷婷白盒测试起因17年下半年我开始介入部门新项目的服务v2版本的功能测试。刚接手项目时,感到十分头疼,首先它不像我刚接触测试时做的to C端项目,主要是页面展示操作,黑盒测试足够&#xf…

App功能测试的注意点

好几个月没有写博客记录学习心得了,这次回老家深夜闲来无事写一篇记录下这段时间的面试心得,这次面试过程很多面试官都问APP的有关测试,下面我就自己的认识和工作中的经验来谈谈自己对APP测试的认识: 1.push消息推送测试 检查push…

什么是区块链智能合约?

链客,专为开发者而生,有问必答! 此文章来自区块链技术社区,未经允许拒绝转载。 自从2009年第一枚比特币诞生,九年多时间里,区块链技术正在被应用在人们生活的各方各面,从1.0时代的数字货币&…