Python built-in module time 内建时间库常用函数

简介: Python built-in module time 内建时间库常用函数

built-in moduletime


1. time() time_ns()

从1970-1-1 0:00到现在的秒数、纳秒数。返回值类型分别为float和int,两者的值相差10^9倍。


time(...)
    time() -> floating point number
    Return the current time in seconds since the Epoch.
    Fractions of a second may be present if the system clock provides them.
time_ns(...)
    time_ns() -> int
    Return the current time in nanoseconds since the Epoch.


>>> import time
>>> time.time
<built-in function time>
>>> time.time()
1620908340.6910295
>>> time.time_ns()
1620908347245421300
>>> type(time.time())
<class 'float'>
>>> type(time.time_ns())
<class 'int'>
>>> time.time()==time.time_ns()/10**9
True
>>> 


2. time.struct_time

时间的结构,本质是一个元组tuple,由9个整数组成的序列,分别是:年月日、时分秒、星期、该天在一年中的序号、夏令时标识;函数gmtime()、localtime()和strptime() 的返回值就是时间元组,函数asctime()、mktime()和strftime()的参数也是时间元组。


class struct_time(builtins.tuple)
    struct_time(iterable=(), /)
    The time value as returned by gmtime(), localtime(), and strptime(), and accepted by asctime(), mktime() and strftime().  May be considered as a sequence of 9 integers.
    Note that several fields' values are not the same as those defined by the C language standard for struct tm.  
    For example, the value of the field tm_year is the actual year, not year - 1900.  See individual fields' descriptions for details.
>>> import time
>>> time.struct_time((0,0,0,0,0,0,0,0,0))
time.struct_time(tm_year=0, tm_mon=0, tm_mday=0, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=0, tm_yday=0, tm_isdst=0)
>>> 
>>> type(time.struct_time((1,1,1,1,1,1,1,1,1)))
<class 'time.struct_time'>
>>> 
'''
struct_time:
int tm_year; /* 年份,实际年份[1,9999] */
int tm_mon; /* 月份 [0,11] 0代表一月 */
int tm_mday; /* 日期[1,31] */
int tm_hour; /* 时 [0,23] */
int tm_min; /* 分 [0,59] */
int tm_sec; /* 秒 [0,59] */
int tm_wday; /* 星期 [0,6] 0代表星期一 */
int tm_yday; /* 从每年的1月1日开始的天数 – 取值区间为[0,365],其中0代表1月1日,1代表1月2日,以此类推 */
int tm_isdst; /* 夏令时标识符,实行夏令时的时候,tm_isdst为正。不实行夏令时的时候,tm_isdst为0;不了解情况时,tm_isdst()为负。
'''



3. gmtime() localtime()

gmtime([seconds]) 将秒数转换为表示UTC(格林威治标准)的时间结构。

gmtime(...)
    gmtime([seconds]) -> (tm_year, tm_mon, tm_mday, tm_hour, tm_min, tm_sec, tm_wday, tm_yday, tm_isdst)
    Convert seconds since the Epoch to a time tuple expressing UTC (a.k.a. GMT).  When 'seconds' is not passed in, convert the current time instead.
    If the platform supports the tm_gmtoff and tm_zone, they are available as attributes only.


localtime([seconds]) 将秒数转换为表示本地(北京时间)的时间结构。

localtime(...)
    localtime([seconds]) -> (tm_year,tm_mon,tm_mday,tm_hour,tm_min, tm_sec,tm_wday,tm_yday,tm_isdst)
    Convert seconds since the Epoch to a time tuple expressing local time.
    When 'seconds' is not passed in, convert the current time instead.


注:北京时间是东八时区UTC+8,两者相差8小时;默认是转换time()返回的秒数。

>>> import time
>>> a,b=time.gmtime(),time.localtime()
>>> [t1==t2 for i,t1 in enumerate(a) for j,t2 in enumerate(b) if i==j]
[True, True, True, False, True, True, True, True, True]
>>> [t2-t1 for i,t1 in enumerate(a) for j,t2 in enumerate(b) if i==j]
[0, 0, 0, 8, 0, 0, 0, 0, 0]
>>> time.gmtime()==time.gmtime(time.time())
True
>>> time.localtime()==time.localtime(time.time())
True
>>> 



4.  mktime()

把时间元组或时间结构转成秒数。功能正好与gmtime() localtime()相反。

mktime(...)
    mktime(tuple) -> floating point number
    Convert a time tuple in local time to seconds since the Epoch. Tuple or struct_time argument required.
    Note that mktime(gmtime(0)) will not generally return zero for most time zones; instead the returned value will either be equal to that of the timezone or altzone attributes on the time module.



5.  strftime() strptime()

strftime(tuple) 时间元组转字符串。

strftime(...)
    strftime(format[, tuple]) -> string
    Convert a time tuple to a string according to a format specification.
    See the library reference manual for formatting codes. When the time tuple is not present, current time as returned by localtime() is used.


strptime(string, format) 时间格式串转换成时间结构元组。

strptime(...)
    strptime(string, format) -> struct_time
    Parse a string to a time tuple according to a format specification.
    See the library reference manual for formatting codes (same as strftime()).



时间串格式:

 %Y  Year with century as a decimal number.
    %m  Month as a decimal number [01,12].
    %d  Day of the month as a decimal number [01,31].
    %H  Hour (24-hour clock) as a decimal number [00,23].
    %M  Minute as a decimal number [00,59].
    %S  Second as a decimal number [00,61].
    %z  Time zone offset from UTC.
    %a  Locale's abbreviated weekday name.
    %A  Locale's full weekday name.
    %b  Locale's abbreviated month name.
    %B  Locale's full month name.
    %c  Locale's appropriate date and time representation.
    %I  Hour (12-hour clock) as a decimal number [01,12].
    %p  Locale's equivalent of either AM or PM.
目录
相关文章
|
2月前
|
存储 JavaScript Java
(Python基础)新时代语言!一起学习Python吧!(四):dict字典和set类型;切片类型、列表生成式;map和reduce迭代器;filter过滤函数、sorted排序函数;lambda函数
dict字典 Python内置了字典:dict的支持,dict全称dictionary,在其他语言中也称为map,使用键-值(key-value)存储,具有极快的查找速度。 我们可以通过声明JS对象一样的方式声明dict
234 1
|
2月前
|
算法 Java Docker
(Python基础)新时代语言!一起学习Python吧!(三):IF条件判断和match匹配;Python中的循环:for...in、while循环;循环操作关键字;Python函数使用方法
IF 条件判断 使用if语句,对条件进行判断 true则执行代码块缩进语句 false则不执行代码块缩进语句,如果有else 或 elif 则进入相应的规则中执行
342 1
|
2月前
|
Java 数据处理 索引
(numpy)Python做数据处理必备框架!(二):ndarray切片的使用与运算;常见的ndarray函数:平方根、正余弦、自然对数、指数、幂等运算;统计函数:方差、均值、极差;比较函数...
ndarray切片 索引从0开始 索引/切片类型 描述/用法 基本索引 通过整数索引直接访问元素。 行/列切片 使用冒号:切片语法选择行或列的子集 连续切片 从起始索引到结束索引按步长切片 使用slice函数 通过slice(start,stop,strp)定义切片规则 布尔索引 通过布尔条件筛选满足条件的元素。支持逻辑运算符 &、|。
198 0
|
3月前
|
监控 数据可视化 数据挖掘
Python Rich库使用指南:打造更美观的命令行应用
Rich库是Python的终端美化利器,支持彩色文本、智能表格、动态进度条和语法高亮,大幅提升命令行应用的可视化效果与用户体验。
294 0
|
3月前
|
设计模式 缓存 监控
Python装饰器:优雅增强函数功能
Python装饰器:优雅增强函数功能
288 101
|
3月前
|
缓存 测试技术 Python
Python装饰器:优雅地增强函数功能
Python装饰器:优雅地增强函数功能
238 99
|
3月前
|
存储 缓存 测试技术
Python装饰器:优雅地增强函数功能
Python装饰器:优雅地增强函数功能
207 98
|
2月前
|
数据可视化 关系型数据库 MySQL
【可视化大屏】全流程讲解用python的pyecharts库实现拖拽可视化大屏的背后原理,简单粗暴!
本文详解基于Python的电影TOP250数据可视化大屏开发全流程,涵盖爬虫、数据存储、分析及可视化。使用requests+BeautifulSoup爬取数据,pandas存入MySQL,pyecharts实现柱状图、饼图、词云图、散点图等多种图表,并通过Page组件拖拽布局组合成大屏,支持多种主题切换,附完整源码与视频讲解。
288 4
【可视化大屏】全流程讲解用python的pyecharts库实现拖拽可视化大屏的背后原理,简单粗暴!
|
2月前
|
传感器 运维 前端开发
Python离群值检测实战:使用distfit库实现基于分布拟合的异常检测
本文解析异常(anomaly)与新颖性(novelty)检测的本质差异,结合distfit库演示基于概率密度拟合的单变量无监督异常检测方法,涵盖全局、上下文与集体离群值识别,助力构建高可解释性模型。
352 10
Python离群值检测实战:使用distfit库实现基于分布拟合的异常检测
|
Python
pip批量安装Python库 requirement.txt 离线环境无互联网环境下pip安装Python库
pip批量安装Python库 requirement.txt 离线环境无互联网环境下pip安装Python库
994 3

推荐镜像

更多