Python学习笔记 - 模块 - time 模块

简介

时间模块,用于延迟程序运行和获取当前时间

帮助文档请点击链接:https://docs.python.org/library/time

使用方法

1
import time # 引入模块

简单使用

1
2
import time
...

类使用

1
2
from time import struct_time
t = struct_time()

常用函数

  • time.sleep(seconds)
  • time.time()
  • time.gmtime([seconds])
  • time.localtime([seconds])
  • time.mktime(tuple)
  • time.strftime()
  • time.strptime()

time.gmtime([seconds])

-> (tm_year, tm_mon, tm_mday, tm_hour, tm_min, tm_sec, tm_wday, tm_yday, tm_isdst)

接收一个可选的整数参数 seconds,其值为时间戳,如果省略则使用现在的时间。返回一个由传入的时间戳转换为的 UTC 时间构造的 time.struct_time 类的实例。

time.localtime([seconds])

-> (tm_year, tm_mon, tm_mday, tm_hour, tm_min, tm_sec, tm_wday, tm_yday, tm_isdst)        

接收一个可选的整数参数 seconds,其值为时间戳,如果省略则使用现在的时间。返回一个由传入的时间戳转换为当前市区时间构造的 time.struct_time 类的实例。

mktime(tuple)

接收一个元组 tuple,这个元组使用 time.struct_time 类的实例或 (tm_year, tm_mon, tm_mday, tm_hour, tm_min, tm_sec, tm_wday, tm_yday, tm_isdst) 格式,将会返回此元组时间对应的时间戳。

time.sleep(seconds)

接收一个数字参数 seconds ,用处为让程序暂停运行 seconds

time.strftime(format[, tuple])

接收两个参数,第一个参数为一个字符串 format,第二个参数为一个元组 tuple(使用 time.struct_time 类的实例或 (tm_year, tm_mon, tm_mday, tm_hour, tm_min, tm_sec, tm_wday, tm_yday, tm_isdst) 格式)(可选,不提供则使用 localtime),将会返回这个时间按照 format 参数格式指定的字符串。

格式化字符串

  • %Y:年(四位数字)
  • %y:年(两位数字)
  • %m:月(两位数字)
  • %d:日(两位数字)
  • %H:时(两位数字,01-23)
  • %I:时(两位数字,01-12)
  • %p:上午OR下午(AM 或 PM)
  • %M:分(两位数字)
  • %S:秒(两位数字) Second as a decimal number [00,61].
  • %b:月(名称,简写)
  • %B:月(名称,全称)
  • %w:星期X(一位数字,0-6,0为周日)
  • %a:星期X(名称,简写)
  • %A:星期X(名称,全称)
  • %U & %W:当年的第X周(两位数字,00-53)
  • %j:当年的第X日(三位数字)
  • %z:相对于 UTC 的时间(HHMM 格式,-2359 ~ +2359)
  • %Z:当前时区名称(未设置则为空)
  • %c:等价于 %a %b %d %H:%M:%S %Y
  • %x:等价于 %m/%D/%Y
  • %X:等价于 %H:%M:5S
  • %%:百分号 %

time.strptime(string, format)

接收两个参数,第二个参数 format 为格式化的格式,第一个为根据 format 的格式格式化的字符串 string,返回根据这两个参数而构造的 time.struct_time 对象,未提供的则以 localtime 为准

time.time()

返回 Unix 时间戳(即 1970-01-01 08:00:00 至今的秒数)

不常用函数

time.asctime([tuple])

    asctime([tuple]) -> string
    
    Convert a time tuple to a string, e.g. 'Sat Jun 06 16:26:11 1998'.
    When the time tuple is not present, current time as returned by localtime()
    is used.

time.clock()

返回程序从运行开始到执行此语句时 CPU 执行的时间(单位为秒)

time.ctime(seconds)

    ctime(seconds) -> string
    
    Convert a time in seconds since the Epoch to a string in local time.
    This is equivalent to asctime(localtime(seconds)). When the time tuple is
    not present, current time as returned by localtime() is used.

time.tzset(zone)

    tzset(zone)
    
    Initialize, or reinitialize, the local timezone to the value stored in
    os.environ['TZ']. The TZ environment variable should be specified in
    standard Unix timezone format as documented in the tzset man page
    (eg. 'US/Eastern', 'Europe/Amsterdam'). Unknown timezones will silently
    fall back to UTC. If the TZ environment variable is not set, the local
    timezone is set to the systems best guess of wallclock time.
    Changing the TZ environment variable without calling tzset *may* change
    the local timezone used by methods such as localtime, but this behaviour
    should not be relied on.

类使用

CLASSES
builtin.object
struct_time

class struct_time(__builtin__.object)
 |  Methods defined here:
 |  
 |  __add__(...)
 |      x.__add__(y) <==> x+y
 |  
 |  __contains__(...)
 |      x.__contains__(y) <==> y in x
 |  
 |  __eq__(...)
 |      x.__eq__(y) <==> x==y
 |  
 |  __ge__(...)
 |      x.__ge__(y) <==> x>=y
 |  
 |  __getitem__(...)
 |      x.__getitem__(y) <==> x[y]
 |  
 |  __getslice__(...)
 |      x.__getslice__(i, j) <==> x[i:j]
 |      
 |      Use of negative indices is not supported.
 |  
 |  __gt__(...)
 |      x.__gt__(y) <==> x>y
 |  
 |  __hash__(...)
 |      x.__hash__() <==> hash(x)
 |  
 |  __le__(...)
 |      x.__le__(y) <==> x<=y
 |  
 |  __len__(...)
 |      x.__len__() <==> len(x)
 |  
 |  __lt__(...)
 |      x.__lt__(y) <==> x<y
 |  
 |  __mul__(...)
 |      x.__mul__(n) <==> x*n
 |  
 |  __ne__(...)
 |      x.__ne__(y) <==> x!=y
 |  
 |  __reduce__(...)
 |  
 |  __repr__(...)
 |      x.__repr__() <==> repr(x)
 |  
 |  __rmul__(...)
 |      x.__rmul__(n) <==> n*x
 |  
 |  ----------------------------------------------------------------------
 |  Data descriptors defined here:
 |  
 |  tm_hour
 |  
 |  tm_isdst
 |  
 |  tm_mday
 |  
 |  tm_min
 |  
 |  tm_mon
 |  
 |  tm_sec
 |  
 |  tm_wday
 |  
 |  tm_yday
 |  
 |  tm_year
 |  
 |  ----------------------------------------------------------------------
 |  Data and other attributes defined here:
 |  
 |  __new__ = <built-in method __new__ of type object>
 |      T.__new__(S, ...) -> a new object with type S, a subtype of T
 |  
 |  n_fields = 9
 |  
 |  n_sequence_fields = 9
 |  
 |  n_unnamed_fields = 0