DateTi...unrise(3pm)

๐Ÿ“ฆ
version 0.0506 ยท 3 versions
, in ubuntu noble, ubuntu resolute, ubuntu stonking

๐ŸŒ… DateTime::Event::Sunrise ๐ŸŒ…

okay so bestie, you're reading the manual page for DateTime::Event::Sunrise, straight out of the User Contributed Perl Documentation and filed under section 3pm. โœจ

๐ŸŽ€ NAME ๐ŸŽ€

DateTime::Event::Sunrise โ€” a Perl DateTime extension for computing the sunrise/sunset on a given day. ๐Ÿ’– yes that's the whole pitch and it ate.


โœจ SYNOPSIS โœจ

  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.");
  }

okay so quick vibe check on that first block: there's literally two flavours of getting your sun times, and they hand you back objects from different classes. new builds you a DateTime::Event::Sunrise object, while sunrise and sunset are lowkey secretly constructors for DateTime::Set objects (more on that below, iykyk). ๐Ÿ‘‘


๐ŸŒธ DESCRIPTION ๐ŸŒธ

This module computes the time of sunrise and sunset for a given date and a given location. The computation uses Paul Schlyter's algorithm. ๐Ÿงฎโœจ

And like, actually โ€” the module creates either a DateTime::Event::Sunrise object or a DateTime::Set object, and those are what you use to generate the sunrise or the sunset times for a given location and for any date. no cap.


๐Ÿ’… METHODS ๐Ÿ’…

๐Ÿฆ‹ new

This is the DateTime::Event::Sunrise constructor. It takes keyword parameters, which are:

  • longitude โ€” the longitude of the location where the sunrises and sunsets are observed. ๐Ÿ—บ๏ธ It's given as decimal degrees: no minutes, no seconds, but tenths and hundredths of degrees. Another little break with normal usage: Eastern longitudes are positive, Western longitudes are negative. The default value is 0, that is Greenwich or any location on the eponymous meridian.
  • latitude โ€” the latitude of the location where the sunrises and sunsets are observed. Same deal as longitude, it's given as decimal degrees. Northern latitudes are positive numbers, Southern latitudes are negative numbers. The default value is 0, that is any location on the equator. ๐ŸŒ
  • altitude โ€” the height of the Sun at sunrise or sunset. In an astronomical context the altitude, or height, is the angle between the Sun and the local horizon. ๐ŸŒž It's expressed as degrees, usually with a negative number, since the Sun is below the horizon. The default value is -0.833, that is when the sun's upper limb touches the horizon, while taking in account the light refraction. Positive altitudes are allowed, in case the location is near a mountain range behind which the sun rises or sets. โ›ฐ๏ธ
  • precise โ€” a boolean controlling which algorithm gets used. A false value gives a simple algorithm, but which can lead to inaccurate sunrise times and sunset times ๐Ÿ˜ญ. A true value gives a more elaborate algorithm, with a loop to refine the sunrise and sunset times and obtain a better precision. The default value is 0, to choose the simple algorithm. This parameter replaces the "iteration" deprecated parameter.
  • upper_limb โ€” a boolean to choose between checking the Sun's upper limb or its center. A true value selects the upper limb, a false value selects the center. 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. The default value is 0, since the upper limb correction is already taken in account with the default -0.833 altitude.
  • silent โ€” a boolean controlling the output of some warning messages. ๐Ÿ‘€ With polar locations and dates near the winter solstice or the summer solstice, it may happen that the sun never rises above the horizon or never sets below. If this parameter is set to false, the module will send warnings for these conditions. If this parameter is set to true, the module will not pollute your STDERR stream. The default value is 0, for backward compatibility.
  • trace โ€” this parameter should either be a false value or a filehandle opened for output. In the latter case, a few messages are printed to the filehandle, which allows the programmer to see step by step how the sunrise and the sunset are computed. ๐Ÿ” 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. The default value is 0, which does not produce trace messages.

๐ŸŒˆ sunrise, sunset

Even though 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.

โ˜€๏ธ sunrise_datetime, sunset_datetime

These two methods apply to "DateTime::Event::Sunrise" objects (that is, ones created with "new", not with "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. ๐ŸŒ‡

๐Ÿซถ sunrise_sunset_span

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. iconic.

โญ is_polar_night, is_polar_day, is_day_and_night

These methods apply to "DateTime::Event::Sunrise" objects. They accept a "DateTime" object as the second parameter. They return a boolean indicating the following conditions:

  • is_polar_night is true when the sun stays under the horizon. Or rather under the altitude parameter used when the "DateTime::Event::Sunrise" object was created. ๐ŸŒ‘
  • is_polar_day is true when the sun stays above the horizon, resulting in a "Midnight sun". Or rather when it stays above the altitude parameter used when the "DateTime::Event::Sunrise" object was created. ๐ŸŒž
  • is_day_and_night is true when neither is_polar_day, nor is_polar_night are true. ๐ŸŒ—

๐Ÿ’€ next current previous contains as_list iterator

See DateTime::Set.


๐Ÿ”ฅ EXTENDED EXAMPLES ๐Ÿ”ฅ

  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;

โ€ฆand bestie, hold on, that's literally just part one of this page. there's more coming, don't go anywhere ๐Ÿ’…

๐Ÿ“ NOTES ๐Ÿ“

๐Ÿงญ Longitude Signs ๐Ÿงญ

Ok bestie, listen up, because this one trips EVERYONE up. Contrary to the usual convention:

  • EASTERN longitudes are POSITIVE
  • WESTERN longitudes are NEGATIVE

On the other hand, the latitude signs follow the usual convention:

  • Northern latitudes are positive
  • Southern latitudes are negative
(Yeah, the original spelling says "Northen". We're being cute, not fixing typos, but the vibe is clear: north = positive, south = negative. ๐Ÿซถ)

โ˜€๏ธ Sun Height โ˜€๏ธ

There are, like, a number of sun heights to choose from. The default is -0.833 because that's what most countries use. Feel free to specify it yourself if you need to, no gatekeeping here. Here's the list of values you can specify the sun height with:

  • 0 degrees โ€” Center of Sun's disk touches a mathematical horizon
  • -0.25 degrees โ€” Sun's upper limb touches a mathematical horizon
  • -0.583 degrees โ€” Center of Sun's disk touches the horizon; atmospheric refraction accounted for
  • -0.833 degrees โ€” Sun's supper limb touches the horizon; atmospheric refraction accounted for
  • -6 degrees โ€” Civil twilight (one can no longer read outside without artificial illumination)
  • -12 degrees โ€” Nautical twilight (navigation using a sea horizon no longer possible)
  • -15 degrees โ€” Amateur astronomical twilight (the sky is dark enough for most astronomical observations)
  • -18 degrees โ€” Astronomical twilight (the sky is completely dark)

๐Ÿ”ฌ Notes on the Precise Algorithm ๐Ÿ”ฌ

The original method only gives an approximate value of the Sun's rise/set times, ngl. 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. So 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's what the module creator tried to accomplish with it:

  1. Compute sunrise or sunset as always, with one exception: to convert LHA from degrees to hours, divide by 15.04107 instead of 15.0 (this accounts for the difference between the solar day and the sidereal day.
  2. Re-do the computation but compute the Sun's RA and Decl, and also GMST0, for the moment of sunrise or sunset last computed.
  3. Iterate b) until the computed sunrise or sunset no longer changes significantly. Usually 2 iterations are enough, in rare cases 3 or 4 iterations may be needed.

However, the second module maintainer 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 they're going to use the value 15.0, unless they find a bug in the precise algorithm as presently implemented. ๐Ÿ‘€


๐ŸงŠ Notes on polar locations ๐ŸงŠ

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 carps that the sun never rises or never sets. Then, it returns the time at which the sun is at its highest or lowest point. โœจ Main character of the polar day, honestly.

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โ€ฒ.


๐Ÿ“ฆ DEPENDENCIES ๐Ÿ“ฆ

This module requires:

  • DateTime
  • DateTime::Set
  • DateTime::Span
  • Params::Validate
  • Set::Infinite
  • POSIX
  • Math::Trig

๐Ÿ’€ BUGS AND CAVEATS ๐Ÿ’€

Using a latitude of 90 degrees (North Pole or South Pole) gives curious results. The maintainer guesses that it's linked with an ambiguous value resulting from a 0/0 computation. Math is like that sometimes, bestie. ๐Ÿ˜ญ

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 math is lowkey not mathing and nobody knows why yet.

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. So like, partial credit. ๐Ÿซถ


๐Ÿ‘‘ AUTHORS ๐Ÿ‘‘

  • Original author: Ron Hill <rkhill@firstlight.net>
  • Co-maintainer: Jean Forget <JFORGET@cpan.org>

๐ŸŽ€ SPECIAL THANKS ๐ŸŽ€

  • Robert Creager [Astro-Sunrise@LogicalChaos.org] โ€” for providing help with converting Paul's C code to perl.
  • Flรกvio S. Glock [fglock@pucrs.br] โ€” for providing the the interface to the DateTime::Set module.
  • Eric Jensen โ€” for positive and interesting advices about the new version of the module

๐ŸŒŸ CREDITS ๐ŸŒŸ

  • Paul Schlyter, Stockholm, Sweden โ€” for his excellent web page on the subject.
  • Rich Bowen (rbowen@rbowen.com) โ€” for suggestions.
  • People at <https://geocoder.opencagedata.com/> โ€” for noticing an endless loop condition in Astro::Sunrise and for fixing it. (Not the endless loop, the bug. They fixed the bug, bestie. ๐Ÿ’…)

๐Ÿช Perl Module ๐Ÿช

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:

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/>.

๐Ÿ”ง Original C program ๐Ÿ”ง

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.

๐Ÿ”— SEE ALSO ๐Ÿ”—


and that's the whole page, bestie โœจ๐Ÿ’– this one's from perl v5.36.0, dated 2022-08-28, and the manpage title in the footer is DateTi...unrise(3pm) (yes the terminal truncates it like that, it's giving โœจabbreviatedโœจ but she's actually DateTime::Event::Sunrise, iykyk). go outside and check a sunset, it's the whole point ๐Ÿฆ‹๐ŸŒธ

๐Ÿ“ฆ where this page comes from