在python错误中舍入时间

时间:2014-07-30 12:15:52

标签: python datetime

尝试计算时间时出现以下错误,

AttributeError: type object 'datetime.datetime' has no attribute 'timedelta'

fajr_jamaat1打印输出是1900-01-01 04:25:00 请帮帮忙?

from datetime import datetime, timedelta

def round_up(tm):
    upmins = math.ceil(float(tm.minute)/10)*10
    diffmins = upmins - tm.minute
    newtime = tm + datetime.timedelta(minutes=diffmins)
    newtime = newtime.replace(second=0)
    return newtime

cnx = mysql.connector.connect(host=mysql_remote_host, user=mysql_remote_host_user, password=mysql_remote_host_password, database=mysql_remote_host_database)
cursor = cnx.cursor()
cursor.execute("select * from paramatta_prayertimes WHERE id =1")
results = cursor.fetchall()
id, date, fajr_begins, fajr_jamaat, sunrise, zuhr_begins, zuhr_jamaat, asr_begins, asr_jamaat, maghrib_jamaat, isha_begins, isha_jamaat = results[0]
cursor.close()
print fajr_begins

fajr_jamaat1=(timedelta(minutes=20) + datetime.strptime(str(fajr_begins), "%H:%M:%S"))
print fajr_jamaat1
round_up(fajr_jamaat1)

2 个答案:

答案 0 :(得分:4)

由于您将功能导入为

from datetime import datetime, timedelta

您无需将此功能称为

datetime.timedelta

您只需按名称

调用该函数即可
timedelta

如果您更喜欢使用以前的语法,只需导入模块本身

import datetime

然后你可以说

datetime.datetime
datetime.timedelta

答案 1 :(得分:1)

 newtime = tm + datetime.timedelta(minutes=diffmins) 

=>

 newtime = tm + timedelta(minutes=diffmins)