目录
下面把所有的函数的用法,都简单解释一下:
from datetime import datetime,timedelta;
#------------------------------------------------------------------------------
# get current time's timestamp
def getCurTimestamp() :
return datetimeToTimestamp(datetime.now());
#------------------------------------------------------------------------------
# convert datetime value to timestamp
# from "2006-06-01 00:00:00" to 1149091200
def datetimeToTimestamp(datetimeVal) :
return int(time.mktime(datetimeVal.timetuple()));
from datetime import datetime,timedelta;
#------------------------------------------------------------------------------
# convert timestamp to datetime value
# from 1149091200 to "2006-06-01 00:00:00"
def timestampToDatetime(timestamp) :
#print "type(timestamp)=",type(timestamp);
#print "timestamp=",timestamp;
#timestamp = int(timestamp);
timestamp = float(timestamp);
return datetime.fromtimestamp(timestamp);
例 2.2. timestampToDatetime使用范例
createtimeFloat = float(createtimeMillisecond)/1000;
localTime = timestampToDatetime(createtimeFloat);
import time;
#------------------------------------------------------------------------------
#init for calculate elapsed time
def calcTimeStart(uniqueKey) :
global gVal
gVal['calTimeKeyDict'][uniqueKey] = time.time();
return
#------------------------------------------------------------------------------
# to get elapsed time, before call this, should use calcTimeStart to init
def calcTimeEnd(uniqueKey) :
global gVal
return time.time() - gVal['calTimeKeyDict'][uniqueKey];
例 2.3. calcTimeStart和calcTimeEnd的使用范例
calcTimeStart("export_head");
exportHead(blogInfoDic);
gVal['statInfoDict']['exportHeadTime'] = calcTimeEnd("export_head");
from datetime import datetime,timedelta;
#------------------------------------------------------------------------------
# convert local GMT8 to GMT time
# note: input should be 'datetime' type, not 'time' type
def convertLocalToGmt(localTime) :
return localTime - timedelta(hours=8);