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

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

by Bill Lubanovic
March 2022
Intermediate 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.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

可编程网络自动化

可编程网络自动化

Jason Edelman, Scott S. Lowe, Matt Oswalt
Python实用技能学习指南

Python实用技能学习指南

Posts & Telecom Press, Robert Smallshire, Austin Bingham
量子计算机编程:从入门到实践

量子计算机编程:从入门到实践

Eric R. Johnston, Nicholas Harrigan, Mercedes Gimeno-Segovia
Python编程入门与实战

Python编程入门与实战

Posts & Telecom Press, Fabrizio Romano

Publisher Resources

ISBN: 9787115586223