在Python中处理日期和时间的基本知识点整理汇总

(编辑:jimmy 日期: 2024/10/3 浏览:2)

 Python程序可以处理多种方式的日期和时间。日期格式之间的转换是一种常见计算机的杂活。 Python的时间和日历模块,能帮助处理日期和时间。
Tick是什么"htmlcode">

#!/usr/bin/python
import time; # This is required to include time module.

ticks = time.time()
print "Number of ticks since 12:00am, January 1, 1970:", ticks

这将产生一个结果如下:

Number of ticks since 12:00am, January 1, 1970: 7186862.73399

日期计算是很容易。不过当日的时代之前,不能以这种形式来表示。在遥远的将来的日期也不能代表这种方式- 分界点是一段2038年在UNIX和Windows。
什么是TimeTuple?

Python的时间函数处理时间为9个数字的元组,如下图所示:

在Python中处理日期和时间的基本知识点整理汇总

上面的元组相当于struct_time结构。这种结构具有以下属性:

在Python中处理日期和时间的基本知识点整理汇总

 获取当前时间 :

转换一个时刻从秒epoch浮点值转换成时元组,浮点值传递给函数(例如,本地时间)返回时间元组的全部九项有效。

#!/usr/bin/python
import time;

localtime = time.localtime(time.time())
print "Local current time :", localtime

这将产生下面的结果,这可以在任何其他像样形式被格式化:

Local current time : time.struct_time(tm_year=2013, tm_mon=7, 
tm_mday=17, tm_hour=21, tm_min=26, tm_sec=3, tm_wday=2, tm_yday=198, tm_isdst=0)

获取格式化的时间 :

可以随时根据要求格式化,但简单的方法来获取时间,可读的格式是asctime():

#!/usr/bin/python
import time;

localtime = time.asctime( time.localtime(time.time()) )
print "Local current time :", localtime

这将产生以下结果:

Local current time : Tue Jan 13 10:17:09 2009

获取日历月份:

日历模块提供了广泛的方法,如有年和月的日历。在这里,我们打印日历给定月份(2015年1月):

#!/usr/bin/python
import calendar

cal = calendar.month(2015, 1)
print "Here is the calendar:"
print cal;

这将产生以下结果:

Here is the calendar:
  January 2008
Mo Tu We Th Fr Sa Su
  1 2 3 4 5 6
 7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31

高通与谷歌联手!首款骁龙PC优化Chrome浏览器发布
高通和谷歌日前宣布,推出首次面向搭载骁龙的Windows PC的优化版Chrome浏览器。
在对骁龙X Elite参考设计的初步测试中,全新的Chrome浏览器在Speedometer 2.1基准测试中实现了显著的性能提升。
预计在2024年年中之前,搭载骁龙X Elite计算平台的PC将面世。该浏览器的提前问世,有助于骁龙PC问世就获得满血表现。
谷歌高级副总裁Hiroshi Lockheimer表示,此次与高通的合作将有助于确保Chrome用户在当前ARM兼容的PC上获得最佳的浏览体验。