作用:管理时钟时间的函数
Python版本:1.4及以后版本
time模块提供了一些用于管理日期和时间的C库函数.
time模块的核心函数之一是time(),它会把纪元开始以来的秒数作为一个浮点值返回.
>>> import time >>> time.time() 1425368942.161浮点数用于存储或比较日期很有用,但是ctime更适合记录或打印时间:
>>> time.ctime() 'Tue Mar 03 15:50:15 2015' >>> time.ctime(time.time() + 15) 'Tue Mar 03 15:51:03 2015'
clock()返回处理器时钟时间,通常用于性能测试,基准测试等.而如果程序什么都没有做(如调用sleep函数),则clock则不会增加时间:
备注:测试的结果却有影响(书上可能有误!)
import time for i in range(5): #a = range(10000) print '%s %0.2f %0.2f' % (time.ctime(), time.time(), time.clock()) time.sleep(2) #sleep 2s解释器显示如下:
>>> Tue Mar 03 15:59:30 2015 1425369570.85 0.00 Tue Mar 03 15:59:32 2015 1425369572.85 2.00 Tue Mar 03 15:59:34 2015 1425369574.85 4.00 Tue Mar 03 15:59:36 2015 1425369576.85 6.00 Tue Mar 03 15:59:38 2015 1425369578.86 8.02
有时候我们需要用到具体的时间如:年,月,日,时,分,秒等.time模块定义了struct_time来维护日期和时间值:
import time def show_struct(s): print 'tm_year:', s.tm_year print 'tm_mon:', s.tm_mon print 'tm_mday:', s.tm_mday print 'tm_hour:', s.tm_hour print 'tm_min:', s.tm_min print 'tm_sec:', s.tm_sec print 'tm_wday:', s.tm_wday print 'tm_yday:', s.tm_yday print 'tm_isdst:', s.tm_isdst print 'gmtime:' show_struct(time.gmtime()) print '\nlocaltime:' show_struct(time.localtime()) print '\nmktime:', time.mktime(time.localtime())解释器显示如下:
>>> gmtime: tm_year: 2015 tm_mon: 3 tm_mday: 3 tm_hour: 8 tm_min: 4 tm_sec: 54 tm_wday: 1 tm_yday: 62 tm_isdst: 0 localtime: tm_year: 2015 tm_mon: 3 tm_mday: 3 tm_hour: 16 tm_min: 4 tm_sec: 54 tm_wday: 1 tm_yday: 62 tm_isdst: 0 mktime: 1425369894.0
要改变时区,需要设置环境变量TZ,然后调用tzset().
备注:在Python2.7中time模块无tzset()方法!
我们可以使用strptime()和strftime()在struct_time和时间值字符串表示之间进行转换:
import time def show_struct(s): print 'tm_year:', s.tm_year print 'tm_mon:', s.tm_mon print 'tm_mday:', s.tm_mday print 'tm_hour:', s.tm_hour print 'tm_min:', s.tm_min print 'tm_sec:', s.tm_sec print 'tm_wday:', s.tm_wday print 'tm_yday:', s.tm_yday print 'tm_isdst:', s.tm_isdst now = time.ctime() print 'Now:', now parsed = time.strptime(now) print '\nParsed:' show_struct(parsed) print '\nFormatted:', time.strftime("%a %b %d %H:%M:%S %Y", parsed)解释器显示如下:
>>> Now: Tue Mar 03 16:16:42 2015 Parsed: tm_year: 2015 tm_mon: 3 tm_mday: 3 tm_hour: 16 tm_min: 16 tm_sec: 42 tm_wday: 1 tm_yday: 62 tm_isdst: -1 Formatted: Tue Mar 03 16:16:42 2015
作用:datetime模块包含一些函数和类,用于完成日期和时间解析,格式化和算术运算
Python版本:2.3及以后版本
时间值用time类表示.time实例包含hour,minute,second和microsecond属性,还可以包含时区信息:
>>> import datetime >>> t = datetime.time(1, 2, 3) >>> t.hour 1 >>> t.minute 2 >>> t.second 3 >>> t.microsecond 0 >>> t.tzinfo >>> datetime.time.min datetime.time(0, 0) >>> datetime.time.max datetime.time(23, 59, 59, 999999) >>> datetime.time.resolution datetime.timedelta(0, 0, 1)
日历日期值用date类表示.date实例包含year,month和day属性.使用today()类方法很容易创建一个表示当前日期的日期实例:
import datetime today = datetime.date.today() print today print 'ctime:', today.ctime() tt = today.timetuple() print 'tm_year:', tt.tm_year print 'tm_mon:', tt.tm_mon print 'tm_mday:', tt.tm_mday print 'tm_hour:', tt.tm_hour print 'tm_min:', tt.tm_min print 'tm_sec:', tt.tm_sec print 'tm_wday:', tt.tm_wday print 'tm_yday:', tt.tm_yday print 'tm_isdst:', tt.tm_isdst print 'ordinal:', today.toordinal() print 'Year:', today.year print 'Mon:', today.month print 'Day:', today.day解释器显示如下:
>>> 2015-03-04 ctime: Wed Mar 4 00:00:00 2015 tm_year: 2015 tm_mon: 3 tm_mday: 4 tm_hour: 0 tm_min: 0 tm_sec: 0 tm_wday: 2 tm_yday: 63 tm_isdst: -1 ordinal: 735661 Year: 2015 Mon: 3 Day: 4我们可以使用min和max属性确定所支持的日期值范围:
import datetime print 'Earliest :', datetime.date.min print 'Latest :', datetime.date.max print 'Resolution:', datetime.date.resolution解释器显示如下:
>>> Earliest : 0001-01-01 Latest : 9999-12-31 Resolution: 1 day, 0:00:00创建新的date实例还有一种方法,可以使用一个现有date的replace()方法创建:
import datetime d1 = datetime.date(2008, 3, 29) print 'd1:', d1.ctime() d2 = d1.replace(year = 2009) print 'd2:', d2.ctime()解释器显示如下:
>>> d1: Sat Mar 29 00:00:00 2008 d2: Sun Mar 29 00:00:00 2009
通过对两个datetime对象使用算术运算,或者结合使用一个datetime和一个timedelta,可以计算出将来和过去的一些日期.
import datetime for delta in [datetime.timedelta(microseconds=1), datetime.timedelta(milliseconds=1), datetime.timedelta(seconds=1), datetime.timedelta(minutes=1), datetime.timedelta(hours=1), datetime.timedelta(days=1), datetime.timedelta(weeks=1),]: print '%15s = %s seconds' % (delta, delta.total_seconds())解释器显示如下:
>>> 0:00:00.000001 = 1e-06 seconds 0:00:00.001000 = 0.001 seconds 0:00:01 = 1.0 seconds 0:01:00 = 60.0 seconds 1:00:00 = 3600.0 seconds 1 day, 0:00:00 = 86400.0 seconds 7 days, 0:00:00 = 604800.0 seconds
日期算术元素按使用标准算术操作符来完成:
import datetime today = datetime.date.today() print 'Today :', today one_day = datetime.timedelta(days=1) print 'one day:', one_day yesterday = today - one_day print 'Yesterday:', yesterday tomorrow = today + one_day print 'tomorrow:', tomorrow print print 'tomorrow - yesterday:', tomorrow - yesterday print 'yesterday - tomorrow:', yesterday - tomorrow解释器显示如下:
>>> Today : 2015-03-04 one day: 1 day, 0:00:00 Yesterday: 2015-03-03 tomorrow: 2015-03-05 tomorrow - yesterday: 2 days, 0:00:00 yesterday - tomorrow: -2 days, 0:00:00
日期和时间值都可以使用标准比较操作符来比较,确定哪一个在前或在后:
>>> import datetime >>> import time >>> t1 = datetime.time(12, 55, 0) >>> t2 = datetime.time(13, 5, 0) >>> t1 < t2 True >>> d1 = datetime.date.today() >>> d2 = datetime.date.today() + datetime.timedelta(days=1) >>> d1, d2 (datetime.date(2015, 3, 4), datetime.date(2015, 3, 5)) >>> d1 < d2 True
使用datetime类可以存储由日期和时间分量构成的值.
import datetime print 'Now:', datetime.datetime.now() print 'Today:', datetime.datetime.today() print 'UTC Now:', datetime.datetime.utcnow() print FIELDS = ['year', 'month', 'day', 'hour', 'minute', 'second', 'microsecond', ] d = datetime.datetime.now() for attr in FIELDS: print '%15s: %s' % (attr, getattr(d, attr))解释器显示如下:
>>> Now: 2015-03-04 09:43:17.765000 Today: 2015-03-04 09:43:17.796000 UTC Now: 2015-03-04 01:43:17.812000 year: 2015 month: 3 day: 4 hour: 9 minute: 43 second: 17 microsecond: 812000
可以使用strftime()格式化时间:
import datetime format = '%a %b %d %H:%M:%S %Y' today = datetime.datetime.today() print 'ISO :', today s = today.strftime(format) print 'strftime:', s d = datetime.datetime.strptime(s, format) print 'strptime:', d.strftime(format)解释器显示如下:
>>> ISO : 2015-03-04 09:47:22.214000 strftime: Wed Mar 04 09:47:22 2015 strptime: Wed Mar 04 09:47:22 2015
作用:calendar模块实现了一些类来处理日期,管理面向年,月和周的值
prmonth()方法是一个简单的函数,可以生成一个月的格式化文本输出.
>>> import calendar >>> c = calendar.TextCalendar(calendar.SUNDAY) >>> c.prmonth(2015, 3) March 2015 Su Mo Tu We Th Fr Sa 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