2

Python标准库time详解

 2 years ago
source link: https://blog.51cto.com/u_12907475/5452605
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Python标准库time详解

原创

Python标准库time详解

1、time库

  • 时间戳(timestamp)的方式:通常来说,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量
  • 结构化时间(struct_time)方式:struct_time元组共有9个元素
  • 格式化的时间字符串(format_string),时间格式的字符串

1.1、获取格林威治西部的夏令时地区的偏移秒数

  • 如果该地区在格林威治东部会返回负值(如西欧,包括英国)
  • 对夏令时启用地区才能使用
# coding:utf-8
import time
# 获取格林威治西部的夏令时地区的偏移秒数。
print(time.altzone)
Python标准库time详解_python

1.2、时间函数

  • 时间戳
    指格林威治时间1970年01月01日00时00分00秒(北京时间1970年01月01日08时00分00秒)起至现在的总秒数
  • 时间元组
    用一个元组装起来的9组数字
字段 含义
tm_year 4位数年 2008
tm_mon 1 到 12
tm_mday 1 到 31
tm_hour 小时 0 到 23
tm_min 分钟 0 到 59
tm_sec 0 到 61(60或61 是闰秒)
tm_wday 一周的第几日 0 到 6(0是周一)
tm_yday 一年的第几日 1 到 366(儒略历)
tm_isdst 夏令时 -1, 0, 1 是决定是否为夏令时的旗帜
# coding:utf-8
import time
# 返回当前时间的时间戳(1970纪元后经过的浮点秒数)
print(time.time())
#  接收时间戳返回一个24个定长可读形式的字符串
print(time.ctime()) # Thu Jul  7 20:17:14 2022
print(len(time.ctime()), type(time.ctime()))    # 24 <class 'str'>
# 接收时间戳(1970纪元后经过的浮点秒数)并返回当地时间下的时间元组
print(time.localtime())
print(time.localtime(time.time()))
# 接收时间戳(1970纪元后经过的浮点秒数)并返回格林威治天文时间下的时间元组
print(time.gmtime())
print(time.gmtime(time.time()))
# 接收时间元组并返回一个可读的形式为"Thu Jul  7 20:11:04 2022"(2022年7月7日 周四20时11分04秒)的字符串,长度固定为24字符
print(time.asctime())
print(time.asctime(time.gmtime()))
print(time.asctime(time.localtime()))
print(len(time.asctime()), type(time.asctime()))  # 24 <class 'str'>
# 接受时间元组并返回时间戳(1970纪元后经过的浮点秒数)
# print(time.mktime())  # 参数不可为空 TypeError: time.mktime() takes exactly one argument (0 given)
print(time.mktime(time.gmtime()))
print(time.mktime(time.localtime()))
print(type(time.mktime(time.gmtime())))  # 浮点秒数<class 'float'>

# 返回以秒为单位的时间浮点值
print(time.perf_counter())
print(type(time.perf_counter()))
# 返回以纳秒为单位的时间整数值
print(time.perf_counter_ns())           # <class 'float'>
print(type(time.perf_counter_ns()))     # <class 'int'>
Python标准库time详解_python_02

1.3、格式化时间、日期

  • 时间格式,格式化日期和时间时使用
格式符号 符号的含义
%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),星期天为 0,星期一为 1,以此类推。
%W 一年中的星期数(00-53)星期一为星期的开始
%x 本地相应的日期表示
%X 本地相应的时间表示
%Z 当前时区的名称
%% %号本身
# coding:utf-8
import time
import datetime

# 格式化日期
print(time.strftime('%Y-%m-%d %H:%M:%S'))
print(time.strftime('%Y-%m-%d %I:%M:%S'))
print(time.strftime('%X'))
print(time.strftime('%Z'))
# 将字符串转 时间元组
# '%a %b %d %H:%M:%S %Y'
print(time.strptime('2022-07-07 09:58:24', '%Y-%m-%d %H:%M:%S'))
print(time.strptime('2022-07-07 09:58:24', '%Y-%m-%d %I:%M:%S'))

Python标准库time详解_time_03

1.4、单调时钟

  • 单调时钟是不能向后移动的时钟
  • 常用来计算程序运行处理时长
# coding:utf-8
import time
# 获取 单调时钟的值,单调时钟是不能向后移动的时钟
# 常用来计算程序运行处理时长
start_time = time.monotonic()
i = 0
print(f"循环开始时钟值:{start_time}", type(start_time))  # 循环开始时钟值:6718828000000 <class 'int'>
while i <= 10:
    time.sleep(0.1)
    i += 1
end_time = time.monotonic()
print(f"循环结束时钟值:{end_time}")
print(f"循环运行时间为:{end_time - start_time} 秒")
# 获取单调时钟的值 以纳秒计算
start_time_ns = time.monotonic_ns()
i = 0
print(f"循环开始时钟值:{start_time_ns}", type(start_time_ns))  # 循环开始时钟值:6718828000000 <class 'int'>
while i <= 10:
    time.sleep(0.1)
    i += 1
end_time_ns = time.monotonic_ns()
print(f"循环结束时钟值:{end_time_ns}")
print(f"循环运行时间为:{end_time_ns - start_time_ns} 纳秒")
# 返回以秒为单位的时间浮点值,用法同time.monotonic()
print(time.perf_counter())
print(type(time.perf_counter()))
# 返回以纳秒为单位的时间整数值,用法同time.monotonic_ns()
print(time.perf_counter_ns())           # <class 'float'>
print(type(time.perf_counter_ns()))     # <class 'int'>
# 返回当前系统时间与CPU时间的浮动值(以秒为单位)
print(time.process_time())
# 返回当前系统时间与CPU时间的浮动值(以纳秒为单位)
print(time.process_time_ns())
# 睡眠,参数的单位为秒
time.sleep(1) # 程序等待1秒钟后再执行
Python标准库time详解_时间_04
  • 收藏
  • 评论
  • 分享
  • 举报

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK