time 库常用函数

time 库是 Python 中用于处理时间相关操作的常用模块,提供了获取时间、格式化时间、时间戳转换以及程序计时等功能。以下是 time 库中的常用函数及其详细说明:

一、时间获取函数

  1. time.time()

    • 功能:获取当前时间的时间戳,即从世界标准时间(UTC)1970 年 1 月 1 日 00:00:00 开始到当前时刻的总秒数(浮点数)。
    • 示例
      import time
      timestamp = time.time()
      print(timestamp)  # 输出类似:1719345678.123456
      
  2. time.ctime([secs])

    • 功能:将时间戳转换为易读的字符串格式(本地时间)。若不提供参数,则默认使用当前时间戳。
    • 示例
      import time
      print(time.ctime())  # 输出类似:Mon Oct 27 12:34:56 2025
      print(time.ctime(1719345678.123456))  # 输出指定时间戳的字符串
      
  3. time.gmtime([secs])

    • 功能:将时间戳转换为 UTC 时间的 struct_time 对象(元组形式)。若不提供参数,则默认使用当前时间戳。
    • 示例
      import time
      utc_time = time.gmtime()
      print(utc_time)  # 输出类似:time.struct_time(tm_year=2025, tm_mon=10, ...)
      
  4. time.localtime([secs])

    • 功能:将时间戳转换为本地时间的 struct_time 对象(元组形式)。若不提供参数,则默认使用当前时间戳。
    • 示例
      import time
      local_time = time.localtime()
      print(local_time)  # 输出类似:time.struct_time(tm_year=2025, tm_mon=10, ...)
      

二、时间格式化函数

  1. time.strftime(format[, t])

    • 功能:将 struct_time 对象或时间戳格式化为指定格式的字符串。
    • 参数
      • format:格式化字符串,指定输出格式。
      • t(可选):struct_time 对象,若不提供则默认使用 time.localtime() 的结果。
    • 常用格式化符号
      • %Y:四位年份(如 2025)
      • %m:两位月份(01-12)
      • %d:两位日期(01-31)
      • %H:24 小时制小时(00-23)
      • %M:分钟(00-59)
      • %S:秒(00-59)
      • %A:星期全名(如 Monday)
      • %a:星期缩写(如 Mon)
      • %B:月份全名(如 October)
      • %b:月份缩写(如 Oct)
    • 示例
      import time
      formatted_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
      print(formatted_time)  # 输出类似:2025-10-27 12:34:56
      
  2. time.strptime(string, format)

    • 功能:将字符串解析为 struct_time 对象。
    • 参数
      • string:时间字符串。
      • format:与 strftime 相同的格式化字符串。
    • 示例
      import time
      time_tuple = time.strptime("2025-10-27 12:34:56", "%Y-%m-%d %H:%M:%S")
      print(time_tuple)  # 输出类似:time.struct_time(tm_year=2025, tm_mon=10, ...)
      

三、时间戳转换函数

  1. time.mktime(t)
    • 功能:将 struct_time 对象转换为时间戳(秒数)。
    • 参数tstruct_time 对象,表示当地时间。
    • 示例
      import time
      time_tuple = time.localtime()
      timestamp = time.mktime(time_tuple)
      print(timestamp)  # 输出类似:1719345678.0
      

四、程序计时函数

  1. time.sleep(secs)

    • 功能:暂停程序执行指定的秒数(支持浮点数)。
    • 示例
      import time
      print("开始")
      time.sleep(2.5)  # 暂停 2.5 秒
      print("结束")
      
  2. time.perf_counter()

    • 功能:返回一个高精度的时间计数值(秒),用于测量短时间间隔。
    • 示例
      import time
      start = time.perf_counter()
      # 模拟耗时操作
      for _ in range(1000000):
          pass
      end = time.perf_counter()
      print(f"耗时:{end - start:.6f} 秒")
      

五、其他实用函数

  1. time.asctime([t])

    • 功能:将 struct_time 对象转换为字符串,格式为 "Mon Oct 27 12:34:56 2025"
    • 示例
      import time
      print(time.asctime(time.localtime()))  # 输出类似:Mon Oct 27 12:34:56 2025
      
  2. time.tzset()

    • 功能:根据环境变量 TZ 设置时区(仅在 Unix 系统有效)。
    • 示例
      import time
      import os
      os.environ['TZ'] = 'Asia/Shanghai'
      time.tzset()
      print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
      
posted @ 2025-10-26 01:09  nxhujiee  阅读(6)  评论(0)    收藏  举报