Learning time module in Python

1. Functions included in the Time module

The Time module contains the following built-in functions, both for time processing and time format conversion:

序号  函数及描述
1   time.altzone
      返回格林威治西部的夏令时地区的偏移秒数。如果该地区在格林威治东部会返回负值(如西欧,包括英国)。对夏令时启用地区才能使用。
2   time.asctime([tupletime])
      接受时间元组并返回一个可读的形式为"Tue Dec 11 18:07:14 2008"20081211日 周二180714秒)的24个字符的字符串。
3   time.clock( )
      用以浮点数计算的秒数返回当前的CPU时间。用来衡量不同程序的耗时,比time.time()更有用。
4   time.ctime([secs])
      作用相当于asctime(localtime(secs)),未给参数相当于asctime()
5   time.gmtime([secs])
      接收时间戳(1970纪元后经过的浮点秒数)并返回格林威治天文时间下的时间元组t。注:t.tm_isdst始终为0
6   time.localtime([secs])
      接收时间戳(1970纪元后经过的浮点秒数)并返回当地时间下的时间元组t(t.tm_isdst可取01,取决于当地当时是不是夏令时)。
7   time.mktime(tupletime)
      接受时间元组并返回时间戳(1970纪元后经过的浮点秒数)。
8   time.sleep(secs)
      推迟调用线程的运行,secs指秒数。
9   time.strftime(fmt[,tupletime])
      接收以时间元组,并返回以可读字符串表示的当地时间,格式由fmt决定。
10  time.strptime(str,fmt='%a %b %d %H:%M:%S %Y')
      根据fmt的格式把一个时间字符串解析为时间元组。
11  time.time( )
      返回当前时间的时间戳(1970纪元后经过的浮点秒数)。
12  time.tzset()
      根据环境变量TZ重新初始化时间相关设置。

Below we explain the usage of each function separately

  1. altzone()
    Parameters: None
    Return value: Returns the offset description of the summer time zone in the west of Greenwich. If the region is in the east of Greenwich, it will return a negative value (such as Western Europe, including the United States).
    Syntax:
m = time.altzone
print("m =",m)
>>>m = -32400
  1. asctime()
    parameter: the ancestor of 9 elements or the time value
    returned by the function gmtime() or localtime() Return value: return a readable form of "Tue Dec 11 18:07:14 2008" (October 2018 Tuesday, 11th, 18:07:14) a string of 24 characters.
    grammar:
time.asctime()
>>>'Tue Oct 23 16:27:39 2018'
time.asctime(time.localtime())
>>>'Tue Oct 23 16:28:08 2018'
  1. perf_counter()
    parameter: None
    Return value:
    This function has two functions.
    When it is called for the first time, it returns the actual time the program is running;
    when it is called after the second time, it returns since the first call. , The time interval to this call
    Syntax:
print(time.perf_counter())
for i in range(1, 100):
    print(i)
print(time.perf_counter())
>>>0.0
>>>6.6986723828671e-05
  1. ctime()
    parameter: the number of seconds
    to be converted to string time Return value: none
    Syntax:
print(time.ctime())
'Tue Oct 23 16:38:18 2018'
print(time.ctime(1024))
'Thu Jan  1 08:17:04 1970'
  1. gmtime()
    parameter: converted to the opposite number of seconds of the time.stryct_time type
    Return value: None
    Syntax:
print(time.gmtime())
>>>time.struct_time(tm_year=2018, tm_mon=10, tm_mday=23, tm_hour=8, tm_min=41, tm_sec=2, tm_wday=1, tm_yday=296, tm_isdst=0)
print(time.gmtime(45))
>>>time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=0, tm_min=0, tm_sec=45, tm_wday=3, tm_yday=1, tm_isdst=0)
print(time.gmtime(2))
>>>time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=0, tm_min=0, tm_sec=2, tm_wday=3, tm_yday=1, tm_isdst=0)
  1. locatime()
    parameter: converted to the opposite number of seconds of type time.struct_time
    Return value: None
    Syntax:
print(time.localtime())
>>>time.struct_time(tm_year=2018, tm_mon=10, tm_mday=23, tm_hour=16, tm_min=44, tm_sec=6, tm_wday=1, tm_yday=296, tm_isdst=0)
  1. sleep()
    parameter: the number of seconds
    to postpone execution Return value: none
    Syntax:
print(time.ctime())
>>>Tue Oct 23 16:49:45 2018
time.sleep(5)
print(time.ctime())
>>>Tue Oct 23 16:49:50 2018
  1. strftime(format[, t ])
    Parameters: format-format string t-optional parameter t is a struct_time object
    Return value: local time identified by a readable string
    Description:
    Time and date formatting symbols in python
%y 两位数的年份表示(00-99%Y 四位数的年份表示(000-9999%m 月份(01-12%d 月内中的一天(0-31%H 24小时制小时数(0-23%I 12小时制小时数(01-12%M 分钟数(00=59%S 秒(00-59%a 本地简化星期名称
%A 本地完整星期名称
%b 本地简化的月份名称
%B 本地完整的月份名称
%c 本地相应的日期表示和时间表示
%j 年内的一天(001-366%p 本地A.M.或P.M.的等价符
%U 一年中的星期数(00-53)星期天为星期的开始
%w 星期(0-6),星期天为星期的开始
%W 一年中的星期数(00-53)星期一为星期的开始
%x 本地相应的日期表示
%X 本地相应的时间表示
%Z 当前时区的名称
%% %号本身

语法:
print(time.strftime("%Y %m %d %H:%M:%S"))

2018 10 23 16:57:02’

  1. time()
    parameter: none
    Return value: return the timestamp of the current time (floating point seconds elapsed since the 1970 epoch)
print ("time.time(): %f " %  time.time())
>>>time.time(): 1540285177.428731
print (time.asctime( time.localtime(time.time()) ))
>>>Tue Oct 23 16:59:37 2018

Time Originator:

索引值(index)  属性(Attribute)(Values)
0   tm_year()  (例如:2015)
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(是否为夏令时)    0,1,-1(-1代表夏令时)

Second, the time conversion diagram

Insert picture description here

Guess you like

Origin blog.csdn.net/zhuan_long/article/details/110002479