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.
目录
相关文章
|
3天前
|
调度 开发者 网络架构
探索Python中的异步编程:深入理解asyncio库
【9月更文挑战第32天】在现代软件开发中,异步编程已成为提升性能和响应性的关键策略之一。本文将深入探讨Python的asyncio库,一个强大的异步I/O框架,它允许开发者编写单线程并发代码,同时处理多个任务而无需复杂的多线程或多进程编程。通过本文,你将学习到如何利用asyncio来构建高效、可扩展的应用程序,并了解其背后的原理和设计哲学。
7 2
|
6天前
|
安全 Python
Python量化炒股的获取数据函数—get_industry()
Python量化炒股的获取数据函数—get_industry()
15 3
|
6天前
|
Python
Python sorted() 函数和sort()函数对比分析
Python sorted() 函数和sort()函数对比分析
|
5天前
|
数据挖掘 Python
【Python】应用:pyproj地理计算库应用
这篇博客介绍了 `pyproj` 地理计算库的应用,涵盖地理坐标系统转换与地图投影。通过示例代码展示了如何进行经纬度与UTM坐标的互转,并利用 `pyproj.Geod` 计算两点间的距离及方位角,助力地理数据分析。 安装 `pyproj`:`pip install pyproj`。更多内容欢迎关注本博客,一起学习进步! Pancake 🍰 不迷路。😉*★,°*:.☆( ̄▽ ̄)/$:*.°★* 😏
11 1
|
6天前
|
Python
Python量化炒股的获取数据函数—get_security_info()
Python量化炒股的获取数据函数—get_security_info()
14 1
|
6天前
|
Python
Python量化炒股的获取数据函数— get_billboard_list()
Python量化炒股的获取数据函数— get_billboard_list()
12 0
|
6天前
|
安全 数据库 数据格式
Python量化炒股的获取数据函数—get_fundamentals()
Python量化炒股的获取数据函数—get_fundamentals()
14 0
|
4月前
|
开发工具 git Python
安装和使用`libnum`是一个用于数字理论函数的Python库
【6月更文挑战第19天】`libnum`是Python的数字理论函数库。安装可通过`git clone`,进入目录后运行`python setup.py install`,也可用`pip install libnum`。示例:使用`int_to_hex`将十进制数42转换为十六进制字符串&#39;2a&#39;。注意,信息可能已过时,应查最新文档以确保准确性。如遇问题,参考GitHub仓库或寻求社区帮助。
89 1
|
3月前
|
Python
确保你已经安装了`python-barcode`库。如果没有,可以通过pip来安装:
确保你已经安装了`python-barcode`库。如果没有,可以通过pip来安装:
|
Linux Python
不可出外网的主机如何快速、方便、优雅的安装Python库?
不可出外网的主机如何快速、方便、优雅的安装Python库?
493 0
不可出外网的主机如何快速、方便、优雅的安装Python库?
下一篇
无影云桌面