【发布时间】:2014-04-11 09:47:30
【问题描述】:
我编写了一个模块,可以生成特定时间发生的事件的百分比概率。我的数据库有一个生成事件的时间戳。我正在将 y-m-d h-m-s 格式的时间戳转换为纪元时间。我得到的纪元时间是 UTC (GMT) 时间,而我的时区是 GMT + 1。
一个例子如下:
我的第一个数据库条目是在 10:13:36,其纪元时间为 1397211216,当我尝试从我的代码中获取此条目的纪元时间时,返回的纪元时间为 1397207617(09:13: 36).我的代码在下面,我知道如何定义纪元时间存在问题,但我不知道如何更改它以获得正确的时间。
def getPercentageDuringTime(dbName, sensorType, beginningTime, endTime):
count = 0
reading = [i [1]for i in cur.execute("SELECT * FROM " + dbName + " WHERE sensor = '" + sensorType + "' ")]
readingtime = [i [2] for i in cur.execute("SELECT * FROM " + dbName + " WHERE sensor = '" + sensorType + "' ")]
for i in range(len(reading)):
pattern = '%Y-%m-%d %H:%M:%S'
epoch = int(time.mktime(time.strptime(readingtime[i], pattern)))
if ((epoch >= beginningTime) and (epoch <= endTime)):
count = count + 1
percentage = count / (len(reading)) * 100
print (epoch)
return percentage
print (getPercentageDuringTime('event4312593', 'sound', 1397207617, 1397207616))
【问题讨论】:
标签: python datetime time epoch