使用期间,遇到一个情况,才知道存在这两个关于时间的,另外还有一个date类型的对象的。
此处要解释的是两者之间,datetime支持timedelta,而time支持timedelta:
datestr = "2011-12-11 23:21:07" print "datestr=",datestr parsedLocalTime = datetime.strptime(datestr, '%Y-%m-%d %H:%M:%S') # here is GMT+8 local time print "datetime: parsedLocalTime=",parsedLocalTime print "datetime: type(parsedLocalTime)=",type(parsedLocalTime) gmt0Time = parsedLocalTime - timedelta(hours=8) print "datetime: gmt0Time=",gmt0Time parsedLocalTime = time.strptime(datestr, '%Y-%m-%d %H:%M:%S') # here is GMT+8 local time print "time: parsedLocalTime=",parsedLocalTime print "time: type(parsedLocalTime)=",type(parsedLocalTime) #gmt0Time = parsedLocalTime - timedelta(hours=8) # this line will cause error !!! #print "time: gmt0Time=",gmt0Time #-----------following is outpu ----------------- #datestr= 2011-12-11 23:21:07 #datetime: parsedLocalTime= 2011-12-11 23:21:07 #datetime: type(parsedLocalTime)= <type 'datetime.datetime'> #datetime: gmt0Time= 2011-12-11 15:21:07 #time: parsedLocalTime= time.struct_time(tm_year=2011, tm_mon=12, tm_mday=11, tm_hour=23, tm_min=21, tm_sec=7, tm_wday=6, tm_yday=345, tm_isdst=-1) #time: type(parsedLocalTime)= <type 'time.struct_time'>
【参考资料】