Git Product home page Git Product logo

Comments (3)

jfbauer432 avatar jfbauer432 commented on August 14, 2024 1

I suspect this is the same issue I reported in #12, with a difference that you are on the other side of UTC so the sunrise is the wrong day instead of sunset.

I expanded my workaround, see it this works.

import datetime
from suntime import Sun
import pytz


def sunset(lat, lon, dt=None, tz=None):
    if dt is None:
        dt = datetime.datetime.now()

    sun = Sun(lat, lon)
    ss = sun.get_local_sunset_time(dt, local_time_zone=tz)
    sr = sun.get_local_sunrise_time(dt, local_time_zone=tz)
    if ss.utcoffset() < datetime.timedelta(0):
        if ss < sr:
            ss = ss + datetime.timedelta(1)
            print('workaround: added 1 day to sunset time')
    return ss


def sunrise(lat, lon, dt=None, tz=None):
    if dt is None:
        dt = datetime.datetime.now()

    sun = Sun(lat, lon)
    ss = sun.get_local_sunset_time(dt, local_time_zone=tz)
    sr = sun.get_local_sunrise_time(dt, local_time_zone=tz)
    if sr.utcoffset() > datetime.timedelta(0):
        if ss < sr:
            sr = sr - datetime.timedelta(1)
            print('workaround: subtracted 1 day from sunrise time')

    return sr


latitude = 56.49771
longitude = 82.0475315
tz = pytz.timezone('Asia/Novosibirsk')
day = datetime.datetime(2021, 4, 24)
print(f'For {latitude}, {longitude}, tz={tz}, day={day}')
print('sunrise:', sunrise(latitude, longitude, day, tz))
print('sunset: ', sunset(latitude, longitude, day, tz))
print()

latitude = 39.8895
longitude = -77.0353
tz = pytz.timezone('US/Eastern')
day = datetime.datetime(2020, 4, 29)
print(f'For {latitude}, {longitude}.  tz={tz}, day={day}')
print('sunrise:', sunrise(latitude, longitude, day, tz))
print('sunset: ', sunset(latitude, longitude, day, tz))

Running that I get

For 56.49771, 82.0475315, tz=Asia/Novosibirsk, day=2021-04-24 00:00:00
workaround: subtracted 1 day from sunrise time
sunrise: 2021-04-24 06:04:00+07:00
sunset:  2021-04-24 20:57:00+07:00

For 39.8895, -77.0353.  tz=US/Eastern, day=2020-04-29 00:00:00
sunrise: 2020-04-29 06:10:00-04:00
workaround: added 1 day to sunset time
sunset:  2020-04-29 20:01:00-04:00

from suntime.

tkhoa22 avatar tkhoa22 commented on August 14, 2024

Thank for reply
I can fix it but I just want to report to module developer for module inside fix

from suntime.

yasirroni avatar yasirroni commented on August 14, 2024

Solved in #19, just add tz as input and done.

Usage:

import datetime
import pytz
from suntime import Sun, SunTimeException

latitude = 7.7956
longitude = 110.3695
tz = pytz.timezone("Asia/Jakarta")

day = datetime.datetime(2022, 4, 24)
print(tz.utcoffset(day))

sun = Sun(latitude, longitude)
try:
    print("")
    print(datetime.datetime.now())
    print()
    print(sun.get_sunrise_time())
    print(sun.get_sunset_time())
    print("")
    print(sun.get_sunrise_time(tz=tz))
    print(sun.get_sunset_time(tz=tz))

from suntime.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.