首页 » Linux » python模块之time

python模块之time

 

python中时间的几种表示格式

时间戳(timestamp):通常来说,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量。我们运行“type(time.time())”,返回的是float类型。

格式化的时间字符串(Format String)

结构化的时间(struct_time):struct_time元组共有9个元素共九个元素:(年,月,日,时,分,秒,一年中第几周,一年中第几天,夏令时)

时间戳

time.time()

>>> import time
>>> time.time()
1537931712.4534721

结构化的时间

格林时间和本地时间打印出来都是结构化时间
time.localtime()
time.gmtime()
time.strptime()

>>> time.localtime()
time.struct_time(tm_year=2018, tm_mon=9, tm_mday=26, tm_hour=11, tm_min=22, tm_sec=40, tm_wday=2, tm_yday=269, tm_isdst=0)
>>> time.gmtime()
time.struct_time(tm_year=2018, tm_mon=9, tm_mday=26, tm_hour=3, tm_min=22, tm_sec=52, tm_wday=2, tm_yday=269, tm_isdst=0)
>>> time.localtime().tm_year
2018
>>> time.localtime().tm_mon
9
>>> time.strptime('2018-05-05 15:08:08','%Y-%m-%d %X')
time.struct_time(tm_year=2018, tm_mon=5, tm_mday=5, tm_hour=15, tm_min=8, tm_sec=8, tm_wday=5, tm_yday=125, tm_isdst=-1)

格式化的时间字符串

time.strftime

>>> time.strftime("%Y-%m-%d %X")
'2018-09-26 11:32:03'
>>> time.strftime('%Y-%m-%d %X',time.localtime())
'2018-09-26 11:58:55'

三种时间格式相互转化

只能按以下三种方式转化
timestamp <—> struct_time <—> format_string
三种时间格式转化图:

996085-20161026171443546-488752980

如果需要时间戳转为格式化字符串时间转化不能直接转化的,但是可以用datetime模块来实现datetime.datetime.fromtimestamp(1537933671.8462093)

格式化时间要转为时间戳需要通过结构化时间来转。可以查看上面的图

>>> time.time()
1537933950.3722038
>>> a=time.strptime('2018-09-26 11:52:20','%Y-%m-%d %X')
>>> a
time.struct_time(tm_year=2018, tm_mon=9, tm_mday=26, tm_hour=11, tm_min=52, tm_sec=20, tm_wday=2, tm_yday=269, tm_isdst=-1)
>>> time.mktime(a)
1537933940.0
>>>

time.asctime()和time.ctime()

asctime([t]) : 把一个表示时间的元组或者struct_time表示为这种形式:’Sun Jun 20 23:21:05 1993’。这种形式就是我们在linux上看到的时间格式。 如果没有参数,将会将time.localtime()作为参数传入。

ctime([secs]) : 把一个时间戳(按秒计算的浮点数)转化为time.asctime()的形式。如果参数未给或者为 None的时候,将会默认time.time()为参数。它的作用相当于time.asctime(time.localtime(secs))。

和结构化时间转化图:

996085-20161026171443546-488752980

>>> time.ctime()
'Wed Sep 26 12:06:52 2018'
>>> time.ctime(time.time())
'Wed Sep 26 12:07:00 2018'
>>> time.asctime()
'Wed Sep 26 12:07:04 2018'

格式化时间中的变量解释如下

%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.     
%d    Day of the month as a decimal number [01,31].     
%H    Hour (24-hour clock) as a decimal number [00,23].     
%I    Hour (12-hour clock) as a decimal number [01,12].     
%j    Day of the year as a decimal number [001,366].     
%m    Month as a decimal number [01,12].     
%M    Minute as a decimal number [00,59].     
%p    Locale’s equivalent of either AM or PM.    (1)
%S    Second as a decimal number [00,61].    (2)
%U    Week 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)
%w    Weekday as a decimal number [0(Sunday),6].     
%W    Week 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)
%x    Locale’s appropriate date representation.     
%X    Locale’s appropriate time representation.     
%y    Year without century as a decimal number [00,99].     
%Y    Year with century as a decimal number.     
%z    Time zone offset indicating a positive or negative time difference from UTC/GMT of the form +HHMM or -HHMM, where H represents decimal hour digits and M represents decimal minute digits [-23:59, +23:59].     
%Z    Time zone name (no characters if no time zone exists).     
%%    A literal '%' character.

 

原文链接:python模块之time,转载请注明来源!

0