我在if语句中编写了一些代码来根据一天中的移动和时间来打开灯。如果(本地)时间介于日出和预设时间之间,或日落和预设时间之间,则If语句为true,随后将出现运动检测语句。在
目前,时间仅以小时为单位表示为整数。如何使if语句以小时和分钟工作(认为这将需要一个字符串)。因此,该语句将在6:45而不是7时起作用。在
这是我的密码。在
import RPi.GPIO as GPIO
import time
import urllib2
from datetime import datetime
from business_calendar import Calendar, MO, TU, WE, TH, FR
from pytz import timezone
#uur
fmh = "%H"
fmt = "$H:%M"
#Def. var.
x=0
now_utc = 0
now_amsterdam = 0
print "Here we go! Press CTRL+C to exit"
try:
while x < 1:
count = 0
date = datetime.today()
#werkdag def.
cal = Calendar(workdays=[MO,TU,WE,TH,FR])
busday = cal.isbusday(date)
# dT AMS-UTC
now_utc = datetime.now(timezone('UTC'))
now_amsterdam = now_utc.astimezone(timezone('Europe/Amsterdam'))
UTC = int(now_utc.strftime(fmh))
AMS = int(now_amsterdam.strftime(fmh))
dT = AMS - UTC
# Zonsopgang/Ondegang
request = 'http://api.sunrise-sunset.org/json?lat=51.9913XXX&lng=4.4733XXX&formatted=0'
response = urllib2.urlopen(request)
timestring = response.read()
utcsunrise = timestring[35:39] #Will be 35:39 for HH:MM and cant be a string then
utcsunset = timestring[71:73] #Will be 71:79 for HH:MM
amssunrise = int(utcsunrise) + dT #Wont work with a string
amssunset = int(utcsunset) + dT
if amssunrise < 8 #Will be 7:30
amssunrise = 8 #Will be 7:30
if amssunset < 21 #Will be 21:30
amssunset = 21 #Will be 21:30
#uur
uur = date.hour #Must be HH:MM instead of only hours
if busday == True and ((uur >= 7 and uur <= amssunrise) or (uur >= amssunset and uur <= 23)):
# rest of the statements
# end statement
except KeyboardInterrupt:
GPIO.cleanup()
我想听听你的想法。请注意,我对python不熟悉,但对编码不熟悉。在
您好, Thijs公司
使用
datetime
-对象执行所有计算:我不再使用它糟糕的内置库。查看
arrow
http://crsmithdev.com/arrow/只要一个提示。在
还要研究一下使用
requests
这是在python中处理http内容的实际方法。在我已经重写了你的应用程序,我可以提供以下链接。我不太确定你的目标是什么,普特试图尽可能复制它,但是。我希望这有帮助。在
http://hastebin.com/elomonacod.py
相关问题 更多 >
编程相关推荐