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.
目录
相关文章
|
1天前
|
XML 前端开发 数据格式
BeautifulSoup 是一个 Python 库,用于从 HTML 和 XML 文件中提取数据
BeautifulSoup 是 Python 的一个库,用于解析 HTML 和 XML 文件,即使在格式不规范的情况下也能有效工作。通过创建 BeautifulSoup 对象并使用方法如 find_all 和 get,可以方便地提取和查找文档中的信息。以下是一段示例代码,展示如何安装库、解析 HTML 数据以及打印段落、链接和特定类名的元素。BeautifulSoup 还支持更复杂的查询和文档修改功能。
6 1
|
1天前
|
机器学习/深度学习 自然语言处理 算法
Gensim详细介绍和使用:一个Python文本建模库
Gensim详细介绍和使用:一个Python文本建模库
10 1
|
2天前
|
JSON 数据格式 Python
Python 的 requests 库是一个强大的 HTTP 客户端库,用于发送各种类型的 HTTP 请求
`requests` 库是 Python 中用于HTTP请求的强大工具。要开始使用,需通过 `pip install requests` 进行安装。发送GET请求可使用 `requests.get(url)`,而POST请求则需结合 `json.dumps(data)` 以JSON格式发送数据。PUT和DELETE请求类似,分别调用 `requests.put()` 和 `requests.delete()`。
11 2
|
2天前
|
JSON 数据格式 索引
python之JMESPath:JSON 查询语法库示例详解
python之JMESPath:JSON 查询语法库示例详解
13 0
|
3天前
|
Java C# 开发者
Python 中的类型注解是一种用于描述变量、函数参数和返回值预期类型的机制
Python的类型注解提升代码可读性和可维护性,虽非强制,但利于静态类型检查(如Mypy)。包括:变量注解、函数参数和返回值注解,使用内置或`typing`模块的复杂类型,自定义类型注解,以及泛型模拟。类型注解可在变量声明、函数定义和注释中使用,帮助避免类型错误,提高开发效率。
15 6
|
4天前
|
存储 Python
【Python 基础】解释reduce函数的工作原理
【5月更文挑战第6天】【Python 基础】解释reduce函数的工作原理
|
4天前
|
Python
【Python 基础】解释map函数的工作原理
【5月更文挑战第6天】【Python 基础】解释map函数的工作原理
|
4天前
|
索引 Python
【Python 基础】解释Range函数
【5月更文挑战第6天】【Python 基础】解释Range函数
|
Shell Python Windows
Python Module_pdb_DEBUG 方法
目录 目录 pdb pdb 的 Debug 方式 pdb 的调试指令 示例 IPython 自带的 Debug 工具 ipdb pdb pdb 是 Python 自带的程序包,为 Python 程序提供了一种可交互的源码调试功能。
1301 0
|
2天前
|
JSON 数据格式 开发者
pip和requests在Python编程中各自扮演着不同的角色
`pip`是Python的包管理器,用于安装、升级和管理PyPI上的包;`requests`是一个HTTP库,简化了HTTP通信,支持各种HTTP请求类型及数据交互。两者在Python环境中分别负责包管理和网络请求。
13 5