time模块学习

时间三种形式:

  1.timestamp   从1970-1-1 00:00到现在经历的秒数

  2.string_time   Sat Mar 28 22:24:24 2009

  3.struct_time  时间为9个元素的数组

    year (four digits, e.g. 1998)
    month (1-12)
    day (1-31)
    hours (0-23)
    minutes (0-59)
    seconds (0-59)
    weekday (0-6, Monday is 0)
    Julian day (day in the year, 1-366)
    DST (Daylight Savings Time) flag (-1, 0 or 1) 是否是夏令时

经常用的函数:

1. asctime([tuple]) --> string_time      ##将stuct_time转化为string_time,默认当前时间

    time.asctime()   ##将当前时间转化为string_time

    time.asctime((1989,01,01,8,8,1,0,4,0))   ##后面那三位对结果好像没有影响

2.ctime([seconds]) --> string_time           ##将timestamp转化为string_time,默认当前时间

    time.ctime()     ##将当前时间转化为string_time

    time.ctime(123123123.1)  ##将这个时间戳的时间转化为string_time

3.gmtime([seconds]) --> struct_time  ##将timestamp转化为UTC时区(0时区)的struct_time

    time.gmtime()

    time.gmtime(123123123)

4.localtime([seconds])  --> struct_time  ##将timestamp转化为当前时区的struct_time

    time.localtime()

    time.localtime(123123123)

5.mktime(tuple) --> stamptime             ##将struct_time转化为stamptime

    time.mktime((1973, 11, 26, 8, 52, 3, 0, 330, 0))  ##与localtime相反

6.strftime(format[,struct_time]) --> string_time   ##将struct_time通过格式符转化为string_time

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

    time.strftime('%Y-%m-%d')

    time.strftime('%Y-%m-%d %H-%M-%S',(1973, 11, 26, 8, 52, 3, 0, 330, 0))

7.strptime(string, format) -> struct_time  ##string_time根据格式化符转化为struct_time

    time.strptime('2013-09-09','%Y-%m-%d')

8.sleep          ##睡几秒,也就是暂停几秒

    time.sleep(10)          ##睡10秒

9.time     --> ##返回到今天的StampTime,不接受参数

    time.time()

转化图:

原文地址:https://www.cnblogs.com/cmsd/p/3310363.html