mourner / suncalc

A tiny JavaScript library for calculating sun/moon positions and phases.
BSD 2-Clause "Simplified" License
3.07k stars 412 forks source link

dates with times before a certain time return times for previous day #11

Open yorickvP opened 10 years ago

yorickvP commented 10 years ago

see title, when inputting a date that has a time before 00:40:54, the times are for the previous day. suncalc.getTimes(new Date(2013, 10, 20, 0, 40, 53), 52.2, 5.1) returns times for november 19th, but a second later returns times for november 20th as expected.

fchristant commented 10 years ago

I have a similar issue, this date...

Wed Feb 12 2014 00:47:44 GMT+0100

...which is a console.log on a valid js Date object, gives me back the SunCalc times for the day before. This bug destroys the accuracy and credibility of what I'm trying to build. Did you ever get a fix for your issue?

yorickvP commented 10 years ago

As far as I know, the issue is not fixed yet. However, a simple workaround is just to set the hours to the mid of the day, you'll get the correct times that way.

fchristant commented 10 years ago

Thanks, that's going to be some serious refactoring for me as in the real-time system I'm building the date is also a display date. Still hoping for a real fix, but I see little activity here.

yorickvP commented 10 years ago

replace your gettimes calls with a suncalc.getTimes(new Date(d.getFullYear(), d.getMonth(), d.getDate(), 12, 0, 0, 0, 0), lat, long)

fchristant commented 10 years ago

Thanks, that is helpful. It still means I need to split the display time from the internal time, but that looks like the only way forward, and doable.

mourner commented 10 years ago

@fchristant while I'm going to fix the problem, please don't complain — this is open source, if something's not working for you, I'm happily accepting pull requests. There's little activity on this repo because it's pretty much feature complete, this is a low priority bug because there's an easy workaround, and I'm focused on more important things like https://github.com/Leaflet/Leaflet.

fchristant commented 10 years ago

I'm not complaining, I'm reporting an issue. With "little activity" I mean activity on the issues, as in communication. I'm in favor of at least a basic acknowledgement, even a message saying "sorry, I won't fix this" is better than nothing. Anyway, thanks for clarifying.

mourner commented 10 years ago

OK, now I see what's going on here — SunCalc calculates all the times after it gets the time of the nearest solar transit. But solar transit isn't strictly 12am — e.g. if it's 12:44am, then 0:43am as input will result in dates of the previous day since its transit is closer.

fchristant commented 10 years ago

Thanks for the explanation. I did manage to work around the issue using the trick above, so no issues. I actually just launched a first version of a visualization that combines most info that your lib returns (except for azimuth):

http://www.jungledragon.com/daylight

This one is optimized for (wildlife) photographers. It has stateful URLs and one can even add a "light period" appointment to one's calendar. I'm sharing this just to let you know how valuable you library is. So thank you very much, and I hope you'll see better days in your country soon.

emcjh commented 10 years ago

Not sure if the issue I'm having is related but my dates all seem wrong. I'm making the call like this: var today = new Date(); var t = SunCalc.getTimes(today, 34.2, 84.1); console.log(today); console.log(t)

In the console this is what I see... first is valid js date followed by dump of my t var. It's showing dawn time as evening of day before and dusk as morning of current day...

Wed Apr 23 2014 15:46:54 GMT-0400 (Eastern Daylight Time)

Object {solarNoon: Wed Apr 23 2014 02:23:06 GMT-0400 (Eastern Daylight Time), nadir: Tue Apr 22 2014 14:23:06 GMT-0400 (Eastern Daylight Time), sunrise: Tue Apr 22 2014 19:44:26 GMT-0400 (Eastern Daylight Time), sunset: Wed Apr 23 2014 09:01:45 GMT-0400 (Eastern Daylight Time), sunriseEnd: Tue Apr 22 2014 19:47:06 GMT-0400 (Eastern Daylight Time)…}

dawn: Tue Apr 22 2014 19:18:13 GMT-0400 (Eastern Daylight Time) dusk: Wed Apr 23 2014 09:27:59 GMT-0400 (Eastern Daylight Time)

mourner commented 10 years ago

@emcjh it shows you times in your local time zone, you need to convert them according to the time zone of the location if you want precise local times and dates.

emcjh commented 10 years ago

Not sure I understand your comment. My local time zone and the time zone of the location I want to get dawn/dusk times are the same. If I submit a date of Apr 23, 2014 shouldn't I get the dawn time for Apr 23 and dusk time for April 23? Why does dawn show evening of Apr 22 and dusk as morning of Apr 23? Or am I just really missing something.

mourner commented 10 years ago

If your timezones are good, why is sunrise happening at 7:44pm?

emcjh commented 10 years ago

That's exactly what I'm trying to figure out. I called getTimes() with a date of Apr 23 so why is sunrise returned as 7:44pm of Apr 22? Pretty sure I'm using the latest version of suncalc.js as I just downloaded it from here yesterday. Thanks for your assistance with this.

fchristant commented 10 years ago

@emcjh What happens if you do this:

var today = new Date(d.getFullYear(), d.getMonth(), d.getDate(), 12, 0, 0, 0, 0);

emcjh commented 10 years ago

@fchristant Dates and times still appear incorrect. I assumed d in your example is a js date object.

My code: var d = new Date(); var today = new Date(d.getFullYear(), d.getMonth(), d.getDate(), 12, 0, 0, 0, 0); var t = SunCalc.getTimes(today, 34.2, 84.1); console.log(today); console.log(t);

Results in console:

Thu Apr 24 2014 12:00:00 GMT-0400 (Eastern Daylight Time) (index):1844 Object {solarNoon: Thu Apr 24 2014 02:22:55 GMT-0400 (Eastern Daylight Time), nadir: Wed Apr 23 2014 14:22:55 GMT-0400 (Eastern Daylight Time), sunrise: Wed Apr 23 2014 19:43:18 GMT-0400 (Eastern Daylight Time), sunset: Thu Apr 24 2014 09:02:33 GMT-0400 (Eastern Daylight Time), sunriseEnd: Wed Apr 23 2014 19:45:58 GMT-0400 (Eastern Daylight Time)…} dawn: Wed Apr 23 2014 19:17:00 GMT-0400 (Eastern Daylight Time) dusk: Thu Apr 24 2014 09:28:50 GMT-0400 (Eastern Daylight Time) goldenHour: Thu Apr 24 2014 08:28:32 GMT-0400 (Eastern Daylight Time) goldenHourEnd: Wed Apr 23 2014 20:17:19 GMT-0400 (Eastern Daylight Time) nadir: Wed Apr 23 2014 14:22:55 GMT-0400 (Eastern Daylight Time) nauticalDawn: Wed Apr 23 2014 18:45:38 GMT-0400 (Eastern Daylight Time) nauticalDusk: Thu Apr 24 2014 10:00:12 GMT-0400 (Eastern Daylight Time) night: Thu Apr 24 2014 10:32:53 GMT-0400 (Eastern Daylight Time) nightEnd: Wed Apr 23 2014 18:12:57 GMT-0400 (Eastern Daylight Time) solarNoon: Thu Apr 24 2014 02:22:55 GMT-0400 (Eastern Daylight Time) sunrise: Wed Apr 23 2014 19:43:18 GMT-0400 (Eastern Daylight Time) sunriseEnd: Wed Apr 23 2014 19:45:58 GMT-0400 (Eastern Daylight Time) sunset: Thu Apr 24 2014 09:02:33 GMT-0400 (Eastern Daylight Time) sunsetStart: Thu Apr 24 2014 08:59:53 GMT-0400 (Eastern Daylight Time) proto: Object

fchristant commented 10 years ago

hmmm, well at least this means your issue is not related to the issue described at the beginning of the thread, we just excluded that.

jonsadka commented 9 years ago

No issue for me. It just required converting the result times to local timezone. Thanks!

janett-baresel commented 8 years ago

I still experience this issue. Here an example:

// in Sydney this would be 23:59 March 15th
var currentDate = new Date("Sun Mar 15 2015 14:59:00 GMT+0100 (Central Europe Standard Time)");
var latitude = -33.87644639725535;
var longitude = 151.20731600000002;

var dates = SunCalc.getTimes(currentDate, latitude, longitude);

console.debug(dates);

This returns: sunrise: Sun Mar 15 2015 20:55:56 GMT+0100 (Central Europe Standard Time) sunset: Mon Mar 16 2015 09:14:51 GMT+0100 (Central Europe Standard Time)

If you calculate the local time for Sydney this will be: sunrise: Mon Mar 16 2015 05:55:56 GMT+1000 sunset: Mon Mar 16 2015 18:14:51 GMT+1000

This means that for the date and time of 15th of March 23:59 it returns the sunrise and sunset for the following day.

I have tested this with the latest version of the library. It seems to me that the conversion into and from julian could be the reason. But I am not an expert and don't understand the calculations there, so it could be something else as well. The change to 12 o'clock midday, as suggested above, should not be able to fix this, because depending on the local timezone of the computer and the long/lat of the target location this again ends up close to midnight for some locations.

krishnaglick commented 7 years ago

+1 on this issue, currently hacking around it w/ the midday line but would love a fix! const midday = new Date(date.getFullYear(), date.getMonth(), date.getDate(), 12, 0, 0, 0, 0);

Pierre-Gilles commented 7 years ago

+1 I have the same problem in Gladys ( It's a home assistant project => https://github.com/GladysProject/Gladys ).

I calculate the sunrise at midnight, and it calculates for the day before I will use the hack, but would love a fix :)

Thx a lot for this awesome library !