python模块之time

一、在Python中,通常有这几种方式来表示时间:

  1. 时间戳
  2. 格式化的时间字符串
  3. 元组(struct_time)共九个元素。由于Python的time模块实现主要调用C库,所以各个平台可能有所不同

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

元组(struct_time)方式:struct_time元组共有9个元素,返回struct_time的函数主要有gmtime(),localtime(),strptime()。

 

time模块的方法

  • time.localtime([secs]):将一个时间戳转换为当前时区的struct_time。secs参数未提供,则以当前时间为准。
  • time.gmtime([secs]):和localtime()方法类似,gmtime()方法是将一个时间戳转换为UTC时区(0时区)的struct_time。
  • time.time():返回当前时间的时间戳。
  • time.mktime(t):将一个struct_time转化为时间戳。
  • time.sleep(secs):线程推迟指定的时间运行。单位为秒。
  • time.asctime([t]):把一个表示时间的元组或者struct_time表示为这种形式:'Sun Oct 1 12:04:38 2017'。如果没有参数,将会将time.localtime()作为参数传入。
  • time.ctime([secs]):把一个时间戳(按秒计算的浮点数)转化为time.asctime()的形式。如果参数未给或者为None的时候,将会默认time.time()为参数。它的作用相当于time.asctime(time.localtime(secs))。
  • time.strftime(format[, t]):把一个代表时间的元组或者struct_time(如由time.localtime()和time.gmtime()返回)转化为格式化的时间字符串。如果t未指定,将传入time.localtime()。

    • 举例:time.strftime("%Y-%m-%d %X", time.localtime()) #输出'2017-10-01 12:14:23'
  • time.strptime(string[, format]):把一个格式化时间字符串转化为struct_time。实际上它和strftime()是逆操作。

    • 举例:time.strptime('2017-10-3 17:54',"%Y-%m-%d %H:%M") #输出 time.struct_time(tm_year=2017, tm_mon=10, tm_mday=3, tm_hour=17, tm_min=54, tm_sec=0, tm_wday=1, tm_yday=276, tm_isdst=-1) 
字符串转时间格式对应表
 MeaningNotes
%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.

 

转换关系图

 

 

 

计算时间大小要先转换成时间戳,在相减。

 

代码示例:

import time

a = time.localtime()
print('%s-%s-%s %s:%s:%s' % (a.tm_year, a.tm_mon, a.tm_mday, a.tm_hour, a.tm_min, a.tm_sec))  # 2018-11-21 16:20:5

b = time.localtime(123123123)
print(b)  # time.struct_time(tm_year=1973, tm_mon=11, tm_mday=26, tm_hour=8, tm_min=52, tm_sec=3, tm_wday=0, tm_yday=330, tm_isdst=0)

# 将一个struct_time转化为时间戳。

print(time.mktime(b))  # 123123123.0

# 国外格式的时间

print(time.asctime())  # Wed Nov 21 16:24:45 2018

print(time.ctime(-12323123231))  # Sun Jul  1 14:18:32 1579 没有时间戳的话和上面是一样的

# 把一个代表时间的元组或者struct_time,转化为格式化的时间字符串
print(time.strftime('%Y-%m-%d %H:%M:%S'))  # 2018-11-21 11/21/2018-11-21 17:22:08

# strptime,strftime的逆操作
s = time.strftime('%Y-%m-%d %H:%M:%S')
print(time.strptime(s, '%Y-%m-%d %H:%M:%S')) # time.struct_time(tm_year=2018, tm_mon=11, tm_mday=21, tm_hour=17, tm_min=22, tm_sec=8, tm_wday=2, tm_yday=325, tm_isdst=-1)
s2 = time.strptime(s, '%Y-%m-%d %H:%M:%S')

print(time.mktime(s2))  # 1542792210.0

 

 

 

posted @ 2018-11-21 16:58  梁少华  阅读(279)  评论(0编辑  收藏  举报