
正文
python3 时间和日期
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
Python程序可以通过多种方式来处理日期和时间。日期格式之间的转换是计算机的一个共同核心。Python的时间和日历模块能够帮助我们跟踪的日期和时间。
什么是刻度?
时间间隔以秒为单位的浮点数。特别是在时间瞬间在自1970年1月1日(纪元)12点的表示。
在Python提供一个流行时间 time 模块,提供时间的的 函数功能,并可转换表示。函数 time.time()返回当前系统时间,从1970年1月1日12点起来的秒数。
示例
#!/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
日期计算使用刻度线是很容易做到的。 然而,新纪元日期之前不能以这种形式表示。在未来的一个日期也不能表示这种方式 - 分界点是在某个时候,在 UNIX和Windows上为 2038年。
什么是TimeTuple?
许多Python时间的函数处理时间9个数字的元组,如下图所示 -
| 索引 | 字段 | 值 |
|---|---|---|
| 0 | 4-digit year | 2016 |
| 1 | Month | 1 to 12 |
| 2 | Day | 1 to 31 |
| 3 | Hour | 0 to 23 |
| 4 | Minute | 0 to 59 |
| 5 | Second | 0 to 61 (60 or 61 are leap-seconds) |
| 6 | Day of Week | 0 to 6 (0 is Monday) |
| 7 | Day of year | 1 to 366 (Julian day) |
| 8 | Daylight savings | -1, 0, 1, -1 means library determines DST |
示例
>>> print (time.localtime())
这将产生一个结果如下 -
time.struct_time(tm_year=2016, tm_mon=3, tm_mday=15, tm_hour=9, tm_min=29, tm_sec=2, tm_wday=0, tm_yday=46, tm_isdst=0)
上述元组相当于struct_time结构。这种结构具有以下属性 -
| 索引 | 属性 | 值 |
|---|---|---|
| 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 to 61 (60 or 61 are leap-seconds) |
| 6 | tm_wday | 0 to 6 (0 is Monday) |
| 7 | tm_yday | 1 to 366 (Julian day) |
| 8 | tm_isdst | -1, 0, 1, -1 means library determines DST |
获取当前时间
划时代浮点值从秒时刻转换生成为时间元组,浮点值传递给一个函数返回时间的元组并带上所有九个项目无效(例如,本地时间)。
#!/usr/bin/python3
import timelocaltime = 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 timelocaltime = time.asctime( time.localtime(time.time()) )
print ("Local current time :", localtime)
这将产生以下结果 -
Local current time : Mon Feb 15 09:34:03 2016
获取一个月份的日历
日历模块提供各种各样年度和月度的日历方法。在这里,我们将打印给定月份(2008年1月)的日历 -
#!/usr/bin/python3
import calendarcal = 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提供一个流行时间模块,为时间和表示之间转换提供了相应函数。这里是所有可用方法的列表 -
| SN | 函数及描述 |
|---|---|
| 1 | time.altzone 如果定义则在当地的DST时区偏移,在UTC西部秒数。这是负值,如果当地的MST时区是UTC以东 (在西欧,包括英国).只使用这个,如果白天不为零。 |
| 2 | time.asctime([tupletime]) 接受时间元组,并返回一个可读24个字符的字符串,如 'Tue Dec 11 18:07:14 2008'. |
| 3 | time.clock( ) 返回当前CPU时间为几秒钟的浮点数。要测量计算成本的不同的方法,time.clock()的值比time.time更有用。 |
| 4 | time.ctime([secs]) 类似 asctime(localtime(secs))和不带参数的 ctime()函数 |
| 5 | time.gmtime([secs]) 接受从新纪元用秒表示瞬间和返回用UTC表示时间元组t。注: t.tm_isdst 总是为 0 |
| 6 | time.localtime([secs]) 接受从新纪元用秒表示瞬间和返回本地时间的时间元组t(t.tm_isdst 为 0 或 1, 根据DST是否通过局部规则适用于时刻秒) |
| 7 | time.mktime(tupletime) 接受表示为本地时间的时间元组并返回从新纪元以秒表示瞬间的一个浮点值 |
| 8 | time.sleep(secs) 线程暂停secs秒调用 |
| 9 | time.strftime(fmt[,tupletime]) 接受表示为本地时间的时间元组瞬间,并返回表示由字符串指定 fmt 瞬间的字符串 |
| 10 | time.strptime(str,fmt='%a %b %d %H:%M:%S %Y') 解析 str 按照格式字符串格式化并返回时间元组格式的时间 |
| 11 | time.time( ) 返回当前时刻,从新纪元以来的秒的浮点数 |
| 12 | time.tzset() 通过重置库例程使用时间转换规则。 环境变量TZ指定如何做到这一点 |
让我们通过简单的功能 -
下列是时间模块提供两个重要属性:
| SN | 属性及说明 |
|---|---|
| 1 | time.timezone 属性time.timezone是在本地时区以UTC的秒偏移量(不包括DST) |
| 2 | time.tzname 属性time.tzname是一对区域设置相关的字符串 |
calendar模块日历 calendar 模块提供日历相关的功能,包括函数用来打印给定月份或年份文本日历。
默认情况下,日历采用星期一作为一周的第一天,周日是最后一个。如要改变这种情况,调用calendar.setfirstweekday()函数。
这里是 calendar 模块可用的函数列表:
| SN | 方法及描述 |
|---|---|
| 1 | calendar.calendar(year,w=2,l=1,c=6) 返回一个多行字符串使用格式化成用c空格分隔的三列逐年的日历 |
| 2 | calendar.firstweekday( ) 返回每周工作日开始的当前设置。默认情况下,当日历被首次导入是0,这意味着星期一 |
| 3 | calendar.isleap(year) 如果 year 是闰年返回True;否则为false |
| 4 | calendar.leapdays(y1,y2) 返回跨越润年在范围内(y1,y2)的总数 |
| 5 | calendar.month(year,month,w=2,l=1) 返回一个多行字符串以及逐年月月份的日历,每周一行加上两个标题行。 |
| 6 | calendar.monthcalendar(year,month) 返回列表的列表整数。 |
| 7 | calendar.monthrange(year,month) 返回两个整数。 |
| 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(weekday) 设定每个星期的第一天工作日代码。工作日代码是0(星期一)至6(星期日) |
| 11 | calendar.timegm(tupletime) time.gmtime反转:接受时间元组形式的时刻,并返回同一时刻作为自新纪元以来秒的浮点数 |
| 12 | calendar.weekday(year,month,day) 返回给定日期是星期几代码。 工作日代码是0(星期一)至60(星期日);月数是1(1月)到12(12月) |







