DateTime::Event::Sunrise(3pm) | User Contributed Perl Documentation | DateTime::Event::Sunrise(3pm) |
DateTime::Event::Sunrise - Perl DateTime extension for computing the sunrise/sunset on a given day
use DateTime; use DateTime::Event::Sunrise; # generating DateTime objects from a DateTime::Event::Sunrise object my $sun_Kyiv = DateTime::Event::Sunrise->new(longitude => +30.85, # 30°51'E latitude => +50.45); # 50°27'N for (12, 13, 14) { my $dt_yapc_eu = DateTime->new(year => 2013, month => 8, day => $_, time_zone => 'Europe/Kiev'); say "In Kyiv (50°27'N, 30°51'E) on ", $dt_yapc_eu->ymd, " sunrise occurs at ", $sun_Kyiv->sunrise_datetime($dt_yapc_eu)->hms, " and sunset occurs at ", $sun_Kyiv->sunset_datetime ($dt_yapc_eu)->hms; } # generating DateTime objects from DateTime::Set objects my $sunrise_Austin = DateTime::Event::Sunrise->sunrise(longitude => -94.73, # 97°44'W latitude => +30.3); # 30°18'N my $sunset_Austin = DateTime::Event::Sunrise->sunset (longitude => -94.73, latitude => +30.3); my $dt_yapc_na_rise = DateTime->new(year => 2013, month => 6, day => 3, time_zone => 'America/Chicago'); my $dt_yapc_na_set = $dt_yapc_na_rise->clone; say "In Austin (30°18'N, 97°44'W), sunrises and sunsets are"; for (1..3) { $dt_yapc_na_rise = $sunrise_Austin->next($dt_yapc_na_rise); $dt_yapc_na_set = $sunset_Austin ->next($dt_yapc_na_set); say $dt_yapc_na_rise, ' ', $dt_yapc_na_set; } # If you deal with a polar location my $sun_in_Halley = DateTime::Event::Sunrise->new( longitude => -26.65, # 26°39'W latitude => -75.58, # 75°35'S precise => 1, ); my $Alex_arrival = DateTime->new(year => 2006, # approximate date, not necessarily the exact one month => 1, day => 15, time_zone => 'Antarctica/Rothera'); say $Alex_arrival->strftime("Alex Gough (a Perl programmer) arrived at Halley Base on %Y-%m-%d."); if ($sun_in_Halley->is_polar_day($Alex_arrival)) { say "It would be days, maybe weeks, before the sun would set."; } elsif ($sun_in_Halley->is_polar_night($Alex_arrival)) { say "It would be days, maybe weeks, before the sun would rise."; } else { my $sunset = $sun_in_Halley->sunset_datetime($Alex_arrival); say $sunset->strftime("And he saw his first antarctic sunset at %H:%M:%S."); }
This module will computes the time of sunrise and sunset for a given date and a given location. The computation uses Paul Schlyter's algorithm.
Actually, the module creates a DateTime::Event::Sunrise object or a DateTime::Set object, which are used to generate the sunrise or the sunset times for a given location and for any date.
This is the DateTime::Event::Sunrise constructor. It takes keyword parameters, which are:
Default value is 0, that is Greenwich or any location on the eponymous meridian.
Default value is 0, that is any location on the equator.
Default value is -0.833, that is when the sun's upper limb touches the horizon, while taking in account the light refraction.
Positive altitude are allowed, in case the location is near a mountain range behind which the sun rises or sets.
Default value is 0, to choose the simple algorithm.
This parameter replaces the "iteration" deprecated parameter.
This parameter is significant only when the altitude does not already deal with the sun radius. When the altitude takes into account the sun radius, this parameter should be false.
Default value is 0, since the upper limb correction is already taken in account with the default -0.833 altitude.
Default value is 0, for backward compatibility.
Used for analysis and debugging purposes. You need to read the text doc/astronomical-notes.pod in the sister module Astro::Sunrise to understand what the traced values represent.
Default value is 0, which does not produce trace messages.
Although they come from the DateTime::Event::Sunrise module, these methods are "DateTime::Set" constructors. They use the same parameters as the "new" constructor, but they give objects from a different class.
These two methods apply to "DateTime::Event::Sunrise" objects (that is, created with "new", not "sunrise" or "sunset"). They receive one parameter in addition to $self, a "DateTime" object. They return another "DateTime" object, for the same day, but with the time of the sunrise or sunset, respectively.
This method applies to "DateTime::Event::Sunrise" objects. It accepts a "DateTime" object as the second parameter. It returns a "DateTime::Span" object, beginning at sunrise and ending at sunset.
These methods apply to "DateTime::Event::Sunrise" objects. They accept a "DateTime" object as the second parameter. They return a boolean indicating the following condutions:
See DateTime::Set.
my $dt = DateTime->new( year => 2000, month => 6, day => 20, ); my $sunrise = DateTime::Event::Sunrise ->sunrise ( longitude =>'-118', latitude =>'33', altitude => '-0.833', precise => '1' ); my $sunset = DateTime::Event::Sunrise ->sunset ( longitude =>'-118', latitude =>'33', altitude => '-0.833', precise => '1' ); my $tmp_rise = $sunrise->next( $dt ); my $dt2 = DateTime->new( year => 2000, month => 12, day => 31, ); # iterator my $dt_span = DateTime::Span->new( start =>$dt, end=>$dt2 ); my $set = $sunrise->intersection($dt_span); my $iter = $set->iterator; while ( my $dt = $iter->next ) { print ' ',$dt->datetime; } # is it day or night? my $day_set = DateTime::SpanSet->from_sets( start_set => $sunrise, end_set => $sunset ); print $day_set->contains( $dt ) ? 'day' : 'night'; my $dt = DateTime->new( year => 2000, month => 6, day => 20, time_zone => 'America/Los_Angeles', ); my $sunrise = DateTime::Event::Sunrise ->new( longitude =>'-118' , latitude => '33', altitude => '-0.833', precise => '1' ); my $tmp = $sunrise->sunrise_sunset_span($dt); print "Sunrise is:" , $tmp->start->datetime , "\n"; print "Sunset is:" , $tmp->end->datetime;
Remember, contrary to the usual convention,
EASTERN longitudes are POSITIVE,
WESTERN longitudes are NEGATIVE.
On the other hand, the latitude signs follow the usual convention:
Northen latitudes are positive,
Southern latitudes are negative.
There are a number of sun heights to choose from. The default is -0.833 because this is what most countries use. Feel free to specify it if you need to. Here is the list of values to specify the sun height with:
Center of Sun's disk touches a mathematical horizon
Sun's upper limb touches a mathematical horizon
Center of Sun's disk touches the horizon; atmospheric refraction accounted for
Sun's supper limb touches the horizon; atmospheric refraction accounted for
Civil twilight (one can no longer read outside without artificial illumination)
Nautical twilight (navigation using a sea horizon no longer possible)
Amateur astronomical twilight (the sky is dark enough for most astronomical observations)
Astronomical twilight (the sky is completely dark)
The original method only gives an approximate value of the Sun's rise/set times. The error rarely exceeds one or two minutes, but at high latitudes, when the Midnight Sun soon will start or just has ended, the errors may be much larger. If you want higher accuracy, you must then select the precise variant of the algorithm. This feature is new as of version 0.7. Here is what I (module creator) have tried to accomplish with this.
However, I (second module maintainer) have checked with a few external sources, to obtain test data. And actually, using the value 15.0 gives results closer to what Stellarium and the NOAA solar calculator give. So I will use value 15.0, unless I find a bug in the precise algorithm as presently implemented.
If the location is beyond either polar circle, and if the date is near either solstice, there can be midnight sun or polar night. In this case, there is neither sunrise nor sunset, and the module "carp"s that the sun never rises or never sets. Then, it returns the time at which the sun is at its highest or lowest point.
When computing twilights instead of sunrises / sunsets, the limit for polar locations extends a little beyond the polar circle. For example, for nautical twilights (12 degrees below the horizon), the limits where midnight sun might happen is 12 degrees southward of the Arctic Circle and 12 degrees northward of the Antarctic Circle, that is, about 54° latitude instead of 66°33′.
This module requires:
Using a latitude of 90 degrees (North Pole or South Pole) gives curious results. I guess that it is linked with a ambiguous value resulting from a 0/0 computation.
Using a longitude of 177 degrees, or any longitude near the 180 meridian, may also give curious results, especially with the precise algorithm.
The precise algorithm should be thoroughly analysed, to understand why the value 15.04107 advised by Paul Schlyter does not give the expected results.
The precise algorithm is not tested with polar locations. At least, it is tested with a near-polar location, Fairbanks, at the time when the night is at its shortest, that is, in June.
Original author: Ron Hill <rkhill@firstlight.net>
Co-maintainer: Jean Forget <JFORGET@cpan.org>
This program is distributed under the same terms as Perl 5.16.3: GNU Public License version 1 or later and Perl Artistic License
You can find the text of the licenses in the LICENSE file or at <https://dev.perl.org/licenses/artistic.html> and <https://www.gnu.org/licenses/gpl-1.0.html>.
Here is the summary of GPL:
This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 1, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., <https://www.fsf.org/>.
Here is the copyright information provided by Paul Schlyter for the original C program:
Written as DAYLEN.C, 1989-08-16
Modified to SUNRISET.C, 1992-12-01
(c) Paul Schlyter, 1989, 1992
Released to the public domain by Paul Schlyter, December 1992
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
DateTime Web page at <http://datetime.perl.org/>
DateTime::Set
DateTime::SpanSet
Astro::Sunrise
DateTime::Event::Jewish::Sunrise
Astro::Coords
Astro::PAL
Paul Schlyter's homepage at <https://stjarnhimlen.se/english.html>
The NOAA solar calculator at <https://www.esrl.noaa.gov/gmd/grad/solcalc/>
2022-08-28 | perl v5.34.0 |