Skip to content

Commit

Permalink
Added exception handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
Krzysztof Stopa committed Sep 11, 2017
1 parent b1999a5 commit 019e355
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 21 deletions.
17 changes: 14 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
# SunTime
Simple sunset and sunrise time calculation python library.

## Instalation
## Installation

Download and type:

python setup.py install


## Usage

You can use the library to get UTC and local time sunrise and sunset times by using:
You can use the library to get UTC and local time sunrise and sunset times typing:

import datetime
import suntime
Expand All @@ -23,11 +24,21 @@ You can use the library to get UTC and local time sunrise and sunset times by us
today_sr = sun.get_sunrise_time()
today_ss = sun.get_sunset_time()

# On a special date in your machine localtime
# On a special date in your machine's local time zone
abd = datetime.date(2014,10,3)
abd_sr = sun.get_local_sunrise_time(abd)
abd_ss = sun.get_local_sunset_time(abd)

# Error handling (no sunset or sunrise on given location)
latitude = 87.87
longitude = 0.1

try:
abd_sr = sun.get_local_sunrise_time(abd)
abd_ss = sun.get_local_sunset_time(abd)
except SunTimeException as e:
print("Error: {0}".format(e))


## License

Expand Down
81 changes: 63 additions & 18 deletions suntime/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@
from dateutil import tz


class SunTimeException(Exception):

def __init__(self, message):
super(SunTimeException, self).__init__(message)


class Sun:
"""
Approximated calculation of sunrise and sunset datetimes. Adapted from:
Expand All @@ -20,12 +26,26 @@ def get_sunrise_time(self, date=datetime.date.today()):
:param lon: Longitude
:param date: Reference date
:return: UTC sunrise datetime
:raises: SunTimeException when there is no sunrise and sunset on given location and date
"""
return self._calc_sun_time(date, True)

sr = self._calc_sun_time(date, True)
if sr is None:
raise SunTimeException('The sun never rises on this location (on the specified date)')
else:
return sr

def get_local_sunrise_time(self, date=datetime.date.today(), local_time_zone=tz.tzlocal()):
return self.get_sunrise_time(date).astimezone(local_time_zone)
"""
Get sunrise time for local or custom time zone.
:param date: Reference date
:param local_time_zone: Local or custom time zone.
:return: Local time zone sunrise datetime
"""
sr = self._calc_sun_time(date, True)
if sr is None:
raise SunTimeException('The sun never rises on this location (on the specified date)')
else:
return sr.astimezone(local_time_zone)

def get_sunset_time(self, date=datetime.date.today()):
"""
Expand All @@ -34,13 +54,36 @@ def get_sunset_time(self, date=datetime.date.today()):
:param lon: Longitude
:param date: Reference date
:return: UTC sunset datetime
:raises: SunTimeException when there is no sunrise and sunset on given location and date.
"""
return self._calc_sun_time(date, False)
ss = self._calc_sun_time(date, False)
if ss is None:
raise SunTimeException('The sun never sets on this location (on the specified date)')
else:
return ss

def get_local_sunset_time(self, date=datetime.date.today(), local_time_zone=tz.tzlocal()):
return self.get_sunset_time(date).astimezone(local_time_zone)
"""
Get sunset time for local or custom time zone.
:param date: Reference date
:param local_time_zone: Local or custom time zone.
:return: Local time zone sunset datetime
"""
ss = self._calc_sun_time(date, False)
if ss is None:
raise SunTimeException('The sun never sets on this location (on the specified date)')
else:
return ss.astimezone(local_time_zone)

def _calc_sun_time(self, date=datetime.date.today(), isRiseTime=True, zenith=90.8):
"""
Calculate sunrise or sunset date.
:param date: Reference date
:param isRiseTime: True if you want to calculate sunrise time.
:param zenith: Sun reference zenith
:return: UTC sunset or sunrise datetime
:raises: SunTimeException when there is no sunrise and sunset on given location and date
"""
# isRiseTime == False, returns sunsetTime
day = date.day
month = date.month
Expand Down Expand Up @@ -89,12 +132,10 @@ def _calc_sun_time(self, date=datetime.date.today(), isRiseTime=True, zenith=90.
# 7a. calculate the Sun's local hour angle
cosH = (math.cos(TO_RAD*zenith) - (sinDec * math.sin(TO_RAD*self._lat))) / (cosDec * math.cos(TO_RAD*self._lat))

# TODO Raise exceptions
if cosH > 1:
return {'status': False, 'msg': 'the sun never rises on this location (on the specified date)'}

return None # The sun never rises on this location (on the specified date)
if cosH < -1:
return {'status': False, 'msg': 'the sun never sets on this location (on the specified date)'}
return None # The sun never sets on this location (on the specified date)

# 7b. finish calculating H and convert into hours

Expand Down Expand Up @@ -133,14 +174,18 @@ def _force_range(v, max):


if __name__ == '__main__':
sun = Sun(52.234, 21.00)
print(sun.get_local_sunrise_time())
print(sun.get_local_sunset_time())
sun = Sun(85.0, 21.00)
try:
print(sun.get_local_sunrise_time())
print(sun.get_local_sunset_time())

# On a special date in UTC
abd = datetime.date(2014, 10, 3)
print("Ada")
abd_sr = sun.get_local_sunrise_time(abd)
abd_ss = sun.get_local_sunset_time(abd)
print(abd_sr)
print(abd_ss)

abd = datetime.date(2014, 1, 3)
abd_sr = sun.get_local_sunrise_time(abd)
abd_ss = sun.get_local_sunset_time(abd)
print(abd_sr)
print(abd_ss)
except SunTimeException as e:
print("Error: {0}".format(e))

0 comments on commit 019e355

Please sign in to comment.