绵阳城乡住房建设厅网站,wordpress的安装包,wordpress前台美化,用wordpress 安装自己喜欢的主题 主题图片显示不对目录 1 time 模块1.1 struct_time 类1.2 常用函数 1 time 模块
time 模块提供了很多与时间相关的类和函数#xff0c;下面我们介绍一些常用的。
1.1 struct_time 类
time 模块的 struct_time 类代表一个时间对象#xff0c;可以通过索引和属性名访问值。对应关系如下所示下面我们介绍一些常用的。
1.1 struct_time 类
time 模块的 struct_time 类代表一个时间对象可以通过索引和属性名访问值。对应关系如下所示
索引属性值0tm_year年如79451tm_mon月1~122tm_mday日1~313tm_hour时0~234tm_min分0~595tm_sec秒0~616tm_wday周0~67tm_yday一年内第几天1~3668tm_isdst夏时令-101
tm_sec 范围为 0 ~ 61值 60 表示在闰秒的时间戳中有效并且由于历史原因支持值 61。
localtime() 表示当前时间返回类型为 struct_time 对象示例如下所示
import time
t time.localtime()
print(t--, t)
print(tm_year--, t.tm_year)
print(tm_year--, t[0])输出结果
t-- time.struct_time(tm_year2019, tm_mon12, tm_mday1, tm_hour19, tm_min49, tm_sec54, tm_wday6, tm_yday335, tm_isdst0)
tm_year-- 2019
tm_year-- 20191.2 常用函数
函数常量说明time()返回当前时间的时间戳gmtime([secs])将时间戳转换为格林威治天文时间下的 struct_time可选参数 secs 表示从 epoch 到现在的秒数默认为当前时间localtime([secs])与 gmtime() 相似返回当地时间下的 struct_timemktime(t)localtime() 的反函数asctime([t])接收一个 struct_time 表示的时间返回形式为Mon Dec 2 08:53:47 2019 的字符串ctime([secs])ctime(secs) 相当于 asctime(localtime(secs))strftime(format[, t])格式化日期接收一个 struct_time 表示的时间并返回以可读字符串表示的当地时间sleep(secs)暂停执行调用线程指定的秒数altzone本地 DST 时区的偏移量以 UTC 为单位的秒数timezone本地非 DST时区的偏移量UTC 以西的秒数西欧大部分地区为负美国为正英国为零tzname两个字符串的元组第一个是本地非 DST 时区的名称第二个是本地 DST 时区的名称 epoch1970-01-01 00:00:00 UTC 基本使用如下所示
import timeprint(time.time())
print(time.gmtime())
print(time.localtime())
print(time.asctime(time.localtime()))
print(time.tzname)
# strftime 使用
print(time.strftime(%Y-%m-%d %H:%M:%S, time.localtime()))