标签:esc reference 时间戳 orm oca option fun images asc
time模块:
Timestamp:计算机中时间是由数字表示的。将1970.1.1 00:00:00 UTC+00:00时区的时刻称为epoch time,记为0(1970年以前的timestamp为负数),当前的时间就是相对 于epoch time的秒数,称为timestamp。
In [109]: time.time() Out[109]: 1510054096.087551
struct time:time.
gmtime
([secs]) 返回UTC时间,dst=0,secs默认为当前时间戳
time.
localtime
([secs])返回本地时间UTC+8,dst=0,secs默认为当前时间戳
In [108]: time.gmtime()
Out[108]: time.struct_time(tm_year=2017, tm_mon=11, tm_mday=7, tm_hour=11, tm_min=26, tm_sec=3, tm_wday=1, tm_yday=311, tm_isdst=0)
In [107]: time.localtime() Out[107]: time.struct_time(tm_year=2017, tm_mon=11, tm_mday=7, tm_hour=19, tm_min=25, tm_sec=52, tm_wday=1, tm_yday=311, tm_isdst=0)
mktime(tuple) -> floating point number
In [113]: t=time.localtime() In [115]: time.mktime(t) Out[115]: 1510054321.0
time.
ctime
([secs])返回本地时间,secs默认为time.time()
time.
asctime
([t])将表示由gmtime()
或localtime()
返回的时间的元组或struct_time
转换为以下形式字符串 。若未提供t,则使用localtime()
返回的当前时间。
In [104]: time.ctime() Out[104]: ‘Tue Nov 7 19:08:51 2017‘ In [105]: time.asctime() Out[105]: ‘Tue Nov 7 19:08:56 2017‘
time.
strftime
(format[, t])将gmtime()
或localtime()
返回的时间元组或struct_time
转为由格式指定的字符串参数。若未提供t,用localtime()
返回的当前时间。格式必须是字符串。
time.
strptime
(string[, format])根据格式解析表示时间的字符串。返回值是由gmtime()
或localtime()
返回的struct_time
。
In [116]: time.strftime(‘%Y-%m-%d‘) Out[116]: ‘2017-11-07‘ In [117]: time.strptime(‘2017-11-07‘,‘%Y-%m-%d‘) Out[117]: time.struct_time(tm_year=2017, tm_mon=11, tm_mday=7, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=1, tm_yday=311, tm_isdst=-1)
time.
sleep
(secs)延迟
附表:
datetime模块:
获取当前日期及时间:
In [118]: from datetime import datetime In [119]: now = datetime.now() In [121]: print(now) 2017-11-07 20:06:04.360664
In [123]: print(type(now))
<class ‘datetime.datetime‘>
获取指定日期及时间:
In [124]: dt = datetime(2017,11,4,7,20,30) In [125]: print(dt) 2017-11-04 07:20:30
datetime-->timestamp:
In [126]: dt.timestamp() Out[126]: 1509751230.0
timestamp-->datetime:
In [127]: t=1490001234.0 In [128]: print(datetime.fromtimestamp(t)) #本地时间 2017-03-20 17:13:54 In [129]: print(datetime.utcfromtimestamp(t)) #UTC时间 2017-03-20 09:13:54
str-->datetime:
In [130]: cday = datetime.strptime(‘2017-6-4 15:30:45‘,‘%Y-%m-%d %H:%M:%S‘) In [131]: print(cday,type(cday)) 2017-06-04 15:30:45 <class ‘datetime.datetime‘>
datetime-->str:
In [132]: print(now.strftime(‘%a, %b %d %H:%M‘)) Tue, Nov 07 20:06
datetime加减:
In [133]: from datetime import timedelta In [134]: now = datetime.now() In [135]: now Out[135]: datetime.datetime(2017, 11, 7, 20, 36, 8, 36987) In [136]: now + timedelta(hours=10) Out[136]: datetime.datetime(2017, 11, 8, 6, 36, 8, 36987) In [137]: now + timedelta(days=1) Out[137]: datetime.datetime(2017, 11, 8, 20, 36, 8, 36987) In [138]: now - timedelta(days=3,hours=14) Out[138]: datetime.datetime(2017, 11, 4, 6, 36, 8, 36987)
标签:esc reference 时间戳 orm oca option fun images asc
原文地址:http://www.cnblogs.com/lybpy/p/7800984.html