python-時間


時間 time

在Python中,與時間處理相關的模塊有:time、datetime以及calendar。

學會計算時間,對程序的調優非常重要,可以在程序中狂打時間戳,來具體判斷程序中哪一塊耗時最多,從而找到程序調優的重心處。

在Python中,通常有這幾種方式表示時間:時間戳、格式化的時間字符串、元組(struct_time 共九種元素)。由於Python的time模塊主要是調用C庫實現的,所以在不同的平台可能會有所不同。

時間戳(timestamp)的方式:時間戳表示是從1970年1月1號 00:00:00開始到現在按秒計算的偏移量。Tick單位(時間間隔以秒為單位的浮點小數)最適於做日期運算。但是1970年之前的日期就無法以此表示了。太遙遠的日期也不行,UNIX和Windows只支持到2038年某日。查看一下type(time.time())的返回值類型,可以看出是float類型。返回時間戳的函數主要有time()、clock()等。

UTC(世界協調時),就是格林威治天文時間,也是世界標准時間。在中國為UTC+8。DST夏令時。

元組方式:struct_time元組共有9個元素,返回struct_time的函數主要有gmtime(),localtime(),strptime()。

時區(Time Zone)是地球上的區域使用同一個時間定義。1884年在華盛頓召開國際經度會議時,為了克服時間上的混亂,規定將全球划分為24個時區。

time.localtime()時間元祖:

>>> import time

>>> time.localtime()

time.struct_time(tm_year=2018, tm_mon=1, tm_mday=30, tm_hour=22, tm_min=41, tm_sec=56, tm_wday=1, tm_yday=30, tm_isdst=0)

 

 

 

 

既然是一個元組,那么就遵循元組的所有特性,比如索引(都從0開始),切片等。下面是元組中各元素的解釋:

 tm_year:年

tm_mon:月(1-12)

tm_mday:日(1-31)

tm_hour:時(0-23)

tm_min:分(0-59)

tm_sec:秒(0-59)

tm_wday:星期幾(0-6,6表示周日)

tm_yday:一年中的第幾天(1-366)

tm_isdst:是否是夏令時(默認為-1)

>>>t=time.localtime()

>>> t

time.struct_time(tm_year=2018, tm_mon=1, tm_mday=30, tm_hour=22, tm_min=47, tm_sec=45, tm_wday=1, tm_yday=30, tm_isdst=0)

>>> dir(t)

['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getslice__', '__gt__', '__hash__', '__init__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'n_fields', 'n_sequence_fields', 'n_unnamed_fields', 'tm_hour', 'tm_isdst', 'tm_mday', 'tm_min', 'tm_mon', 'tm_sec', 'tm_wday', 'tm_yday', 'tm_year']

>>> 

由上可見,localtime()是有一些內置屬性的,類似於string的letters一樣,可以用t.tm_mday來方位tm_mday這個屬性的值

練習定義兩個函數,返回年月日,時分秒

#encoding=utf-8

 

def get_day():

    import time

    time_t=time.localtime()

    return u"現在是%s年%s月%s日" %(time_t.tm_year,time_t.tm_mon,time_t.tm_mday)

print get_day()

 

def get_sec():

    import time

    time_t=time.localtime()

    return u"現在是%s時%s分%s秒" %(time_t.tm_hour,time_t.tm_min,time_t.tm_sec)

print get_sec()

 

c:\Python27\Scripts>python task_test.py

現在是2018130

現在是225746

 

 

Time.localtime()可以用下標取值

>>> import time

>>> time.localtime()

time.struct_time(tm_year=2018, tm_mon=1, tm_mday=31, tm_hour=23, tm_min=9, tm_sec=34, tm_wday=2, tm_yday=31, tm_isdst=0)

>>> time.localtime()[0]

2018

>>> time.localtime()[1]

1

>>> time.localtime()[2]

31

>>> 

 

格林威治時間,英國

>>> time.gmtime()

time.struct_time(tm_year=2018, tm_mon=1, tm_mday=31, tm_hour=15, tm_min=10, tm_sec=0, tm_wday=2, tm_yday=31, tm_isdst=0)

時間戳time.time()

>>> time.time()

1517411441.422

時間戳取整,用int或者用%.f小數點位數

>>> time.time()

1517411441.422

>>> type(time.time())

<type 'float'>

>>> int(time.time())

1517411467

>>> "%.f"%time.time()

'1517411621'

>>> "%.1f"%time.time()

'1517411632.6'

>>> "%.2f"%time.time()

'1517411635.87'

用時間戳通過localtime()生成時間

>>> time.localtime(1517411635.87)

time.struct_time(tm_year=2018, tm_mon=1, tm_mday=31, tm_hour=23, tm_min=13, tm_sec=55, tm_wday=2, tm_yday=31, tm_isdst=0)

 

練習:實現函數轉換時間戳到時間

#encoding=utf-8

 

#encoding=utf-8

import time

 

def get_year_mon_day(timestamp=None):

    if timestamp ==None:

        time_array=time.localtime()

    else:

        try:

            time_array=time.localtime(timestamp)

        except:

            print "time convert error occur!"

            return None

    return u"%s年%s月%s日" % (time_array.tm_year,time_array.tm_mon,time_array.tm_mday)

   

print get_year_mon_day(1517411635.87)

 

print get_year_mon_day()

 

 

def get_hour_min_sec(timestamp=None):

    if timestamp ==None:

        time_array=time.localtime()

    else:

        try:

            time_array=time.localtime(timestamp)

        except:

            print "time convert error occur!"

            return None

 

    return u"%s時%s分%s秒" %(time_array.tm_hour,time_array.tm_min,time_array.tm_sec)

 

print get_hour_min_sec(1517411635.8)

print get_hour_min_sec()

 

c:\Python27\Scripts>python task_test.py

2018131

2018131

231355

232953

 

Time.mktime()將時間元祖轉換到時間戳

>>> time_array=time.localtime()

>>> time_array

time.struct_time(tm_year=2018, tm_mon=1, tm_mday=31, tm_hour=23, tm_min=31, tm_sec=3, tm_wday=2, tm_yday=31, tm_isdst=0)

>>> time.mktime(time_array)

1517412663.0

>>> time.mktime(time_array)

1517412663.0

 

Time.gmtime()將時間戳轉為UTC時間

>>> time.time()

1517412894.745

>>> time.gmtime(1517412894.745)

time.struct_time(tm_year=2018, tm_mon=1, tm_mday=31, tm_hour=15, tm_min=34, tm_sec=54, tm_wday=2, tm_yday=31, tm_isdst=0)

 

Time.sleep()睡眠

Time. clock()CPU的累計時間,不同時間的time.clock()相減計算時間差

>>> import time

>>> time.clock()

19.208307247590035

>>> time.clock()

20.248680169227494

>>> time.clock()

20.712062032827205

 

格式化時間字符串time.strftime,time的常用函數—strftime

返回字符串表示的當地時間

把一個代表時間的元組或者struct_time(如由time.localtime()和time.gmtime()返回)轉化為格式化的時間字符串,格式由參數format決定。如果未指定,將傳入time.localtime()。如果元組中任何一個元素越界,就會拋出ValueError的異常。函數返回的是一個可讀表示的本地時間的字符串。

參數:

format:格式化字符串

t :可選的參數是一個struct_time對象

 

 

>>> strTime=time.strftime("%Y-%m-%d-%H:%M:%S",time.localtime())

>>> strTime

'2018-02-01-20:15:50'

>>> print time.strftime("%Y-%m-%d-%a:%M:%b:%c",time.localtime())

2018-02-01-Thu:17:Feb:02/01/18 20:17:17

>>> print time.strftime("%Y-%m-%d-%a:%M:%b:%c",time.localtime())

2018-02-01-Thu:17:Feb:02/01/18 20:17:17

>>> print time.strftime("%S",time.localtime())

53

>>> print time.strftime("%X",time.localtime())

20:17:57

>>> print time.strftime("%X-%xx",time.localtime())

20:18:02-02/01/18x

>>> print time.strftime("%X-%x",time.localtime())

20:18:08-02/01/18

>>> print time.strftime("%X-%x-%y-%Y-%z-%Z",time.localtime())

20:18:36-02/01/18-18-2018-中國標准時間-中國標准時間

>>> print time.strftime("%X-%x-%y-%Y-%z-%Z-%z-%Z",time.localtime())

20:19:01-02/01/18-18-2018-中國標准時間-中國標准時間-中國標准時間-中國標准時間

>>> 

 

>>> print time.strftime("%Y/%m/%d %H:%M:%S",time.localtime())

2018/02/01 20:20:24

>>> print time.strftime("%Y/%m/%d %H:%M:%S",time.localtime())

2018/02/01 20:20:39

>>> print time.strftime("%Y/%m/%d %H:%M:%S",time.localtime())

2018/02/01 20:20:56

>>> print time.strftime("%Y/%m/%d %H:%M:%S",time.localtime())

2018/02/01 20:21:08

 

練習,在文件模式下中文顯示時間

 

#encoding=utf-8

 

import time

 

 

print time.strftime("%Y%m%d-%H:%M:%S",time.localtime()).decode('utf-8')

print type(time.strftime("%Y%m%d-%H:%M:%S",time.localtime()).decode('utf-8'))

c:\Python27\Scripts>python task_test.py

20180201-20:32:07

<type 'unicode'>

 

time.strptime把具體的時間字符串轉換為時間元祖

將格式字符串轉化成struct_time.

該函數是time.strftime()函數的逆操作。time strptime() 函數根據指定的格式把一個時間字符串解析為時間元組。所以函數返回的是struct_time對象。

參數:

string :時間字符串

format:格式化字符串

>>> stime="2015-08-24 13:01:30"

>>> print time.strptime(stime,"%Y-%m-%d %H:%M:%S")

time.struct_time(tm_year=2015, tm_mon=8, tm_mday=24, tm_hour=13, tm_min=1, tm_sec=30, tm_wday=0, tm_yday=236, tm_isdst=-1)

>>> format_time=time.strptime(stime,"%Y-%m-%d %H:%M:%S")

>>> format_time

time.struct_time(tm_year=2015, tm_mon=8, tm_mday=24, tm_hour=13, tm_min=1, tm_sec=30, tm_wday=0, tm_yday=236, tm_isdst=-1)

>>> for i in format_time:

...     print i

...

2015

8

24

13

1

30

0

236

-1

 

注意在使用strptime()函數將一個指定格式的時間字符串轉化成元組時,參數format的格式必須和string的格式保持一致,如果string中日期間使用“-”分隔,format中也必須使用“-”分隔,時間中使用冒號“:”分隔,后面也必須使用冒號分隔,否則會報格式不匹配的錯誤

>>> time.strptime("2018-02-01 20:42:14","%Y-%m-%d %H:%M:%S")

time.struct_time(tm_year=2018, tm_mon=2, tm_mday=1, tm_hour=20, tm_min=42, tm_sec=14, tm_wday=3, tm_yday=32, tm_isdst=-1)

 

下面因為后面的格式匹配,報錯

>>> time.strptime("2018-02-01 20:42:14","%Y-%m-%d %H:%M:%S")

time.struct_time(tm_year=2018, tm_mon=2, tm_mday=1, tm_hour=20, tm_min=42, tm_sec=14, tm_wday=3, tm_yday=32, tm_isdst=-1)

>>> time.strptime("2018-02-01 20:42:14","%Y-%m-%d %H:%M:%X")

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

  File "C:\Python27\lib\_strptime.py", line 478, in _strptime_time

    return _strptime(data_string, format)[0]

  File "C:\Python27\lib\_strptime.py", line 315, in _strptime

    format_regex = _TimeRE_cache.compile(format)

  File "C:\Python27\lib\_strptime.py", line 269, in compile

    return re_compile(self.pattern(format), IGNORECASE)

  File "C:\Python27\lib\re.py", line 194, in compile

    return _compile(pattern, flags)

  File "C:\Python27\lib\re.py", line 251, in _compile

    raise error, v # invalid expression

sre_constants.error: redefinition of group name 'H' as group 6; was group 4

>>> time.strptime("2018-02-01 20:42:14","%Y-%m-%d %H:%M:%X")

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

  File "C:\Python27\lib\_strptime.py", line 478, in _strptime_time

    return _strptime(data_string, format)[0]

  File "C:\Python27\lib\_strptime.py", line 315, in _strptime

    format_regex = _TimeRE_cache.compile(format)

  File "C:\Python27\lib\_strptime.py", line 269, in compile

    return re_compile(self.pattern(format), IGNORECASE)

  File "C:\Python27\lib\re.py", line 194, in compile

    return _compile(pattern, flags)

  File "C:\Python27\lib\re.py", line 251, in _compile

    raise error, v # invalid expression

sre_constants.error: redefinition of group name 'H' as group 6; was group 4

>>> time.strptime("2018-02-01 20:42:14","%Y-%m-%d %H:%M:%S")

 

>>> time.strptime(stime,"%Y-%m-%d %H:%M:%S")

time.struct_time(tm_year=2018, tm_mon=2, tm_mday=1, tm_hour=20, tm_min=42, tm_sec=14, tm_wday=3, tm_yday=32, tm_isdst=-1)

 

Python datetime模塊詳解

datetime模塊是Python中另一個用於操作時間的內置模塊,是基於time包的一個高級包。相比於time模塊,datetime模塊的接口更直觀、更容易調用。datetime可以理解為datetime兩個部分組成,date是指年月日構成的日期(相當於日歷),time是指時分秒微妙構成的一天24小時中的具體時間(相當於手表)。由此可以將這兩個分開管理(datetime.date類,datetime.time類),也可以將兩者合並在一起(datetime.datetime類)。下面就分別介紹一下datetime模塊中的這幾個類。 引入datetime模塊: import datetime(或 from datetime import * datetime模塊定義了兩個常量: datetime.MINYEAR :最小年份,MINYEAR = 1 datetime.MAXYEAR:最大年份。MAXYEAR = 9999 datetime模塊定義了下面幾個類: datetime.date:日期類。常用的屬性有yearmonthday datetime.time:時間類。常用的屬性有hourminutesecondmicrosecond datetime.timedalta:表示時間間隔,即兩個時間點之間的長度。 datetime.tzinfo:與時區有關的信息。 以上這些類都是datetime模塊中的類,但是需要注意的是這些類中的對象都是不可被改變的。下面來具體講解一下這些類的使用方法。

Datetime.date.today()

>>> print datetime.date.today()

2018-02-01

 

通過時間戳轉換為當前的年月日

>>> import time

>>> import datetime

#獲取當前時間的時間戳

>>> now=time.time()

>>> now

1517489601.47

#將時間戳轉換為date類型的時間

>>> s=datetime.date.fromtimestamp(now)

>>> s

datetime.date(2018, 2, 1)

>>> print s

 

2018-02-01

 

 

 

date的常用函數—date.weekday函數

 

返回weekday中的星期幾,星期一,返回0;星期二,返回1;以此類推。

 

該函數需要一個datetime.date類型的參數。

 

>>> import datetime

 

>>> now=datetime.date.today()

 

>>> print now

 

2018-02-02

 

>>> print datetime.date.weekday(now)

 

4

 

 

 

now.strftime()轉化成指定格式的年月日

 

>>> import datetime

 

>>> now=datetime.datetime.now()

 

>>> print now

 

2018-02-02 21:40:20.250000

 

>>> otherStyleTime=now.strftime("%Y-%m-%d %H:%M:%S")

 

>>> print otherStyleTime

 

2018-02-02 21:40:20

 

 

 

timedelta,基於年月日算時間差時間加上時間間隔

 

>>> from datetime import *

 

>>> today=date.today()#獲取今天的日期

 

>>> print today

 

2018-02-02

 

#在今天的日期上再加上10

 

>>> print today+timedelta(days=10)

 

2018-02-12

 

 

 

#encoding=utf-8

 

 

 

from datetime import *

 

#獲取今天的日期

 

today=date.today()

 

print today

 

#在今天的日期上減去10天

 

 

 

print today-timedelta(days=10)

 

c:\Python27\Scripts>python task_test.py

 

2018-02-02

 

2018-01-23

 

 

 

兩個日期相減算時間差

 

#encoding=utf-8

 

 

 

from datetime import *

 

#獲取今天的日期

 

today=date.today()

 

print today

 

 

 

#替換形成一個新的日期

 

future=today.replace(day=15)

 

print future

 

 

 

#算一個兩日相隔的間隔

 

delta=future-today

 

print delta

 

 

 

#在原日期上添加一個日期間隔

 

print future+delta

 

c:\Python27\Scripts>python task_test.py

 

2018-02-02

 

2018-02-15

 

13 days, 0:00:00

 

2018-02-28

 

 

 

只取相差天數

 

 

 

#encoding=utf-8

 

 

 

from datetime import *

 

#獲取今天的日期

 

today=date.today()

 

print today

 

 

 

#替換形成一個新的日期

 

future=today.replace(day=15)

 

print future

 

 

 

#算一個兩日相隔的間隔

 

delta=future-today

 

print str(delta)

 

print str(delta).split()

 

print str(delta).split()[0]

 

 

 

#在原日期上添加一個日期間隔

 

print future+delta

 

 

 

c:\Python27\Scripts>python task_test.py

 

2018-02-02

 

2018-02-15

 

13 days, 0:00:00

 

['13', 'days,', '0:00:00']

 

13

 

2018-02-28

 

 

 

比較時間大小

 

#encoding=utf-8

 

 

 

from datetime import *

 

#今天的日期

 

now=date.today()

 

print now

 

 

 

#未來的日期

 

tomorrow=now.replace(day=13)

 

print tomorrow

 

#比較兩日期大小

 

print tomorrow > now

 

 

 

time類

 

#coding=utf-8

 

 

 

from datetime import *

 

#tm=time(23,46,10

 

tm=datetime.now()

 

print tm

 

print tm.hour

 

print tm.minute

 

print tm.second

 

print tm.microsecond

 

 

 

c:\Python27\Scripts>python task_test.py

 

2018-02-02 22:25:54.539000

 

22

 

25

 

54

 

539000

 

 

 

>>> from datetime import *

 

>>> tm=datetime.now()

 

>>> print tm

 

2018-02-02 22:27:45.876000

 

>>> print tm.year

 

2018

 

>>> print tm.month

 

2

 

>>> print tm.day

 

2

 

>>> print tm.hour

 

22

 

>>> print tm.second

 

45

 

>>> print tm.minute

 

27

 

>>> print tm.microsecond

 

876000

 

 

 

datetime.now()

 

>>> import time

 

>>> from datetime import *

 

>>> datetime.now

 

<built-in method now of type object at 0x000000006B870340>

 

>>> datetime.now()

 

datetime.datetime(2018, 2, 2, 22, 29, 12, 628000)

 

>>> 

 

replace()時間替換

 

>>> from datetime import *

 

>>> tm=datetime.now()

 

>>> print tm

 

2018-02-02 22:29:55.649000

 

>>> tm1=tm.replace(hour=12,minute=10,second=10)

 

>>> print tm1

 

2018-02-02 12:10:10.649000

 

>>> 

 

tm=time(23, 46, 10)=> 23:46:10用time()可以指定時分秒

 

tm.strftime("%H-%M-%S")strftime()對具體時間進行格式化

 

 

 

>>> from datetime import *

 

>>> tm=time(23,46,10)

 

>>> print tm

 

23:46:10

 

>>> tm1=tm.strftime("%H-%M-%S")

 

>>> print tm1

 

23-46-10

 

>>> 

 

 

 

Datetime.strptime()將格式時間字符串轉換為datetime對象

 

>>> stime=datetime.datetime.strptime("2015-08-27 17:23:43","%Y-%m-%d %H:%M:%S")

 

>>> stime

 

datetime.datetime(2015, 8, 27, 17, 23, 43)

 

>>> type(stime)

 

<type 'datetime.datetime'>

 

>>> stime.day

 

27

 

>>> stime.year

 

2015

 

>>> stime.second

 

43

 

>>> import datetime

 

>>> print datetime.datetime.strptime("2015-08-27 17:23:43","%Y-%m-%d %H:%M:%S")

 

2015-08-27 17:23:43

 

 

 

times.replace(year = 2017, hour = 23)

 

 

 

#coding=utf-8

 

import datetime

 

times=datetime.datetime.today()

 

print times

 

#替換原有時間,獲得新的日期時間

 

tmp=times.replace(year=2017,hour=23)

 

print tmp

 

 

 

c:\Python27\Scripts>python task_test.py

 

2018-02-02 22:45:41.797000

 

2017-02-02 23:45:41.797000

 

 

 

將datetime對象轉換成時間戳

 

 

 

#coding=utf-8

 

 

 

import datetime

 

import time

 

 

 

#獲取當前時間的datetime對象

 

t=datetime.datetime.now()

 

print t

 

#根據當前時間的datetime對象獲取當前時間的時間元祖

 

struct_time=t.timetuple()

 

print struct_time

 

#根據當前時間的時間元祖獲得當前時間的時間戳

 

timeStamp=time.mktime(struct_time)

 

#對時間戳取整

 

timeStamp=int(timeStamp)

 

print timestamp

 

c:\Python27\Scripts>python task_test.py

 

2018-02-02 22:53:53.935000

 

time.struct_time(tm_year=2018, tm_mon=2, tm_mday=2, tm_hour=22, tm_min=53, tm_sec=53, tm_wday=4, tm_yday=33, tm_isdst=-1)

 

1517583233

 

 

 

 

 

Datetime對象通過timetuple()函數轉換到時間元祖

 

>>> datetime.datetime.today()

 

datetime.datetime(2018, 2, 2, 22, 50, 13, 600000)

 

>>> datetime.datetime.today().timetuple()

 

time.struct_time(tm_year=2018, tm_mon=2, tm_mday=2, tm_hour=22, tm_min=50, tm_sec=26, tm_wday=4, tm_yday=33, tm_isdst=-1)

 

>>> print datetime.datetime.today()

 

2018-02-02 22:50:36.262000

 

>>> 

 

求天數差

 

>>> import datetime

 

>>> d1=datetime.datetime(2015,7,5)

 

>>> d2=datetime.datetime(2015,8,26)

 

>>> print d1

 

2015-07-05 00:00:00

 

>>> print d2

 

2015-08-26 00:00:00

 

>>> print d2-d1

 

52 days, 0:00:00

 

>>> print (d2-d1).days

 

52

 

>>> print (d2-d1).seconds

 

0

 

datetime.datetime(2015,7,5)= 2015-07-05 00:00:00

 

 

 

timedelta類

 

>>> import datetime

 

>>> print datetime.timedelta(days=1)

 

1 day, 0:00:00

 

 

 

#coding=utf-8

 

 

 

import datetime

 

import time

 

 

 

#獲取當前的時間

 

now = datetime.datetime.now()

 

print now

 

 

 

#設定一個時間間隔,間隔為1天

 

delta=datetime.timedelta(days=1)

 

newTime=now+delta

 

print newTime

 

 

 

#得到明天的時間

 

print str(newTime)[:-7]

 

 

 

#或者使用格式化函數

 

print newTime.strftime("%Y-%m-%d %H:%M:%S")

 

 

 

#原時間減一天

 

delta=datetime.timedelta(days=-1)

 

newTime=now+delta

 

print newTime

 

 

 

c:\Python27\Scripts>python task_test.py

 

2018-02-02 23:09:27.367000

 

2018-02-03 23:09:27.367000

 

2018-02-03 23:09:27

 

2018-02-03 23:09:27

 

2018-02-01 23:09:27.367000

 

 

 

 

 

算100天前的日期

 

#coding=utf-8

 

from datetime import datetime

 

from datetime import timedelta

 

 

 

now = datetime.now()

 

#設置100天前的時間間隔

 

delta = timedelta(days = -100)

 

#得到100天前的時間

 

oldTime = now + delta

 

print oldTime.strftime("%Y-%m-%d")

 

c:\Python27\Scripts>python task_test.py

 

2017-10-25

 

 

 

按小時算小時的時間差

 

#coding=utf-8

 

import datetime

 

 

 

threeHoursAgo = datetime.datetime.now() - datetime.timedelta(hours = 3)

 

print datetime.datetime.now()

 

print str(threeHoursAgo)[:-7]

 

c:\Python27\Scripts>python task_test.py

 

2018-02-02 23:14:45.110000

 

2018-02-02 20:14:45

 

 

 

 

 

計算給定時間間隔的總秒數

 

#coding=utf-8

 

import datetime

 

#計算給定時間間隔的總秒數

 

seconds = datetime.timedelta(hours=1, seconds=30).total_seconds()

 

print seconds

 

c:\Python27\Scripts>python task_test.py

 

3630.0

 

 

 

 

 

calendar日歷

 

calendar.isleap(2015) 判斷是否是閏年

 

import calendar

 

#判斷2015年是否是閏年

 

if calendar.isleap(2015) is True :

 

  print u"2015年是閏年"

 

else :

 

  print u"2015年不是閏年"

 

 

 

c:\Python27\Scripts>python task_test.py

 

2015年不是閏年

 

打印日歷

 

 

 

#coding=utf-8

 

 

 

import calendar

 

print calendar.month(2015, 3, 1, 1)

 

c:\Python27\Scripts>python task_test.py

 

     March 2015

 

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

 

 

 

年歷

 

#coding=utf-8

 

 

 

import calendar

 

print calendar.calendar(2015, 3, 1, 1)

 

 

 

 

 

c:\Python27\Scripts>python task_test.py

 

                                         2015

 

 

 

          January                      February                      March

 

Mon Tue Wed Thu Fri Sat Sun  Mon Tue Wed Thu Fri Sat Sun  Mon Tue Wed Thu Fri Sat Sun

 

              1   2   3   4                            1                            1

 

  5   6   7   8   9  10  11    2   3   4   5   6   7   8    2   3   4   5   6   7   8

 

 12  13  14  15  16  17  18    9  10  11  12  13  14  15    9  10  11  12  13  14  15

 

 19  20  21  22  23  24  25   16  17  18  19  20  21  22   16  17  18  19  20  21  22

 

 26  27  28  29  30  31       23  24  25  26  27  28       23  24  25  26  27  28  29

 

                                                           30  31

 

 

 

           April                         May                          June

 

Mon Tue Wed Thu Fri Sat Sun  Mon Tue Wed Thu Fri Sat Sun  Mon Tue Wed Thu Fri Sat Sun

 

          1   2   3   4   5                    1   2   3    1   2   3   4   5   6   7

 

  6   7   8   9  10  11  12    4   5   6   7   8   9  10    8   9  10  11  12  13  14

 

 13  14  15  16  17  18  19   11  12  13  14  15  16  17   15  16  17  18  19  20  21

 

 20  21  22  23  24  25  26   18  19  20  21  22  23  24   22  23  24  25  26  27  28

 

 27  28  29  30               25  26  27  28  29  30  31   29  30

 

 

 

            July                        August                     September

 

Mon Tue Wed Thu Fri Sat Sun  Mon Tue Wed Thu Fri Sat Sun  Mon Tue Wed Thu Fri Sat Sun

 

          1   2   3   4   5                        1   2        1   2   3   4   5   6

 

  6   7   8   9  10  11  12    3   4   5   6   7   8   9    7   8   9  10  11  12  13

 

 13  14  15  16  17  18  19   10  11  12  13  14  15  16   14  15  16  17  18  19  20

 

 20  21  22  23  24  25  26   17  18  19  20  21  22  23   21  22  23  24  25  26  27

 

 27  28  29  30  31           24  25  26  27  28  29  30   28  29  30

 

                              31

 

 

 

          October                      November                     December

 

Mon Tue Wed Thu Fri Sat Sun  Mon Tue Wed Thu Fri Sat Sun  Mon Tue Wed Thu Fri Sat Sun

 

              1   2   3   4                            1        1   2   3   4   5   6

 

  5   6   7   8   9  10  11    2   3   4   5   6   7   8    7   8   9  10  11  12  13

 

 12  13  14  15  16  17  18    9  10  11  12  13  14  15   14  15  16  17  18  19  20

 

 19  20  21  22  23  24  25   16  17  18  19  20  21  22   21  22  23  24  25  26  27

 

 26  27  28  29  30  31       23  24  25  26  27  28  29   28  29  30  31

 

                              30

 

Html calendar

 

生成html代碼

 

import calendar
myCal = calendar.HTMLCalendar(calendar.SUNDAY)
print myCal.formatmonth(2016, 7)
with open('calendar.html','w') as fp:
    fp.write(myCal.formatmonth(2016, 7))

 

然后放到html文件里

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM