This issue was raised because I ask in a post if Weewx can return true if 
it is day, or false if it is night. I was tempted by Mr. Tom Keffer to 
write a script named daylight.py because he told me this is done 
voluntarily and suggested I do it, so I did.

The script ( attachment daylight.py ) is in 3 phases.

1) Parse sunrise and sunset time from Weewx and check if the content is a 
time format in this form,
for example:

00:00
12:00am 12:00AM
12:00 am 12:00 AM
12:00pm 12:00PM
12:00 pm 12:00 PM

If nothing applies from the above return N/A.

2)If the time is in 12hr mode convert it to 24hr.

3)Check if it is night or day using sunrise, sunset and current time.

The problem is that I'm not familiar with weewx development, how will I 
incorporate and register this script to Weewx so by importing sunrise and 
sunset time and giving $current.isDaylight tag, return true or false.

-- 
You received this message because you are subscribed to the Google Groups 
"weewx-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.
#!/usr/bin/env python
#
# This script was developed by Antonis Katsonis
#
"""Returns true if it is day, or false if it is night"""

import time
import re


sunriseTime = '--:--' # weewx sunrise
sunsetTime = '--:--' # weewx sunset


def convertTime(timeIn):
     if not timeIn.replace(':', '').isdigit():
             hour = timeIn.split(':')[0]
             minute = timeIn.split(':')[1].replace(' ', '').replace('AM', '').replace('PM', '')
             ampm = re.search('AM$|PM$', timeIn).group()

             if hour == 12 and ampm == 'AM':
                 return '00:' + minute
             elif hour == 12 and ampm == 'PM':
                   return '12:' + minute
             elif ampm == 'PM':
                   return str(int(hour) + 12) + minute
             else:
                 return timeIn
     else:
         return timeIn

def isDayTime(sunrise, sunset):
     sunrise = convertTime(sunrise.upper())
     sunset = convertTime(sunset.upper())
     currentTime = time.strftime("%H%M")
     sunrise = sunrise.replace(':', '')
     sunset = sunset.replace(':', '')
     if sunrise == sunset or sunrise > sunset:
         return 'N/A'
     elif currentTime >= sunrise and currentTime < sunset:
           return 'true'
     else:
         return 'false'

try:
    if time.strptime(sunriseTime, '%H:%M') and time.strptime(sunsetTime, '%H:%M'):
        format1 = True
except:
       format1 = False
try:
    if time.strptime(sunriseTime, '%I:%M%p') and time.strptime(sunsetTime, '%I:%M%p'):
        format2 = True
except:
       format2 = False
try:
    if time.strptime(sunriseTime, '%I:%M %p') and time.strptime(sunsetTime, '%I:%M %p'):
        format3 = True
except:
       format3 = False

if format1 or format2 or format3:
    print isDayTime(sunriseTime, sunsetTime)
else:
    print 'N/A'

Reply via email to