Skip to content

Commit

Permalink
Solved errors at doc example. Solved issue with script today date whe…
Browse files Browse the repository at this point in the history
…n no date param was given.
  • Loading branch information
kstopa committed Feb 3, 2019
1 parent a39c976 commit 8f94c7b
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 39 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.idea/
build/
dist/
example.py
suntime.egg-info/
suntime/__pycache__/
59 changes: 33 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,40 +9,47 @@ Download and type:

Or using pip:

pip3 install dateutil
pip3 install python-dateutil
pip3 install git+https://github.com/satagro/suntime.git

## Usage

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

import datetime
import suntime

latitude = 51.21
longitude = 21.01

sun = Sun(latitude, longitude)

# Get today's sunrise and sunset in UTC
today_sr = sun.get_sunrise_time()
today_ss = sun.get_sunset_time()

# On a special date in your machine's local time zone
abd = datetime.date(2014,10,3)
```python
import datetime
from suntime import Sun, SunTimeException

latitude = 51.21
longitude = 21.01

sun = Sun(latitude, longitude)

# Get today's sunrise and sunset in UTC
today_sr = sun.get_sunrise_time()
today_ss = sun.get_sunset_time()
print('Today at Warsaw the sun raised at {} and get down at {} UTC'.
format(today_sr.strftime('%H:%M'), today_ss.strftime('%H:%M')))

# 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)
print('On {} the sun at Warsaw raised at {} and get down at {}.'.
format(abd, abd_sr.strftime('%H:%M'), abd_ss.strftime('%H:%M')))

# Error handling (no sunset or sunrise on given location)
latitude = 87.55
longitude = 0.1
sun = Sun(latitude, longitude)
try:
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))

print('On {} at somewhere in the north the sun raised at {} and get down at {}.'.
format(abd, abd_sr.strftime('%H:%M'), abd_ss.strftime('%H:%M')))
except SunTimeException as e:
print("Error: {0}.".format(e))
```

## License

Expand Down
6 changes: 3 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env python

from distutils.core import setup
from setuptools import setup
import suntime

setup(name='suntime',
Expand All @@ -9,6 +9,6 @@
author=suntime.__author__,
author_email=suntime.__email__,
url='https://github.com/SatAgro/suntime',
copyright='Copyright 2017 SatAgro',
packages=['suntime'], requires=['dateutil']
copyright='Copyright 2019 SatAgro',
packages=['suntime'], install_requires=['python-dateutil']
)
4 changes: 2 additions & 2 deletions suntime/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from .suntime import Sun
from .suntime import Sun, SunTimeException

__author__ = 'Krzysztof Stopa'
__version__ = '1.2'
__version__ = '1.2.1'
__email__ = '[email protected]'
20 changes: 12 additions & 8 deletions suntime/suntime.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,63 +19,67 @@ def __init__(self, lat, lon):
self._lat = lat
self._lon = lon

def get_sunrise_time(self, date=datetime.date.today()):
def get_sunrise_time(self, date=None):
"""
Calculate the sunrise time for given date.
:param lat: Latitude
:param lon: Longitude
:param date: Reference date
:param date: Reference date. Today if not provided.
:return: UTC sunrise datetime
:raises: SunTimeException when there is no sunrise and sunset on given location and date
"""
date = datetime.date.today() if date is None else date
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()):
def get_local_sunrise_time(self, date=None, local_time_zone=tz.tzlocal()):
"""
Get sunrise time for local or custom time zone.
:param date: Reference date
:param date: Reference date. Today if not provided.
:param local_time_zone: Local or custom time zone.
:return: Local time zone sunrise datetime
"""
date = datetime.date.today() if date is None else date
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()):
def get_sunset_time(self, date=None):
"""
Calculate the sunset time for given date.
:param lat: Latitude
:param lon: Longitude
:param date: Reference date
:param date: Reference date. Today if not provided.
:return: UTC sunset datetime
:raises: SunTimeException when there is no sunrise and sunset on given location and date.
"""
date = datetime.date.today() if date is None else date
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()):
def get_local_sunset_time(self, date=None, local_time_zone=tz.tzlocal()):
"""
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
"""
date = datetime.date.today() if date is None else date
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):
def _calc_sun_time(self, date, isRiseTime=True, zenith=90.8):
"""
Calculate sunrise or sunset date.
:param date: Reference date
Expand Down

0 comments on commit 8f94c7b

Please sign in to comment.