Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- # -*- coding: iso-8859-1 -*-
- import datetime
- import tzinfo_examples as timezone # implementation of concrete tzinfo subclass
- import math
- from sun import Sun
- __all__ = ['getsuninfo', 'Place']
- class Place(object):
- def __init__(self, name, coords, tz=timezone.Pacific):
- self.name = name # string
- self.coords = coords # tuple (E/W long, N/S lat)
- self.tz = tz # tzinfo constant
- def _hoursmins(hours):
- """Convert floating point time in hours to integer hrs, mins"""
- frach, h = math.modf(hours) # get fractional and whole parts
- m = round(frach * 60, 0) # convert fractional part to whole minutes
- if m == 60: # rounded up to whole hour?
- h, m = h + 1, 0
- return int(h), int(m)
- def _ymd(date):
- """Return y,m,d from datetime object as tuple"""
- return date.timetuple()[:3]
- def getsuninfo(location, date=None):
- """Return local datetime of sunrise, sunset, and length of day in hrs, mins"""
- querydate = datetime.date.today() if date is None else date
- args = _ymd(querydate) + location.coords
- utcrise, utcset = Sun().sunRiseSet(*args)
- daylength = Sun().dayLength(*args)
- hrs, mins = _hoursmins(daylength)
- risehour, risemin = _hoursmins(utcrise)
- sethour, setmin = _hoursmins(utcset)
- # convert times to timedelta values (ie from midnight utc of the date)
- midnight = datetime.datetime(tzinfo=timezone.utc, *_ymd(querydate))
- deltarise = datetime.timedelta(hours=risehour, minutes=risemin)
- utcdatetimerise = midnight + deltarise
- deltaset = datetime.timedelta(hours=sethour, minutes=setmin)
- utcdatetimeset = midnight + deltaset
- # convert results from UTC to local timezone
- localrise = utcdatetimerise.astimezone(location.tz)
- localset = utcdatetimeset.astimezone(location.tz)
- return localrise, localset, hrs, mins
- if __name__ == "__main__":
- import datetime
- import timezone
- def unittest(location, testdate):
- risetime, settime, hrs, mins = getsuninfo(location, testdate)
- print("Location: {}".format(location.name))
- print("Date:".format(testdate.strftime("%a %x")))
- print(risetime.strftime("Sunrise %I:%M %p"),
- settime.strftime("- Sunset %I:%M %p (%Z)"))
- print("daylight: %d:%02d" % (hrs, mins))
- print()
- place = Place("My House", (-121.990278, 47.204444), timezone.Pacific)
- # test dates just before and after 2007 DST change
- print("pre 2007")
- print("=========")
- unittest(place, datetime.date(2006, 4, 1))
- unittest(place, datetime.date(2006, 4, 2))
- unittest(place, datetime.date(2006, 10, 28))
- unittest(place, datetime.date(2006, 10, 29))
- print("2007")
- print("=========")
- unittest(place, datetime.date(2007, 3, 10))
- unittest(place, datetime.date(2007, 3, 11))
- unittest(place, datetime.date(2007, 11, 3))
- unittest(place, datetime.date(2007, 11, 4))
- print("Today")
- print("=========")
- unittest(place, datetime.date.today())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement