martineau

sunriseset.py

Apr 19th, 2021
746
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env python3
  2. # -*- coding: iso-8859-1 -*-
  3.  
  4. import datetime
  5. import tzinfo_examples as timezone  # implementation of concrete tzinfo subclass
  6. import math
  7. from sun import Sun
  8.  
  9. __all__ = ['getsuninfo', 'Place']
  10.  
  11. class Place(object):
  12.     def __init__(self, name, coords, tz=timezone.Pacific):
  13.         self.name = name        # string
  14.         self.coords = coords    # tuple (E/W long, N/S lat)
  15.         self.tz = tz            # tzinfo constant
  16.  
  17.  
  18. def _hoursmins(hours):
  19.     """Convert floating point time in hours to integer hrs, mins"""
  20.     frach, h = math.modf(hours)  # get fractional and whole parts
  21.     m = round(frach * 60, 0)     # convert fractional part to whole minutes
  22.     if m == 60:                  # rounded up to whole hour?
  23.         h, m = h + 1, 0
  24.     return int(h), int(m)
  25.  
  26. def _ymd(date):
  27.     """Return y,m,d from datetime object as tuple"""
  28.     return date.timetuple()[:3]
  29.  
  30. def getsuninfo(location, date=None):
  31.     """Return local datetime of sunrise, sunset, and length of day in hrs, mins"""
  32.     querydate = datetime.date.today() if date is None else date
  33.     args = _ymd(querydate) + location.coords
  34.     utcrise, utcset = Sun().sunRiseSet(*args)
  35.     daylength = Sun().dayLength(*args)
  36.     hrs, mins = _hoursmins(daylength)
  37.  
  38.     risehour, risemin = _hoursmins(utcrise)
  39.     sethour, setmin = _hoursmins(utcset)
  40.  
  41.     # convert times to timedelta values (ie from midnight utc of the date)
  42.     midnight = datetime.datetime(tzinfo=timezone.utc, *_ymd(querydate))
  43.     deltarise = datetime.timedelta(hours=risehour, minutes=risemin)
  44.     utcdatetimerise = midnight + deltarise
  45.     deltaset = datetime.timedelta(hours=sethour, minutes=setmin)
  46.     utcdatetimeset = midnight + deltaset
  47.  
  48.     # convert results from UTC to local timezone
  49.     localrise = utcdatetimerise.astimezone(location.tz)
  50.     localset = utcdatetimeset.astimezone(location.tz)
  51.  
  52.     return localrise, localset, hrs, mins
  53.  
  54. if __name__ == "__main__":
  55.     import datetime
  56.     import timezone
  57.  
  58.     def unittest(location, testdate):
  59.         risetime, settime, hrs, mins = getsuninfo(location, testdate)
  60.  
  61.         print("Location: {}".format(location.name))
  62.         print("Date:".format(testdate.strftime("%a %x")))
  63.         print(risetime.strftime("Sunrise %I:%M %p"),
  64.               settime.strftime("- Sunset %I:%M %p (%Z)"))
  65.         print("daylight: %d:%02d" % (hrs, mins))
  66.         print()
  67.  
  68.     place = Place("My House", (-121.990278, 47.204444), timezone.Pacific)
  69.  
  70.     # test dates just before and after 2007 DST change
  71.     print("pre 2007")
  72.     print("=========")
  73.     unittest(place, datetime.date(2006, 4, 1))
  74.     unittest(place, datetime.date(2006, 4, 2))
  75.     unittest(place, datetime.date(2006, 10, 28))
  76.     unittest(place, datetime.date(2006, 10, 29))
  77.  
  78.     print("2007")
  79.     print("=========")
  80.     unittest(place, datetime.date(2007, 3, 10))
  81.     unittest(place, datetime.date(2007, 3, 11))
  82.     unittest(place, datetime.date(2007, 11, 3))
  83.     unittest(place, datetime.date(2007, 11, 4))
  84.  
  85.     print("Today")
  86.     print("=========")
  87.     unittest(place, datetime.date.today())
  88.  
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×