satagro / suntime Goto Github PK
View Code? Open in Web Editor NEWSimple sunset and sunrise time calculation python library.
License: GNU Lesser General Public License v3.0
Simple sunset and sunrise time calculation python library.
License: GNU Lesser General Public License v3.0
Hi,
The following snippet reveals an inconsistent behaviour regarding the sunrise time returned. For two points close by (Lucknow and Agra both in the same state of Uttar Pradesh in India), the sunrise time returned is completely different
import arrow
from suntime import Sun
import pytz
time = arrow.get("2020-04-30 01:00:00+05:30")
tz = pytz.timezone("Asia/Kolkata")
# Luckdown
lat = 26.848623
long = 80.8024271
sun_lockdown = Sun(lat, long)
# Agra
lat = 27.1761571
long = 77.9099726
sun_agra = Sun(lat, long)
print("Lucknow", sun_lockdown.get_sunrise_time(time).astimezone(tz)) # ==> Lucknow 2020-05-01 05:29:00+05:30
print("Agra", sun_agra.get_sunrise_time(time).astimezone(tz)) # ==> Agra 2020-04-30 05:40:00+05:30
In the polar regions, the sun may stay up the whole day (midnight sun) or may stay down the whole day (polar night). In both cases, we raise the same exception:
Line 36 in 8053379
It would be useful to know whether we have sunlight the whole day or night the whole day.
Looking at
Lines 104 to 107 in 8053379
Could you raise a different exception depending on whether we have midnight sun or polar night?
If you agree that this is a useful feature I can try to make a PR.
Commit e9b1fa4 changed the source for get_sunrise_time
and get_sunset_time
so that the date
parameter defaults to datetime.today()
. Previously, it was the at_date
parameter and defaulted to datetime.datetime.now()
.
Unfortunately, those are two different types. today()
is a datettime.date
, and now()
is a datetime.datetime
. The get_sun_timedelta
method that both of these calls expects a datetime.datetime
, resulting in a TypeError from utcoffset
.
Make the library available on pip as a package.
Unable to get sunrise or sunset on South and North Pole as latitude limited to about 85N and 87S.
Math issue or bug in the module?
Thanks for your work! Works great :)
Would be nice to have an option to suppress the print()
's
Yesterday I saw:
Today at Ohau sunrise is 06:08 and sunset is 18:16 local time
But today I am seeing:
Today at Ohau sunrise is 07:07 and sunset is 18:17 local time
It is just after midnight as I report this and it is worth noting that we go into daylights savings in about 26 hours from now so tomorrow sunrise will be around 07:07 and sunset around 19:17.
Ohau is in New Zealand, north of the capital Wellington.
Hi,
the example needs to read:
sun = suntime.Sun(latitude, longitude)
to work (or, of course, from suntime import Sun
).
I think this may be the same as #29 but testing it, I couldn't exactly get the same reproduction. At issue is when I use this library on my Pi, I'm getting a sunset value from the day before, but sunrise is correct.
This little snippet reproduces the issue for me.
import time
from datetime import datetime
from suntime import Sun, SunTimeException
from dateutil import tz
latitude = 42.01
longitude = -87.98
sun = Sun(latitude, longitude)
def main():
while True:
now = datetime.now(tz.gettz('US/Central'))
ss = sun.get_sunset_time(at_date=now, time_zone=tz.gettz('US/Central'))
sr = sun.get_sunrise_time(at_date=now, time_zone=tz.gettz('US/Central'))
print("Now: {}, sr: {}, ss: {}".format(now, sr, ss))
time.sleep(1)
if __name__ == '__main__':
main()
which gives me the following results
Now: 2024-03-19 07:55:42.667241-05:00, sr: 2024-03-19 06:55:48-05:00, ss: 2024-03-18 19:03:36-05:00
Now: 2024-03-19 07:55:43.670629-05:00, sr: 2024-03-19 06:55:48-05:00, ss: 2024-03-18 19:03:36-05:00
Now: 2024-03-19 07:55:44.673976-05:00, sr: 2024-03-19 06:55:48-05:00, ss: 2024-03-18 19:03:36-05:00
Now: 2024-03-19 07:55:45.677398-05:00, sr: 2024-03-19 06:55:48-05:00, ss: 2024-03-18 19:03:36-05:00
Now: 2024-03-19 07:55:46.680704-05:00, sr: 2024-03-19 06:55:48-05:00, ss: 2024-03-18 19:03:36-05:00
Now: 2024-03-19 07:55:47.684021-05:00, sr: 2024-03-19 06:55:48-05:00, ss: 2024-03-18 19:03:36-05:00
Now: 2024-03-19 07:55:48.687425-05:00, sr: 2024-03-19 06:55:48-05:00, ss: 2024-03-18 19:03:36-05:00
Now: 2024-03-19 07:55:49.690767-05:00, sr: 2024-03-19 06:55:48-05:00, ss: 2024-03-18 19:03:36-05:00
Now: 2024-03-19 07:55:50.694099-05:00, sr: 2024-03-19 06:55:48-05:00, ss: 2024-03-18 19:03:36-05:00
Now: 2024-03-19 07:55:51.697491-05:00, sr: 2024-03-19 06:55:48-05:00, ss: 2024-03-18 19:03:36-05:00
The values returned are correct, but notice that sunset is yesterday, which doesn't make a lot of sense. This behavior is the same whether I use at_date or not.
Name: suntime
Version: 1.3.2
Summary: Simple sunset and sunrise time calculation python library
Home-page: https://github.com/SatAgro/suntime
Author: Krzysztof Stopa
Author-email: None
License: LGPLv3
Location: /home/pi/.local/lib/python3.9/site-packages
Requires: python-dateutil
Required-by:
Hi, I cannot run this script:
pi@sortlandcam-2:~/raspberry-timelapse $ pip3 install suntime
Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple
Collecting suntime
Downloading https://files.pythonhosted.org/packages/93/95/a4eec0b36daeda6fa84b804da308211141d4a6ada13da228ecdf49600434/suntime-1.2.5-py3-none-any.whl
Collecting python-dateutil (from suntime)
Using cached https://files.pythonhosted.org/packages/d4/70/d60450c3dd48ef87586924207ae8907090de0b306af2bce5d134d78615cb/python_dateutil-2.8.1-py2.py3-none-any.whl
Requirement already satisfied: six>=1.5 in /usr/lib/python3/dist-packages (from python-dateutil->suntime) (1.12.0)
Installing collected packages: python-dateutil, suntime
Successfully installed python-dateutil-2.8.1 suntime-1.2.5
pi@sortlandcam-2:~/raspberry-timelapse $ python3 suntime.py
Traceback (most recent call last):
File "suntime.py", line 2, in <module>
from suntime import Sun, SunTimeException
File "/home/pi/raspberry-timelapse/suntime.py", line 2, in <module>
from suntime import Sun, SunTimeException
ImportError: cannot import name 'Sun' from 'suntime' (/home/pi/raspberry-timelapse/suntime.py)
Is this because suntime changed something?
running the example which starts with:
import datetime
from suntime import Sun , SunTimeException
...it fails on the second line with
"ImportError: cannot import name 'Sun' from partially initialized module 'suntime' (most likely due to a circular import) "
The following code prints sunrise at 2020-02-17 19:55:00+00:00 and sunset at 2020-02-17 08:40:00+00:00.
sun = Sun(-21.1814185045857, 149.151316322525)
dt = datetime.fromtimestamp(1581932515)
print(sun.get_sunrise_time(dt))
print(sun.get_sunset_time(dt))
Old code
sun.get_local_sunrise_time()
that previously worked (with suntime-1.2.5) now raises an exception (with suntime-1.3.0):
TypeError: Sun.get_local_sunrise_time() missing 2 required positional arguments: 'at_date' and 'time_zone'
from datetime import datetime
from suntime import Sun, SunTimeException
latitude = 56.49771
longitude = 82.0475315
day = datetime(2021, 4, 24)
sunrise = Sun(latitude, longitude).get_local_sunrise_time(day)
sunset = Sun(latitude, longitude).get_local_sunset_time(day)
print(sunrise)
print(sunset)
2021-04-25 06:04:00+07:00
2021-04-24 20:57:00+07:00
Why is there something wrong on the day? I have tried other days and the number of wrong days is too much in a year (at least 1/2 year)
This package has a few other packages now relying on it.
It would be good to get some basic tests in place.
ping package owners which depend on this, hopefully someone will help @jazzsewera , @GustavsR , @kostya-ten, @Shubbler, @jud55555, @dvprrsh, @lonesomebyte537
Code like this:
def get_sunrise_time(self, at_date=datetime.now(), time_zone=timezone.utc):
isn't going to work right for long-running apps.
The default value for at_date
gets saved when the function is parsed by the Python interpreter.
Thereafter, every invocation of get_sunrise_time()
is going to use the same exact value for at_date
.
You can see with this example:
from datetime import datetime
import time
def foo(when=datetime.now()):
return when
print(foo())
time.sleep(3)
print(foo())
which prints the exact same value twice, something like this:
2024-03-16 15:27:59.125163
2024-03-16 15:27:59.125163
The fix is to do something like this:
def get_sunrise_time(self, at_date=None, time_zone=timezone.utc):
if at_date is None:
at_date = datetime.now()
The result from sun.get_sunrise_time
and sun.get_sunset_time
is related to the starttime of script, not the current time.
For example:
def cal_sun(lat,lon):
sun = Sun(lat, lon)
sr = sun.get_sunrise_time()
ss = sun.get_sunset_time()
return sr, ss
while True:
sr, ss = cal_sun(lat,lon)
print ('Ss time: ', ss, 'Sr time: ',sr)
If I run this script on 2019/02/03, ss
and sr
will depend on 2019/02/03. Even though it's 2019/02/04, ss
and sr
will still be as same as those on 2019/02/03.
I get a ValueError on a corner case, here is the code to reproduce:
import datetime
import suntime
sun = suntime.Sun(40.125, -105.237)
date = datetime.date(2010, 10, 31)
sun.get_sunset_time(date)
The exact error is:
ValueError: day is out of range for month
$ pip3 show suntime | grep Version
Version: 1.2.5
$ python3
Python 3.7.5 (default, Apr 19 2020, 20:18:17)
[GCC 9.2.1 20191008] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import datetime
>>> import pytz
>>> from suntime import Sun
>>>
>>> tz = pytz.timezone('America/New_York')
>>> tz
<DstTzInfo 'America/New_York' LMT-1 day, 19:04:00 STD>
>>>
>>> dt = datetime.datetime.now(tz=tz)
>>> dt
datetime.datetime(2020, 4, 29, 7, 35, 24, 335561, tzinfo=<DstTzInfo 'America/New_York' EDT-1 day, 20:00:00 DST>)
First, get the sunrise/set times for Washington D.C. These look correct.
(www.timeanddate.com report 06:12 and 19:59 for sunrise/set today)
>>> sun = Sun(38.8895, -77.0353)
>>> sun.get_local_sunrise_time(dt)
datetime.datetime(2020, 4, 29, 6, 12, tzinfo=tzlocal())
>>> sun.get_local_sunset_time(dt)
datetime.datetime(2020, 4, 29, 19, 59, tzinfo=tzlocal())
>>> sun.get_sunrise_time(dt)
datetime.datetime(2020, 4, 29, 10, 12, tzinfo=tzutc())
>>> sun.get_sunset_time(dt)
datetime.datetime(2020, 4, 29, 23, 59, tzinfo=tzutc())
Now change the latitude 1 degree north. A small change is expected, but notice that the sunset time jumps back one day now that the sunset time goes beyond 20:00 local time (00:00 UTC)
>>> sun = Sun(39.8895, -77.0353)
>>> sun.get_local_sunrise_time(dt)
datetime.datetime(2020, 4, 29, 6, 10, tzinfo=tzlocal())
>>> sun.get_local_sunset_time(dt)
datetime.datetime(2020, 4, 28, 20, 1, tzinfo=tzlocal()) <=== Wrong, should be the 29th
>>> sun.get_sunrise_time(dt)
datetime.datetime(2020, 4, 29, 10, 10, tzinfo=tzutc())
>>> sun.get_sunset_time(dt)
datetime.datetime(2020, 4, 29, 0, 1, tzinfo=tzutc()) <==== Wrong, should be the 30th
Line 168 in db63eb8
This condition can never be true since force_range
has been called on hr
before.
This causes a bug in sunset times being before sunrise times.
Minimam reproducible example:
import pandas as pd
from suntime import Sun
sun = Sun(37.7, -122.0)
assert ( sun.get_sunset_time(date=pd.Timestamp(year=2022, month=11, day=25, hour=22, minute=12, tz="UTC")) > sun.get_sunrise_time(date=pd.Timestamp(year=2022, month=11, day=25, hour=22, minute=12, tz="UTC")) )
Great lib otherwise :)
>>> from suntime import Sun
>>>
>>> latitude = 43
>>> longitude = -88
>>>
>>> sun = Sun(latitude, longitude)
>>>
>>> today_sr = sun.get_local_sunrise_time()
>>> today_ss = sun.get_local_sunset_time()
>>> print(today_sr)
2023-12-01 13:03:00+00:00
>>> print(today_ss)
2023-12-01 22:18:00+00:00
>>>
>>> today_sr = sun.get_sunrise_time()
>>> today_ss = sun.get_sunset_time()
>>> print(today_sr)
2023-12-01 13:03:00+00:00
>>> print(today_ss)
2023-12-01 22:18:00+00:00
Even using the sample code, I can't seem to get the times to calculate down to the second. It seems to round to minutes only. Am I missing an option and is this possible to add?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.