Python3日期和时间


Python程序可以通过多种方式处理日期和时间。在日期格式之间进行转换是计算机的常见任务。 Python的时间和日历模块有助于跟踪日期和时间。

什么是T


时间间隔是浮点数,以秒为单位。时间的特定时刻以自1970年1月1日上午12:00(纪元)以来的秒数表示。

有一个受欢迎的 time Python中提供的模块,该模块提供用于处理时间以及在表示形式之间进行转换的功能。功能 time.time() 返回自1970年1月1日上午12:00(纪元)以来的当前系统时间(以滴答为单位)。

#!/usr/bin/python3
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: 1455508609.34375

Date arithmetic is easy to do with ticks. However, dates before the epoch cannot be represented in this form. Dates in the far future also cannot be represented this way - the cutoff point is sometime in 2038 for UNIX and Windows.

什么是TimeTuple?


Python的许多时间函数都将时间作为9个数字的元组来处理,如下所示:

Index Field Values
0 4位数的年份 2016
1 Month 1 to 12
2 Day 1 to 31
3 Hour 0 to 23
4 Minute 0 to 59
5 Second 0到61(60或61是leap秒)
6 星期几 0到6(0是星期一)
7 一年中的一天 1至366(儒略日)
8 夏令时 -1,0,1,-1表示库确定DST

例如:

import time

print (time.localtime());

这将产生如下结果:

time.struct_time(tm_year = 2016, tm_mon = 2, tm_mday = 15, tm_hour = 9, 
    tm_min = 29, tm_sec = 2, tm_wday = 0, tm_yday = 46, tm_isdst = 0)

上面的元组等效于 struct_time 结构体。该结构具有以下属性:

Index 属性 Values
0 tm_year 2016
1 tm_mon 1 to 12
2 tm_mday 1 to 31
3 tm_hour 0 to 23
4 tm_min 0 to 59
5 tm_sec 0到61(60或61是leap秒)
6 tm_wday 0到6(0是星期一)
7 tm_yday 1至366(儒略日)
8 tm_isdst -1,0,1,-1表示库确定DST

获取当前时间


要将纪元浮点值以来的秒数转换为时间元组,请将浮点值传递给一个函数(例如localtime),该函数将返回一个包含所有有效九个项目的时间元组。

#!/usr/bin/python3
import time

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

这将产生以下结果,可以将其格式化为任何其他可表示的形式:

Local current time : time.struct_time(tm_year = 2016, tm_mon = 2, tm_mday = 15, 
    tm_hour = 9, tm_min = 29, tm_sec = 2, tm_wday = 0, tm_yday = 46, tm_isdst = 0)

获取格式化时间


你可以根据需要设置任何时间格式,但是获取时间格式的一种简单方法是 asctime()

#!/usr/bin/python3
import time

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

这将产生以下结果:

Local current time : Mon Feb 15 09asctime()03 2016

获取一个月的日历


日历模块提供了多种使用年度和每月日历的方法。在这里,我们打印给定月份(2008年1月)的日历:

#!/usr/bin/python3
import calendar

cal = calendar.month(2016, 2)
print ("Here is the calendar:")
print (cal)

这将产生以下结果:

Here is the calendar:
    February 2016
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

时间模块


有一个受欢迎的 time Python中提供的模块,该模块提供用于处理时间以及在制图表达之间进行转换的功能。这是所有可用方法的列表。

序号 功能说明
1 时区

如果已定义,则本地DST时区的偏移量(以UTC西为秒)。如果当地DST时区位于UTC以东(例如在西欧,包括英国),则为负。如果日光不为零,请使用此选项。

2 time.asctime([tupletime])

接受时间元组并返回可读的24个字符的字符串,例如'Tue Dec 11 18:07:14 2008'。

3 time.clock()

返回当前CPU时间,以秒为单位的浮点数。为了衡量不同方法的计算成本,time.clock的值比time.time()的值更有用。

4 time.ctime([秒])

像asctime(localtime(secs))一样,没有参数,就像asctime()

5 time.gmtime([秒])

接受从纪元以来以秒表示的瞬间,并返回带有UTC时间的时间元组t。注意:t.tm_isdst始终为0

6 time.localtime([秒])

接受从纪元以来以秒表示的瞬间,并返回带有本地时间的时间元组t(t.tm_isdst为0或1,具体取决于DST是否适用于本地规则中的瞬间秒)。

7 time.mktime(元组时间)

接受以当地时间的时间元组表示的瞬时值,并返回一个浮点值,该瞬时值以自该纪元以来的秒数表示。

8 time.sleep(secs)

将调用线程挂起数秒。

9 time.strftime(fmt [,tupletime])

接受表示为本地时间中的时间元组的瞬间,并返回表示由字符串fmt指定的瞬间的字符串。

10 time.strptime(str,fmt ='%a%b%d%H:%M:%S%Y')

根据格式字符串fmt解析str并以时间元组格式返回瞬时值。

11 time.time()

返回当前时刻,即自该纪元以来的秒数的浮点数。

12 time.tzset()

重置库例程使用的时间转换规则。环境变量TZ指定如何完成此操作。

时间模块有两个重要的属性。他们是:

序号 属性和说明
1

时区

属性time.timezone是本地时区相对于UTC的偏移量(不带DST),以秒为单位(美洲> 0;欧洲,亚洲,非洲大多数国家<= 0)。

2

time.tzname

属性time.tzname是一对与语言环境相关的字符串,分别是不带DST和带DST的本地时区的名称。

日历模块


日历模块提供了与日历相关的功能,包括用于打印给定月份或年份的文本日历的功能。

默认情况下,日历将星期一作为一周的第一天,将星期日作为最后一天。要更改此设置,请致电 calendar.setfirstweekday() 功能。

这是可使用的功能的列表 calendar module:

序号 功能说明
1

calendar.calendar(year,w = 2,l = 1,c = 6)

返回带有年份日历的多行字符串,该日历的年格式为三列,用c空格分隔。 w是每个日期的字符宽度;每行的长度为21 * w + 18 + 2 * c。 l是每周的行数。

2

calendar.firstweekday()

返回每周开始的工作日的当前设置。默认情况下,首次导入日历时,该值为0,表示星期一。

3

calendar.isleap(年)

如果year是a年,则返回True;否则返回True。否则为False。

4

calendar.leapdays(y1,y2)

返回范围(y1,y2)内的年份中的leap日总数。

5

calendar.month(year,month,w = 2,l = 1)

返回一个多行字符串,其中包含一年中月份月份的日历,每周一行,外加两行标题行。 w是每个日期的字符宽度;每行的长度为7 * w + 6。 l是每周的行数。

6

calendar.monthcalendar(年,月)

返回整数列表的列表。每个子列表表示一个星期。一年中月份月份中的天数设置为0;每月中的天数设置为其每月的第1天及以后。

7

calendar.monthrange(年,月)

返回两个整数。第一个是年份月份月份的第一天的工作日代码;第二个是一个月中的天数。工作日代码为0(星期一)至6(星期日);月数是1到12。

8

calendar.prcal(year,w = 2,l = 1,c = 6)

类似于打印calendar.calendar(year,w,l,c)。

9

calendar.prmonth(year,month,w = 2,l = 1)

像打印calendar.month(year,month,w,l)。

10

calendar.setfirstweekday(工作日)

将每周的第一天设置为工作日代码工作日。工作日代码为0(星期一)至6(星期日)。

11

calendar.timegm(tupletime)

time.gmtime的逆数:接受以时间元组形式的时刻,并返回与该时刻相距的浮点秒数相同的时刻。

12

calendar.weekday(年,月,日)

返回给定日期的工作日代码。工作日代码为0(星期一)至6(星期日);月份为1月(1月)至12月(12月)。

其他模块和功能


如果你有兴趣,则可以在这里找到其他重要模块和函数的列表,这些模块和函数可以在Python中与日期和时间一起使用:

  • 日期时间模块
  • pytz模块
  • dateutil模块