close

Reference

  • Introduction to datetime object

http://www.seehuhn.de/pages/pdate

  • 官方文件 - time module

https://docs.python.org/2/library/time.html

 

Convert time structure to user-defined string format

>> t = time.localtime(time.time())

>> s = time.strftime("%y-%m-%d", t)

>> s

'14-10-06'

 

Modify time structure

>> t = time.localtime(time.time())

>> L = list(t)

>> L[0] = 2016

>> tm = time.struct_time(L)

>> tm

time.struct_time(tm_year=2016, tm_mon=10, tm_mday=6, tm_hour=17, tm_min=50, tm_sec=11, tm_wday=0, tm_yday=279, tm_isdst=0)

 

We should convert the time structure to list because we can not modify the time structure directly.

 

Convert time structure to seconds 

>> t = time.localtime(time.time())

>> x = time.mktime(t)

>> x

1412589011.0

 

Get time structure

>> t = time.localtime(time.time())

>> x

time.struct_time(tm_year=2014, tm_mon=10, tm_mday=6, tm_hour=17, tm_min=49, tm_sec=52, tm_wday=0, tm_yday=279, tm_isdst=0)

 

time structure is like a list , elements of time structure are as follow:

IndexAttributeValues
0 tm_year (for example, 1993)
1 tm_mon range [1, 12]
2 tm_mday range [1, 31]
3 tm_hour range [0, 23]
4 tm_min range [0, 59]
5 tm_sec range [0, 61]; see (2) in strftime()description
6 tm_wday range [0, 6], Monday is 0
7 tm_yday range [1, 366]
8 tm_isdst 0, 1 or -1; see below

 

Get current time

>> time.time()

1412588975.540302

 

 

arrow
arrow
    全站熱搜

    JerryCheng 發表在 痞客邦 留言(0) 人氣()