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.
In reply to Re: Trouble with time zones and Sun rise/set
by haukex
in thread Trouble with time zones and Sun rise/set
by GrandFather
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |