Open ottopic opened 5 years ago
@gregseth using UTC date time and converting it in current timezone the result it seems to works but only in my current European timezone, if I try with U.S.A latitude and longitude it returns wrong day.
This is the workaround I have found to calculate correct date time, but I don't think it's a good job, someone can help me?
$DateTimeZone = new DateTimeZone($timeZone->timeZoneId);
$DateTimeZoneUTC = new DateTimeZone("UTC");
if(strpos($timeZone->timeZoneId, "Europe") !== false) $DateTimeZone = $DateTimeZoneUTC;
$newSunData = new AurorasLive\SunCalc(new DateTime($date, $DateTimeZone), $lat, $lng);
If I try to calculate sunTimes with:
datetime : 2019-02-09 00:01:00 latitude: 43.7102466075706 longitude: 11.2609452326665
in results I find the day before 2019-02-08
I also faced with this problem. I don't found right fix or workaround for this yet.
I have encountered this same issue and found that modifying this line to read $d = toDays($this->date) + 0.5;
fixes the issue
If you look at the toJulian
function, there 0.5 gets subtracted from the result, I believe that means it removes half a day from the time, and if you're using 00:00 as time from which to calculate the sun/moon times, then remove half a day, the previous day will be selected.
This seems to be working for me, but certainly needs more testing.
Comparing the results this gives me to what timeanddate is telling me, the sun times only have a variation of +/- 5 minutes. Though you cannot add the 0.5 to the moontimes algorithm, as that will mess things up.
This was tested at 5 different coordinates with 20 random dates
@pixlmint
I have encountered this same issue and found that modifying this line to read
$d = toDays($this->date) + 0.5;
fixes the issue
Unfortunately, no. Please, check this comment: https://github.com/mourner/suncalc/issues/107#issuecomment-362287177
Here is my implementation on PHP:
function toJulian($date): float
{
$month = $date->format('m');
$day = $date->format('d');
$year = $date->format('Y');
$gregorian = !($year < 1583);
if ($month == 1 || $month == 2) {
$year -= 1;
$month += 12;
}
$a = floor($year / 100);
if ($gregorian) {
$b = 2 - $a + floor($a / 4);
} else {
$b = 0.0;
}
$jd = floor(365.25 * ($year + 4716))
+ floor(30.6001 * ($month + 1))
+ $day + $b - 1524.5;
return $jd + 0.5;
}
Hello @gregseth , thank you for your great work. I'm testing your script but I have strange problem.
If I try to calculate sunTimes with:
datetime : 2019-02-09 00:01:00 latitude: 43.7102466075706 longitude: 11.2609452326665
in results I find the day before 2019-02-08
I tried to edit your script passing right timezone to "fromJulian" function but never change. Can you help me?