Python——time模块

三种时间格式转化

 

1. time() 函数

time( )函数用于返回当前时间的时间戳(从1970年1月1日00时00分00秒到现在的浮点秒数)

time()函数的语法如下:

        time.time()
 
1、此语法中第一个 time 表示 time 模块,该函数不需要传递参数
2、返回值:返回当前时间的时间戳
 

>>> import time
>>>
>>> print("当前时间的时间的时间戳:%f" % time.time())
当前时间的时间的时间戳:1536375255.196752

2. localtime([secs])  函数

time.localtime( )函数的作用是格式化时间戳为本地时间(struct_time类型)。如果secs参数未传入,就以当前时间为转换标准

localtime() 方法的语法:

   time.localtime([ secs ])
 
1、参数secs -- 指转换为  time.struct_time 类型的对象的秒数
2、返回值:该函数无任何返回值
 

>>> import time
>>>
>>> print(time.localtime())
time.struct_time(tm_year=2018, tm_mon=9, tm_mday=8, tm_hour=10, tm_min=59, tm_sec=39, tm_wday=5, tm_yday=251, tm_isdst=0)

3. gmtime([secs]) 函数

gmtime( )  函数用于将一个时间戳转换为UTC时区(0时区)的 struct_time。可选参数 secs 表示从1970-1-1 到现在的秒数,无参数时默认为本地时间

函数返回 time.struct_time 类型的对象 (struct_time 是在 time 模块中定义的表示时间的对象)

gmtime([secs]) 的语法如下:

    time.gmtime([secs])
 
1、参数secs -- 指转换为 time.struct_time 类型的对象的秒数
2、返回值:该函数无任何返回值
 

>>> import time
>>>
>>> time.gmtime()
time.struct_time(tm_year=2018, tm_mon=9, tm_mday=8, tm_hour=8, tm_min=22, tm_sec=14, tm_wday=5, tm_yday=251, tm_isdst=0)

4. mktime(t)  函数

mktime( )函数用于执行与 gmtime()、localtime() 相反的操作,接收 struct_time 对象作为参数,返回用秒数表示时间的浮点数。如果输入的值不是合法时间,就会触发OverflowError或ValueError

以下是 mktime()方法的语法:

     time.mktime(t)
 
1、参数t -- 这是 struct_time  (结构化时间)或全满 9 个元素的元组。
 
2、返回值:该方法返回一个浮点数,为了兼容time()函数。
 

>>> import time
>>>
>>> t = ( 2018,9,8,16,34,30,5,251,0)
>>>
>>> time.mktime(t)
1536395670.0
>>>
>>>
>>> time.mktime(time.localtime())
1536395774.0

5. asctime([t]) 函数

该函数用于接收一个时间元组并返回一个可读的形式为"Tue Dec 11 18:07:14 2008"(2008年12月11日 周二18时07分14秒)的24个字符的字符串。

asctime() 方法的语法:

    time.asctime([t]))
 
1、参数  t -- 完整的9位元组元素或 struct_time 表示,由 gmtime() 和 localtime() 函数返回的时间的元组。
 
2、返回值:此方法返回以下形式的24个字符的字符串: 'Tue Feb 17 23:21:05 2009'.
 

>>> import time
>>>
>>> t = ( 2018,9,8,16,34,30,5,251,0)
>>>
>>> time.asctime(t)
'Sat Sep  8 16:34:30 2018'
>>>
>>>
>>> time.asctime(time.localtime())
'Sat Sep  8 16:41:27 2018'
>>>
>>> time.asctime()
'Sat Sep  8 16:41:54 2018'

6. ctime([ secs ])  函数

用于把一个时间戳转换为time.asctime()的形式。如果未给参数  secs 或参数为  None,就会默认将  time.time() 作为参数。

语法如下:

   time.ctime([ sec ])
 
1、参数sec -- 这是将被转换成字符串表示的秒数。
 
2、返回值:此方法不返回任何值。
 

>>> import time
>>>
>>> time.ctime()
'Sat Sep  8 17:08:55 2018'
>>>
>>> time.ctime(1455502418.000000)
'Mon Feb 15 10:13:38 2016'

7.  sleep(secs) 函数

sleep()函数用于推迟调用线程的运行,可通过参数secs指定进程挂起的时间

sleep()方法的语法:

  time.sleep(t)
  
1、参数t -- 这是要暂停执行的秒数。
 
2、返回值:此方法不返回任何值。
 

>>> import time
>>>
>>> def sleep_time():
...     print(time.ctime())
...     time.sleep(5)
...     print(time.ctime())
...
>>>
>>>
>>> sleep_time()
Sat Sep  8 17:18:52 2018
Sat Sep  8 17:18:57 2018

8. clock()  函数

clock()  函数用于以浮点数计算的秒数返回当前的  CPU  (处理器)时间。用来衡量不同程序的耗时,比time.time()更有用。

该函数在不同的系统上含义不同在UNIX系统上,它返回的是"进程时间"(unix系统始终返回全部运行时间),它是用秒表示的浮点数(时间戳)。而在Windows中,第一次调用,返回的是进程运行的实际时间。而第二次之后的调用是自第一次调用以后到现在的运行时间.


clock()方法语法:

    time.clock()
 
参数:无
返回值:在 win32 系统下,这个函数返回的是真实时间(wall time),而在Unix/Linux下返回的是CPU时间。
 

【注】

time.clock已在Python 3.3中弃用,将从Python 3.8中删除:使用time.perf_counter或time.process_time代替

9. strftime(format[, t])  函数

strftime()  方法用于接收时间元组,并返回以可读字符串表示的当地时间。格式由format参数决定。

strftime()只能接受struct_time类型的参数,若提供的是9位元素的时间元组,则需要将其转化为时间戳再转化为struct_time类型的时间元组

如果不提供 t  (tupletime),则默认使用  localtime()  函数返回的当前时间。格式必须是字符串。如果  tupletime  的任何字段在允许的范围之外,那么异常  ValueError  将会被引发。

strftime()方法的语法:

    time.strftime(format  [,t]  )
 
1、参数  t -- 这是要被格式化以秒为单位的时间,为一个可选参数。
 
2、参数format -- 这将用于格式化给定时间的指令。
 
3、返回值:返回以可读字符串表示的当地时间
 

import time

t = (2016, 9, 25, 17, 50, 38, 6, 48, 0)
t = time.mktime(t)
print(time.strftime('%b %d %Y %H:%M:%S', time.gmtime(t)))

print("\n\n",time.strftime('%b %d %Y %H:%M:%S'))

print("\n\n",time.strftime('%H:%M:%S'))

执行结果:

Sep 25 2016 09:50:38


 Sep 08 2018 21:30:54


 21:30:54

10.  strPtime(string [, format])  函数

strptime( )函数用于根据format的格式把一个时间字符串解析为时间元组。

语法如下:

    strptime (string [,format] )
 
1、参数:
string -- 时间字符串
format  -- 指格式化字符串
 
2、返回值:返回  struct_time  对象
 

import time

struct_time = time.strptime("8 Sep 18", "%d %b %y")

print('returned tuple: ', struct_time)

执行结果:

returned tuple:  time.struct_time(tm_year=2018, tm_mon=9, tm_mday=8, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=5, tm_yday=251, tm_isdst=-1)

猜你喜欢

转载自blog.csdn.net/qq_41573234/article/details/82533820