Python Time模块快速入门
在Python中,与时间处理有关的模块就包括:time,datetime以及calendar。现在我们主要开始讲解time模块的入门。
*注意记得看博客前先导入包哦!
>>> import time
入门第一步:表示时间的方式
1)时间戳 (timestamp)
2)格式化的时间字符串
3)时间元组(struct_time)
1 、时间戳(timestamp)
获得时间戳方式:
方式一:利用time.time()直接获得
这是官方文档:
time(...)
time() -> floating point number
Return the current time in seconds since the Epoch.
Fractions of a second may be present if the system clock provides them.
(END)
可以看出,time.time()是将当前时间与1970年1月1日00:00:00按秒计算的偏移量作为返回值。
举个例子,理解更深刻:
>>> time.time()
1531620001.289046
>>> type(time.time())
<class 'float'>
方式二:利用time.mktime(tuple)将时间元组(strunk_time)转化为时间戳
**不急,接下来我们会补充说明时间元组(strunk_time)这里可以先看看有个印象,等下蓦然回头,会恍然大悟的
老规矩,官方文档走一波
mktime(...)
mktime(tuple) -> floating point number
Convert a time tuple in local time to seconds since the Epoch.
Note that mktime(gmtime(0)) will not generally return zero for most
time zones; instead the returned value will either be equal to that
of the timezone or altzone attributes on the time module.
(END)
举个例子,理解更深刻:
>>> time_tuple = time.localtime()
>>> time.mktime(time_tuple)
1531621341.0
>>>
2、时间元组(struct_time)
小问题 1、:时间元组 长啥样?
我们先弄一个时间元组出来,对,就是用上面的localtime()方法
>>> time.localtime()
time.struct_time(tm_year=2018, tm_mon=7, tm_mday=15, tm_hour=10, tm_min=27, tm_sec=38, tm_wday=6, tm_yday=196, tm_isdst=0)
>>> type(time.localtime())
<class 'time.struct_time'>
我们可以看见,struct_time由9部分组成:>>> 属性 值
tm_year(年) 比如2017 tm_mon(月) 1 - 12 tm_mday(日) 1 - 31 tm_hour(时) 0 - 23 tm_min(分) 0 - 59 tm_sec(秒) 0 - 61 tm_wday(weekday) 0 - 6(0表示周日) tm_yday(一年中的第几天) 1 - 366 tm_isdst(是否是夏令时) 默认为-1
获取时间元组方式:
方式一:time.localtime()方法
官方文档:
localtime(...)
localtime([seconds]) -> (tm_year,tm_mon,tm_mday,tm_hour,tm_min,
tm_sec,tm_wday,tm_yday,tm_isdst)
Convert seconds since the Epoch to a time tuple expressing local time.
When 'seconds' is not passed in, convert the current time instead.
(END)
通过查看官方文档,我们发现:
当time.localtime()不传参进入进入时,返回的是当前时间的时间元组
当传入时间戳时,这就比较有意思了,能将时间戳变为时间元组。
学到这里,我们就能如下转换关系了知道
传入时间戳方式(注意:这里起始时间始1970-1-1)
>>> time.localtime(12)
time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=8, tm_min=0, tm_sec=12, tm_wday=3, tm_yday=1, tm_isdst=0)
获取本地时间
>>> time.localtime()
time.struct_time(tm_year=2018, tm_mon=7, tm_mday=15, tm_hour=14, tm_min=0, tm_sec=17, tm_wday=6, tm_yday=196, tm_isdst=0)
方式二 gmtime():
其实gmtime()和localtime()一样,但是我还是相信有人想问,gmtime()是什么东西?
#把localtime()换为gmtime() 就表示格林威治时间
如果你觉得格林威治时间又让你丈二的和尚——摸不着头脑的话,还是来举个例子:
>>> time.gmtime()
time.struct_time(tm_year=2018, tm_mon=7, tm_mday=15, tm_hour=3, tm_min=4, tm_sec=10, tm_wday=6, tm_yday=196, tm_isdst=0)
>>> time.localtime()
time.struct_time(tm_year=2018, tm_mon=7, tm_mday=15, tm_hour=11, tm_min=4, tm_sec=21, tm_wday=6, tm_yday=196, tm_isdst=0)
看红色的时间,你就能恍然大悟!
原来gmtime()就是不进行时区转换的时间
而localtime()是进行了时区转换的。
**注意,这里的tm_wday从0表示周一,取值范围【0,6】分别表示周一~周日
方式三:time.strptime()将字符串转化为时间数组
官方文档:
strptime(...)
strptime(string, format) -> struct_time
Parse a string to a time tuple according to a format specification.
See the library reference manual for formatting codes (same as
strftime()).
Commonly used format codes:
%Y Year with century as a decimal number.
%m Month as a decimal number [01,12].
%d Day of the month as a decimal number [01,31].
%H Hour (24-hour clock) as a decimal number [00,23].
%M Minute as a decimal number [00,59].
%S Second as a decimal number [00,61].
%z Time zone offset from UTC.
%a Locale's abbreviated weekday name.
%A Locale's full weekday name.
%b Locale's abbreviated month name.
%B Locale's full month name.
%c Locale's appropriate date and time representation.
%I Hour (12-hour clock) as a decimal number [01,12].
%p Locale's equivalent of either AM or PM.
Other codes may be available on your platform. See documentation for
the C library strftime function.
(END)
再来个中文的参数说明:
%Y 年 Year with century as a decimal number. %m 月 Month as a decimal number [01,12]. %d 日 Day of the month as a decimal number [01,31]. %H 时 Hour (24-hour clock) as a decimal number [00,23]. %M 分 Minute as a decimal number [00,59]. %S 秒 Second as a decimal number [00,61]. %z Time zone offset from UTC. %a 周几简写(英文Sun) Locale's abbreviated weekday name. %A 周几全名(英文Sunday) Locale's full weekday name. %b 月份简写(英语Apr) Locale's abbreviated month name. %B 月份全名(英语April) Locale's full month name. %c Locale's appropriate date and time representation. %I 十二小时制小时数 Hour (12-hour clock) as a decimal number [01,12]. %p AM/PM Locale's equivalent of either AM or PM.
举个例子深刻感受一下
>>> time.strptime('1990-1-1','%Y-%m-%d')
time.struct_time(tm_year=1990, tm_mon=1, tm_mday=1, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=0, tm_yday=1, tm_isdst=-1)
第一个参数是你输入的字符串,第二个参数是你需要的格式化条件3、格式化的时间字符串
于是就有了以下基本操作:
方式一:time.strftime()
既然前面又time.strptime(),与之相对应的,就有了time.strftime()
(**备注:我是这么记忆这两个方法的,自己输入的‘input’转时间元组所以是strptime,想要格式化’format‘输出时间的就是strftime)
官方文档:
strftime(...)
strftime(format[, tuple]) -> string
Convert a time tuple to a string according to a format specification.
See the library reference manual for formatting codes. When the time tuple
is not present, current time as returned by localtime() is used.
Commonly used format codes:
%Y Year with century as a decimal number.
%m Month as a decimal number [01,12].
%d Day of the month as a decimal number [01,31].
%H Hour (24-hour clock) as a decimal number [00,23].
%M Minute as a decimal number [00,59].
%S Second as a decimal number [00,61].
%z Time zone offset from UTC.
%a Locale's abbreviated weekday name.
%A Locale's full weekday name.
%b Locale's abbreviated month name.
%B Locale's full month name.
%c Locale's appropriate date and time representation.
%I Hour (12-hour clock) as a decimal number [01,12].
%p Locale's equivalent of either AM or PM.
Other codes may be available on your platform. See documentation for
the C library strftime function.
(END)
第一个参数是format,你想要输出的约束条件。
第二个是可选参数,如果不传入参数,则默认使用当前时间(localtime()),简言之,按你想要的格式输出当前时间
举个例子:
>>> time.strftime('%H:%M:%S')#当前时间
'13:55:50'
>>> time.strftime('%H:%M:%S',time.strptime('18:33:50','%H:%M:%S'))#自己定义的时间
'18:33:50'
所以,现在又能做到如下转换了:
学习到这里,我们可以做以下骚操作了:
#计算5分30秒后的时间,你也可以举一反三,将一年的时间转换为秒数后加在时间戳上就好(代替这里的330=5*60+30)
time_input = '12:1:1'
time_date = time.strptime(time_input,'%H:%M:%S')
time.strftime('%H:%M:%S',time.localtime(time.mktime(time_date)+330))
Out[76]: '12:06:31'
补充一些方法,直接按格式输出的方式:
方法 asctime()
asctime(...)
asctime([tuple]) -> string
Convert a time tuple to a string, e.g. 'Sat Jun 06 16:26:11 1998'.
When the time tuple is not present, current time as returned by localtime()
is used.
(END)
可选参数:传入一个时间元组,输出为 ‘Sat Jun 06 16:26:11 1998’ 的字符串
不传参数时,显示的是当前时间的字符串
方法 ctime()
ctime(...)
ctime(seconds) -> string
Convert a time in seconds since the Epoch to a string in local time.
This is equivalent to asctime(localtime(seconds)). When the time tuple is
not present, current time as returned by localtime() is used.
(END)
可选参数:传入一个时间戳,输出为 ‘Sat Jun 06 16:26:11 1998’ 的字符串
不传参数时,显示的是当前时间的字符串
举个例子:
>>> time.asctime()
'Sat Apr 8 14:20:04 2017'
>>>
>>> time.ctime()
'Sat Apr 8 14:20:09 2017'
>>>
>>> time.asctime(time.localtime())
'Sat Apr 8 14:20:27 2017'
>>>
>>> time.ctime(time.time())
'Sat Apr 8 14:20:44 2017'
以上是我的个人见解,如有错误,感谢各位大佬指出哦。
上一篇: 利用Python进行数据分析学习笔记