2 time模块

1 python中,有这几种的方式表示时间

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

2 定义

  • UTC 简称:协调世界时,又称世界统一时间、世界标准时间、国际协调时间

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

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

序号 属性
0 tm_year 2019
1 tm_mon 1~12
2 tm_mday 1~31
3 tm_hour 0~23
4 tm_min 0~59
5 tm_sec 0 到 61 (60或61 是闰秒)
6 tm_wday 0到6 (0是周一)
7 tm_yday 1 到 366(儒略历)
8 tm_isdst -1, 0, 1, -1是决定是否为夏令时的旗帜

time模块的方法

  • time.localtime([secs]):将一个时间戳转换为当前时区的struct_time。secs参数未提供,则以系统当前时间为准。
time.struct_time(tm_year=2018, tm_mon=12, tm_mday=5, tm_hour=21, tm_min=17, tm_sec=15, tm_wday=2, tm_yday=339, tm_isdst=0)
  • time.gmtime([secs]):和localtime()方法类似,gmtime()方法是将一个时间戳转换为UTC时区(0时区)的struct_time。
time.struct_time(tm_year=2018, tm_mon=12, tm_mday=5, tm_hour=13, tm_min=18, tm_sec=52, tm_wday=2, tm_yday=339, tm_isdst=0)
  • time.time():返回当前时间的时间戳。
1544015983.3351276
  • time.mktime(t):将一个struct_time转化为时间戳。
time.mktime(time.localtime())

输出

1544016032.0
  • time.sleep(secs):线程推迟指定的时间运行。单位为秒。
  • time.asctime([t]):把一个表示时间的元组或者struct_time表示为这种形式:'Sun Oct 1 12:04:38 2017'。如果没有参数,将会将time.localtime()作为参数传入。
time.asctime()  相当于  time.asctime(time.localtime())

输出

Wed Dec  5 21:23:40 2018 
  • time.ctime([secs]):把一个时间戳(按秒计算的浮点数)转化为time.asctime()的形式。如果参数未给或者为None的时候,将会默认time.time()为参数。它的作用相当于time.asctime(time.localtime(secs))。
print(time.ctime(0))  #如果里面的参数是负数会回到1970年之前
print(time.ctime())
print(time.ctime(time.time()))

输出

Thu Jan  1 08:00:00 1970
Wed Dec  5 21:27:33 2018
Wed Dec  5 21:27:33 2018
  • 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)

最后为了容易记住转换关系,看下图

%a Locale’s abbreviated weekday name. 显示星期几 例如: Wed
%A Locale’s full weekday name. 显示星期几 例如:Wednesday
%b Locale’s abbreviated month name. 显示月份 例如:Dec
%B Locale’s full month name. 显示月份 例如:December
%c Locale’s appropriate date and time representation. 例如:Wed Dec 5 21:36:46 2018
%d Day of the month as a decimal number [01,31]. 显示几天几号 例如:05
%H Hour (24-hour clock) as a decimal number [00,23]. 显示现在几点(24小时制) 例如:21
%I Hour (12-hour clock) as a decimal number [01,12]. 显示现在几点(12小时制) 例如:09
%j Day of the year as a decimal number [001,366]. 显示今天是今年的多少天 例如:339
%m Month as a decimal number [01,12]. 显示现在是几月份 例如:12
%M Minute as a decimal number [00,59]. 显示现在是一分钟中的几分钟 例如:41
%p Locale’s equivalent of either AM or PM. 显示现在在是上午(AM)还是 下午(PM)
%S Second as a decimal number [00,61]. 显示现在在一分钟中的几秒钟 例如:45
%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. 显示现在这周是今年中的第几周( 新年第一个星期日之前的所有日子都被认为是在第0周。)例如 48
%w Weekday as a decimal number [0(Sunday),6]. 显示今天是这周中的第几个工作日例如:3
%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. 显示现在这周是今年中的第几周( 新年第一个星期一之前的所有日子都被认为是在第0周。)例如: 49
%x Locale’s appropriate date representation. 显示年月日例如:12/05/18
%X Locale’s appropriate time representation. 显示时分秒显示:21:47:57
%y Year without century as a decimal number [00,99]. 显示今年是哪一年例如:18
%Y Year with century as a decimal number. 显示今年是哪一年例如:2018
%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]. 显示时区例如:+0800
%Z Time zone name (no characters if no time zone exists).
%% A literal '%'character.
原文地址:https://www.cnblogs.com/shibojie/p/11658341.html