Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/346.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将datetime.timedelta与datetime.now进行比较_Python_Datetime_Raspberry Pi_Delta - Fatal编程技术网

Python 将datetime.timedelta与datetime.now进行比较

Python 将datetime.timedelta与datetime.now进行比较,python,datetime,raspberry-pi,delta,Python,Datetime,Raspberry Pi,Delta,我需要从sql数据库中设置的时间停止脚本。我的想法是使用If-else 正如您所看到的,我并不擅长python,但请继续:-) 下面是我现在使用的脚本 我现在想不出如何将timedelta与datetime进行比较 #!/usr/bin/python import MySQLdb as mdb import sys import wiringpi as wiringpi import os import settings from time import sleep from time

我需要从sql数据库中设置的时间停止脚本。我的想法是使用If-else

正如您所看到的,我并不擅长python,但请继续:-)

下面是我现在使用的脚本

我现在想不出如何将timedelta与datetime进行比较

    #!/usr/bin/python

import MySQLdb as mdb
import sys
import wiringpi as wiringpi
import os
import settings
from time import sleep
from time import time
from datetime import datetime, date, time 

if len(sys.argv) != 2:
        print "Usage: ./thermostat.py <scheduleid>"
        sys.exit()
scheduleid = sys.argv[1]

# Enable GPIO
os.environ['WIRINGPI_GPIOMEM'] = '1'
wiringpi.wiringPiSetupGpio()

# open a database connection
con = mdb.connect(host=settings.SQLSERVER,user=settings.SQLUSER,passwd=settings.SQLPASS,db=settings.SQLDB);

# prepare a cursor object using cursor() method
cursor_actual = con.cursor ()
cursor_target = con.cursor ()
cursor_stats = con.cursor ()
cursor_endtime = con.cursor ()

# execute the SQL query using execute() method.
cursor_actual.execute ("SELECT temperature FROM temperature WHERE timestamp = (SELECT MAX(timestamp) FROM temperature)")
cursor_target.execute (("SELECT targettemp FROM rules WHERE id = %s"),(scheduleid))
cursor_endtime.execute ("SELECT timeend FROM schedules WHERE id = '4'")


# fetch all of the rows from the query
actual = cursor_actual.fetchall()
target = cursor_target.fetchall()
endtime = cursor_endtime.fetchall()

# print the rows
for row in actual:
 actual = row[0]

for row in target:
 target = row[0]

#delta now time
utcnow = datetime.utcnow()
midnight_utc = datetime.combine(utcnow.date(), time(0))
delta = utcnow - midnight_utc

# Switching ON and OFF the boiler
if actual < target and endtime < delta.total_seconds():
    cursor_stats.execute("INSERT INTO stats(timestamp,currenttemp,targettemp,state) VALUES (CURRENT_TIMESTAMP,%s,%s,'ON')",(actual,target))
    print "The temperature is {} and the target temperature is {} so the heating is turned ON".format(actual,target)
    print endtime
    print delta.total_seconds()
    exec settings.on

else:
    cursor_stats.execute("INSERT INTO stats(timestamp,currenttemp,targettemp,state) VALUES (CURRENT_TIMESTAMP,%s,%s,'OFF')",(actual,target))
    print "The temperature is {} and the target temperature is {} so the heating is turned OFF".format(actual,target)
    print endtime
    print delta.total_seconds()
    exec settings.off

# close mysql cleanly
con.commit()
cursor_actual.close ()
cursor_target.close ()
con.close ()

# Exit the script
sys.exit()

谢谢

如何将
timeend
插入
时间表
表中?看起来你的代码还可以,但是你从时间表中读取的日期很奇怪。我使用的是phpmyadmin。我创建了表并将类型设置为time。然后我把这个17:00:00
61200==17*3600
,所以它有点道理。数据库中的
timeend
字段是什么类型?如果它是整数(而不是日期),也许phpmyadmin所做的事情是明智的!3600从哪里来?TIMEND字段类型为time。很明显,一小时内感谢了3600秒。时间增量在内部以秒为单位测量。
pi@Relay-Boiler:~/scripts $ ./3.py 1
The temperature is 21.05 and the target temperature is 40.0 so the heating is turned OFF
((datetime.timedelta(0, 68400),),)
77363.030178