python-time模块

0.摘要

本文主要介绍python中time模块中的方法。

 

1.获取当前时间

import time
print(time.time())   #1550236531.8000307

time.time()返回的是当前时间的时间戳,并以浮点秒数显示(计时起点为1970纪元)

 

2.以标准格式显示

time localtime([seconds]) 作用是格式化时间戳为本地的时间,与time.gmtime()相同; 

能够将seconds转换为格式化的时间元组,计时起点为1970纪元;

若seconds未指定,则默认为当前时间。

import time


print(time.localtime(0))            
#time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=8, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=1, tm_isdst=0)
print(time.localtime(time.time()))  
#time.struct_time(tm_year=2019, tm_mon=2, tm_mday=15, tm_hour=21, tm_min=24, tm_sec=27, tm_wday=4, tm_yday=46, tm_isdst=0)
print(time.gmtime())                
#time.struct_time(tm_year=2019, tm_mon=2, tm_mday=15, tm_hour=21, tm_min=24, tm_sec=27, tm_wday=4, tm_yday=46, tm_isdst=0)

返回值含义如下:

struct tm {
        int tm_sec;     /* seconds after the minute - [0,59] */
        int tm_min;     /* minutes after the hour - [0,59] */
        int tm_hour;    /* hours since midnight - [0,23] */
        int tm_mday;    /* day of the month - [1,31] */
        int tm_mon;     /* months since January - [0,11] */
        int tm_year;    /* years since 1900 */
        int tm_wday;    /* days since Sunday - [0,6] */
        int tm_yday;    /* days since January 1 - [0,365] */
        int tm_isdst;   /* daylight savings time flag */
          }

 

3.以冒号分隔显示

import time
print(time.asctime())                 #Fri Feb 15 22:01:03 2019
print(time.asctime(time.localtime())) #Fri Feb 15 22:01:03 2019
print(time.asctime(100))              #TypeError: Tuple or struct_time argument required
t = (1970, 1, 1, 8, 1, 40, 3, 1, 0)
print(time.asctime(t)) #Thu Jan  1 08:01:40 1970

time.asctime(seconds)能够以冒号分隔的方式显示时间,但其中的参数sedonds必须为函数 gmtime() 或 localtime() 返回的时间值或者包含9个元素的时间元组。

为了简化参数形式,time模块提供了另外一个函数,ctime()函数。

time.ctime(seconds) == time.asctime(localtime(seconds))

print(time.ctime(100))  #Thu Jan  1 08:01:40 1970

 

4.自定义格式

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

  • format -- 格式字符串。
  • t -- 可选的参数t是一个struct_time对象
import time
print(time.localtime())  
#time.struct_time(tm_year=2019, tm_mon=2, tm_mday=15, tm_hour=22, tm_min=39, tm_sec=54, tm_wday=4, tm_yday=46, tm_isdst=0)
print(time.strftime("%Y-%m-%d %H:%M:%S",time.localtime()))       #2019-02-15 22:39:54
print(time.strftime("%a %b %d %H:%M:%S %Y", time.localtime()))   #Fri Feb 15 22:39:54 2019

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 当前时区的名称
  • %% %号本身

 

5.sleep()

Python time sleep() 函数推迟调用线程的运行,可通过参数secs指秒数,表示进程挂起的时间。

import time
print("Start : %s" % time.ctime())    #Start : Fri Feb 15 22:41:56 2019
time.sleep(5) 
print("End : %s" % time.ctime())      #End : Fri Feb 15 22:42:01 2019

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值