Skip to Content
Python语言及其应用(第2版)
book

Python语言及其应用(第2版)

by Bill Lubanovic
March 2022
Intermediate to advanced content levelIntermediate to advanced
522 pages
13h 52m
Chinese
Posts & Telecom Press
Content preview from Python语言及其应用(第2版)
日历和时钟
215
13.3
使用
time
模块
令人困惑的是,
Python
有一个带有
time
对象的
datetime
模块,以及一个单独的
time
块。此外,
time
模块还有一个函数叫作
time()
表示绝对时间的一种方法是计算从某个时间起点开始的秒数。
Unix
使用自
1970
1
1
0
点开始的秒数。
2
这个值通常称为
纪元
epoch
),它是在系统之间交换日期和时间
的最简单方法。
time
模块的
time()
函数能够以纪元值的形式返回当前时间:
>>> import time
>>> now = time.time()
>>> now
1554512132.778233
1970
年新年以来,已经过去了
10
亿秒。那现在是什么时间?
可以使用
ctime()
将纪元值转换为字符串:
>>> time.ctime(now)
'Fri Apr 5 19:55:32 2019'
下一节会介绍如何生成更悦目的日期和时间格式。
纪元值是与不同系统(如
JavaScript
)交换日期和时间时的最小公分母
。但是有时候,你
需要的是实际的天数、小时数等,
time
struct_time
对象的形式给出了这些信息。
localtime()
提供了系统时区时间,而
gmtime()
提供了
UTC
(协调世界时以前称为
格林
尼治时间
祖鲁时间
):
>>> time.localtime(now)
time.struct_time(tm_year=2019, tm_mon=4, tm_mday=5, tm_hour=19,
tm_min=55, ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Python编程入门与实战

Python编程入门与实战

Posts & Telecom Press, Fabrizio Romano
Python实用技能学习指南

Python实用技能学习指南

Posts & Telecom Press, Robert Smallshire, Austin Bingham
Python技术基础视频教程

Python技术基础视频教程

保罗·J·戴特尔
Python面向对象编程指南

Python面向对象编程指南

Posts & Telecom Press, Steven F. Lott

Publisher Resources

ISBN: 9787115586223