如何在if语句中使用本地时间(HH:MM)-python

蒂斯

我根据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-objects进行所有计算

import RPi.GPIO as GPIO
import time
import urllib2
import json
from datetime import datetime
from business_calendar import Calendar, MO, TU, WE, TH, FR
from pytz import timezone, utc

local_timezone = timezone('Europe/Amsterdam')

print "Here we go! Press CTRL+C to exit"
try:
    while True:
        #werkdag def.
        cal = Calendar(workdays=[MO,TU,WE,TH,FR])
        busday = cal.isbusday(date)
        now = datetime.now(local_timezone)
        work_start = now.replace(hour=7, minute=0, second=0, microsecond=0)
        work_end = now.replace(hour=23, minute=0, second=0, microsecond=0)

        # Zonsopgang/Ondegang
        request = 'http://api.sunrise-sunset.org/json?lat=51.9913XXX&lng=4.4733XXX&formatted=0'
        response = urllib2.urlopen(request)
        timestring = json.loads(response.read())
        sunset = datetime.strptime(timestring['results']['sunset'],'%Y-%m-%dT%H:%M:%S+00:00')
        sunset = utc.localize(sunset).astimezone(local_timezone)
        sunrise = datetime.strptime(timestring['results']['sunrise'],'%Y-%m-%dT%H:%M:%S+00:00')
        sunrise = utc.localize(sunset).astimezone(local_timezone)
        if busday and (work_start <= now <= sunrise or sunset <= now <= work_end):
            # rest of the statements
            # end statement
except KeyboardInterrupt:
        GPIO.cleanup()

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何在python中以hh:mm格式获取时间?

来自分类Dev

如何在Python的MySQL语句中使用DATE_FORMAT?

来自分类Dev

如何在MySQL Insert语句中使用python变量,出现元组错误,请提出建议

来自分类Dev

如何在多个if语句中使用python生成器?

来自分类Dev

如何在Python的MySQL语句中使用DATE_FORMAT?

来自分类Dev

如何在 Python 的 if 語句中使用 RegEx?

来自分类Dev

如何在Python的'in'语句中获取索引

来自分类Dev

如何使python自动以时间格式(HH:MM:SS)放置冒号

来自分类Dev

我如何使用 python 将 ISO 时间格式转换为 yyyy-mm-dd hh:mm:ss

来自分类Dev

在Python中使用涉及时间的if语句

来自分类Dev

如何在python 3.8的异步编程中使用“ yield”语句?

来自分类Dev

如何在Python中使用多个逻辑AND缩短长的'if'语句?

来自分类Dev

在python条件语句中使用模运算符

来自分类Dev

在Python的If语句中使用re.match

来自分类Dev

在Python的Try and Except中的“ if”语句中使用“或”

来自分类Dev

在from-import语句中的python函数中使用输入

来自分类Dev

在if和if not语句中使用多个条件(python)

来自分类Dev

python在if语句中使用ands求值的顺序

来自分类Dev

在sqlite3语句中使用Python变量

来自分类Dev

在if语句中使用“ not in”时出现Python错误?

来自分类Dev

在if语句中使用python表达式的输出

来自分类Dev

在 DEF() 语句中使用 if 在 python 中返回错误值

来自分类Dev

在python中select语句的like子句中使用%

来自分类Dev

如何在python中使用pass语句?如何有效使用pass语句?

来自分类Dev

如何在Python中使用Javascript + Date()数字时间戳?

来自分类Dev

如何在Python中使用Lambda作为日期时间参数

来自分类Dev

如何在Python中使用Javascript + Date()数字时间戳?

来自分类Dev

如何在“ if”语句中将列表与整数进行比较?Python

来自分类Dev

如何在python逻辑语句中概念化分组

Related 相关文章

  1. 1

    如何在python中以hh:mm格式获取时间?

  2. 2

    如何在Python的MySQL语句中使用DATE_FORMAT?

  3. 3

    如何在MySQL Insert语句中使用python变量,出现元组错误,请提出建议

  4. 4

    如何在多个if语句中使用python生成器?

  5. 5

    如何在Python的MySQL语句中使用DATE_FORMAT?

  6. 6

    如何在 Python 的 if 語句中使用 RegEx?

  7. 7

    如何在Python的'in'语句中获取索引

  8. 8

    如何使python自动以时间格式(HH:MM:SS)放置冒号

  9. 9

    我如何使用 python 将 ISO 时间格式转换为 yyyy-mm-dd hh:mm:ss

  10. 10

    在Python中使用涉及时间的if语句

  11. 11

    如何在python 3.8的异步编程中使用“ yield”语句?

  12. 12

    如何在Python中使用多个逻辑AND缩短长的'if'语句?

  13. 13

    在python条件语句中使用模运算符

  14. 14

    在Python的If语句中使用re.match

  15. 15

    在Python的Try and Except中的“ if”语句中使用“或”

  16. 16

    在from-import语句中的python函数中使用输入

  17. 17

    在if和if not语句中使用多个条件(python)

  18. 18

    python在if语句中使用ands求值的顺序

  19. 19

    在sqlite3语句中使用Python变量

  20. 20

    在if语句中使用“ not in”时出现Python错误?

  21. 21

    在if语句中使用python表达式的输出

  22. 22

    在 DEF() 语句中使用 if 在 python 中返回错误值

  23. 23

    在python中select语句的like子句中使用%

  24. 24

    如何在python中使用pass语句?如何有效使用pass语句?

  25. 25

    如何在Python中使用Javascript + Date()数字时间戳?

  26. 26

    如何在Python中使用Lambda作为日期时间参数

  27. 27

    如何在Python中使用Javascript + Date()数字时间戳?

  28. 28

    如何在“ if”语句中将列表与整数进行比较?Python

  29. 29

    如何在python逻辑语句中概念化分组

热门标签

归档