| 1 |
package Time::JulianDay; |
| 2 |
|
| 3 |
require 5.008001; |
| 4 |
|
| 5 |
use Carp; |
| 6 |
use Time::Timezone; |
| 7 |
|
| 8 |
@ISA = qw(Exporter); |
| 9 |
@EXPORT = qw(julian_day inverse_julian_day day_of_week |
| 10 |
jd_secondsgm jd_secondslocal |
| 11 |
jd_timegm jd_timelocal |
| 12 |
gm_julian_day local_julian_day |
| 13 |
); |
| 14 |
@EXPORT_OK = qw($brit_jd); |
| 15 |
|
| 16 |
use strict; |
| 17 |
use integer; |
| 18 |
|
| 19 |
# constants |
| 20 |
use vars qw($brit_jd $jd_epoch $jd_epoch_remainder $VERSION); |
| 21 |
|
| 22 |
$VERSION = 2026.0219; |
| 23 |
|
| 24 |
# calculate the julian day, given $year, $month and $day |
| 25 |
sub julian_day |
| 26 |
{ |
| 27 |
my($year, $month, $day) = @_; |
| 28 |
my($tmp); |
| 29 |
|
| 30 |
use Carp; |
| 31 |
# confess() unless defined $day; |
| 32 |
|
| 33 |
$tmp = $day - 32075 |
| 34 |
+ 1461 * ( $year + 4800 - ( 14 - $month ) / 12 )/4 |
| 35 |
+ 367 * ( $month - 2 + ( ( 14 - $month ) / 12 ) * 12 ) / 12 |
| 36 |
- 3 * ( ( $year + 4900 - ( 14 - $month ) / 12 ) / 100 ) / 4 |
| 37 |
; |
| 38 |
|
| 39 |
return($tmp); |
| 40 |
|
| 41 |
} |
| 42 |
|
| 43 |
sub gm_julian_day |
| 44 |
{ |
| 45 |
my($secs) = @_; |
| 46 |
my($sec, $min, $hour, $mon, $year, $day, $month); |
| 47 |
($sec, $min, $hour, $day, $mon, $year) = gmtime($secs); |
| 48 |
$month = $mon + 1; |
| 49 |
$year += 1900; |
| 50 |
return julian_day($year, $month, $day) |
| 51 |
} |
| 52 |
|
| 53 |
sub local_julian_day |
| 54 |
{ |
| 55 |
my($secs) = @_; |
| 56 |
my($sec, $min, $hour, $mon, $year, $day, $month); |
| 57 |
($sec, $min, $hour, $day, $mon, $year) = localtime($secs); |
| 58 |
$month = $mon + 1; |
| 59 |
$year += 1900; |
| 60 |
return julian_day($year, $month, $day) |
| 61 |
} |
| 62 |
|
| 63 |
sub day_of_week |
| 64 |
{ |
| 65 |
my ($jd) = @_; |
| 66 |
return (($jd + 1) % 7); # calculate weekday (0=Sun,6=Sat) |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
# The following defines the first day that the Gregorian calendar was used |
| 71 |
# in the British Empire (Sep 14, 1752). The previous day was Sep 2, 1752 |
| 72 |
# by the Julian Calendar. The year began at March 25th before this date. |
| 73 |
|
| 74 |
$brit_jd = 2361222; |
| 75 |
|
| 76 |
# Usage: ($year,$month,$day) = &inverse_julian_day($julian_day) |
| 77 |
sub inverse_julian_day |
| 78 |
{ |
| 79 |
my($jd) = @_; |
| 80 |
my($jdate_tmp); |
| 81 |
my($m,$d,$y); |
| 82 |
|
| 83 |
carp("warning: julian date $jd pre-dates British use of Gregorian calendar\n") |
| 84 |
if ($jd < $brit_jd); |
| 85 |
|
| 86 |
$jdate_tmp = $jd - 1721119; |
| 87 |
$y = (4 * $jdate_tmp - 1)/146097; |
| 88 |
$jdate_tmp = 4 * $jdate_tmp - 1 - 146097 * $y; |
| 89 |
$d = $jdate_tmp/4; |
| 90 |
$jdate_tmp = (4 * $d + 3)/1461; |
| 91 |
$d = 4 * $d + 3 - 1461 * $jdate_tmp; |
| 92 |
$d = ($d + 4)/4; |
| 93 |
$m = (5 * $d - 3)/153; |
| 94 |
$d = 5 * $d - 3 - 153 * $m; |
| 95 |
$d = ($d + 5) / 5; |
| 96 |
$y = 100 * $y + $jdate_tmp; |
| 97 |
if($m < 10) { |
| 98 |
$m += 3; |
| 99 |
} else { |
| 100 |
$m -= 9; |
| 101 |
++$y; |
| 102 |
} |
| 103 |
return ($y, $m, $d); |
| 104 |
} |
| 105 |
|
| 106 |
{ |
| 107 |
my($sec, $min, $hour, $day, $mon, $year) = gmtime(0); |
| 108 |
$year += 1900; |
| 109 |
if ($year == 1970 && $mon == 0 && $day == 1) { |
| 110 |
# standard unix time format |
| 111 |
$jd_epoch = 2440588; |
| 112 |
} else { |
| 113 |
$jd_epoch = julian_day($year, $mon+1, $day); |
| 114 |
} |
| 115 |
$jd_epoch_remainder = $hour*3600 + $min*60 + $sec; |
| 116 |
} |
| 117 |
|
| 118 |
sub jd_secondsgm |
| 119 |
{ |
| 120 |
my($jd, $hr, $min, $sec) = @_; |
| 121 |
|
| 122 |
my($r) = (($jd - $jd_epoch) * 86400 |
| 123 |
+ $hr * 3600 + $min * 60 |
| 124 |
- $jd_epoch_remainder); |
| 125 |
|
| 126 |
no integer; |
| 127 |
return ($r + $sec); |
| 128 |
use integer; |
| 129 |
} |
| 130 |
|
| 131 |
sub jd_secondslocal |
| 132 |
{ |
| 133 |
my($jd, $hr, $min, $sec) = @_; |
| 134 |
my $jds = jd_secondsgm($jd, $hr, $min, $sec); |
| 135 |
return $jds - tz_local_offset($jds); |
| 136 |
} |
| 137 |
|
| 138 |
# this uses a 0-11 month to correctly reverse localtime() |
| 139 |
sub jd_timelocal |
| 140 |
{ |
| 141 |
my ($sec,$min,$hours,$mday,$mon,$year) = @_; |
| 142 |
$year += 1900 unless $year > 1000; |
| 143 |
my $jd = julian_day($year, $mon+1, $mday); |
| 144 |
my $jds = jd_secondsgm($jd, $hours, $min, $sec); |
| 145 |
return $jds - tz_local_offset($jds); |
| 146 |
} |
| 147 |
|
| 148 |
# this uses a 0-11 month to correctly reverse gmtime() |
| 149 |
sub jd_timegm |
| 150 |
{ |
| 151 |
my ($sec,$min,$hours,$mday,$mon,$year) = @_; |
| 152 |
$year += 1900 unless $year > 1000; |
| 153 |
my $jd = julian_day($year, $mon+1, $mday); |
| 154 |
return jd_secondsgm($jd, $hours, $min, $sec); |
| 155 |
} |
| 156 |
|
| 157 |
1; |
| 158 |
|
| 159 |
__END__ |
| 160 |
|
| 161 |
=head1 NAME |
| 162 |
|
| 163 |
Time::JulianDay -- Julian calendar manipulations |
| 164 |
|
| 165 |
=head1 SYNOPSIS |
| 166 |
|
| 167 |
use Time::JulianDay |
| 168 |
|
| 169 |
$jd = julian_day($year, $month_1_to_12, $day) |
| 170 |
$jd = local_julian_day($seconds_since_1970); |
| 171 |
$jd = gm_julian_day($seconds_since_1970); |
| 172 |
($year, $month_1_to_12, $day) = inverse_julian_day($jd) |
| 173 |
$dow = day_of_week($jd) |
| 174 |
|
| 175 |
print (Sun,Mon,Tue,Wed,Thu,Fri,Sat)[$dow]; |
| 176 |
|
| 177 |
$seconds_since_jan_1_1970 = jd_secondslocal($jd, $hour, $min, $sec) |
| 178 |
$seconds_since_jan_1_1970 = jd_secondsgm($jd, $hour, $min, $sec) |
| 179 |
$seconds_since_jan_1_1970 = jd_timelocal($sec,$min,$hours,$mday,$month_0_to_11,$year) |
| 180 |
$seconds_since_jan_1_1970 = jd_timegm($sec,$min,$hours,$mday,$month_0_to_11,$year) |
| 181 |
|
| 182 |
=head1 DESCRIPTION |
| 183 |
|
| 184 |
JulianDay is a package that manipulates dates as number of days since |
| 185 |
some time a long time ago. It's easy to add and subtract time |
| 186 |
using julian days... |
| 187 |
|
| 188 |
The day_of_week returned by day_of_week() is 0 for Sunday, and 6 for |
| 189 |
Saturday and everything else is in between. |
| 190 |
|
| 191 |
=head1 ERRATA |
| 192 |
|
| 193 |
Time::JulianDay is not a correct implementation. There are two |
| 194 |
problems. The first problem is that Time::JulianDay only works |
| 195 |
with integers. Julian Day can be fractional to represent time |
| 196 |
within a day. If you call inverse_julian_day() with a non-integer |
| 197 |
time, it will often give you an incorrect result. |
| 198 |
|
| 199 |
The second problem is that Julian Days start at noon rather than |
| 200 |
midnight. The julian_day() function returns results that are too |
| 201 |
large by 0.5. |
| 202 |
|
| 203 |
What to do about these problems is currently open for debate. I'm |
| 204 |
tempted to leave the current functions alone and add a second set |
| 205 |
with more accurate behavior. |
| 206 |
|
| 207 |
There is another implementation in Astro::Time that may be more accurate. |
| 208 |
|
| 209 |
=head1 AUTHOR |
| 210 |
|
| 211 |
Best Practical Solutions, LLC E<lt>[email protected]<gt> |
| 212 |
|
| 213 |
=head1 ORIGINAL AUTHOR |
| 214 |
|
| 215 |
David Muir Sharnoff |
| 216 |
|
| 217 |
=head1 GENESIS |
| 218 |
|
| 219 |
Based on previous work by |
| 220 |
Kurt Jaeger, Ian Miller, Gary Puckering (based on Collected Algorithms |
| 221 |
of the ACM), and the author of Time::Local. |
| 222 |
|
| 223 |
=head1 LICENSE AND COPYRIGHT |
| 224 |
|
| 225 |
Copyright (C) 1996-1999 David Muir Sharnoff. |
| 226 |
Copyright (C) 2026 Best Practical Solutions, LLC. |
| 227 |
|
| 228 |
This library is free software; you can redistribute it and/or modify |
| 229 |
it under the same terms as Perl itself. |
| 230 |
|