in reply to Trouble with time zones and Sun rise/set

Expected: Rise: 2022-04-01T06:12:04 Set: 2022-04-01T18:34:16 (Pacific/Auckland)

I am wondering where you're getting these from, since they don't seem to match any information I could find. My results below do match those from timeanddate.com. Even if you fiddle with DateTime::Event::Sunrise's altitude parameter as per "Sun Height" in its documentation, I don't get the same times. For Astro::Coords, the only reference to time zones in its documentation is "A copy of the input argument is created, guaranteeing a UTC representation." - so I figured it's best to just work with UTC inside the module, and only convert to local time zone once one gets the results. I think the slight deviation between the two modules could be because Astro::Coords takes the altitude into account, or some other minor inaccuracy.

use strict; use warnings; use Astro::Coords; use Astro::Telescope; use DateTime::Event::Sunrise; use DateTime; my %params = ( year => 2022, month => 4, day => 1, hour => 0, minute => 1, second => 0, time_zone => 'Pacific/Auckland' ); my $obsDate = DateTime->new(%params); $obsDate->set_time_zone('UTC'); my $tel = Astro::Telescope->new('485'); print "Lat=", $tel->lat('d'), " Lon=", $tel->long('d'), " Alt=", $tel->alt, "\n"; my $cSun = Astro::Coords->new(planet => 'sun'); $cSun->telescope($tel); $cSun->datetime($obsDate); my $sRise = $cSun->rise_time(); my $sSet = $cSun->set_time(); $sRise->set_time_zone('Pacific/Auckland'); $sSet ->set_time_zone('Pacific/Auckland'); print "Rise: ",$sRise->strftime('%FT%T %Z'), " Set: ",$sSet ->strftime('%FT%T %Z'),"\n"; my $sun = DateTime::Event::Sunrise->new( altitude=>-0.833, longitude => $tel->long('d'), latitude => $tel->lat('d') ); $obsDate->add(days=>1); # not sure yet why the two modules differ here my $rise2 = $sun->sunrise_datetime($obsDate); my $set2 = $sun-> sunset_datetime($obsDate); $rise2->set_time_zone('Pacific/Auckland'); $set2 ->set_time_zone('Pacific/Auckland'); print "Rise: ",$rise2->strftime('%FT%T %Z'), " Set: ",$set2 ->strftime('%FT%T %Z'),"\n"; __END__ Lat=-41.2842368910727 Lon=174.7654 Alt=144.005447541833 Rise: 2022-04-01T07:35:56 NZDT Set: 2022-04-01T19:13:17 NZDT Rise: 2022-04-01T07:36:11 NZDT Set: 2022-04-01T19:13:42 NZDT

Update: For Solstice/Equinox, see my script at Re: calculate length of day as function of space at onset of fall.

Replies are listed 'Best First'.
Re^2: Trouble with time zones and Sun rise/set
by ikegami (Patriarch) on Apr 19, 2022 at 14:58 UTC

    not sure yet why the two modules differ here

    Astro::Coords returns the next sunrise/sunset after the provided timestamp.

    DateTime::Event::Sunrise returns the sunrise/sunset for the provided date.

    You should have used

    $obsDate->set_time_zone('Pacific/Auckland');
    instead of
    $obsDate->add(days=>1);
Re^2: Trouble with time zones and Sun rise/set
by GrandFather (Saint) on Apr 22, 2022 at 01:02 UTC

    Most of the time zone mess is due to the evolution of this project. I'm taking over generating a report of visibility of solar system objects for the next month. The person doing this before me used Guide 9 to generate Sun and planet rise/set times and various other details. He manually transcribed various tables then added other events of note. Manually transcribing tables was not appealing so I reached for Perl. I started by parsing the tables and formatting them for a website, then started adding planet and star conjunction detection for the "other details" section. That is when things came unstuck with a mixture of time zones depending on where data originally came from.

    Thanks again for the help.

    Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond
Re^2: Trouble with time zones and Sun rise/set
by GrandFather (Saint) on Apr 19, 2022 at 21:52 UTC

    Thank you! Setting the time zone to UTC for the DateTime given to Astro::Coords fixes the problem. I'll follow up later with a little more detail, but right now I'm about to head out the door on holiday.

    Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond

      Please post revised source. I'm fascinated by the view from "down under," which I have never experienced in real life. I'll see if I can replicate your results.