-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathTime.pm
448 lines (288 loc) · 10.2 KB
/
Time.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
package Astro::Montenbruck::Time;
use warnings;
use strict;
our $VERSION = 0.01;
use Exporter qw/import/;
our $SEC_PER_DAY = 86400; # Seconds per day
our $SEC_PER_CEN = 3155760000;
our $J2000 = 2451545; # Standard Julian Date for 1.1.2000 12:00
our $J1900 = 2415020
; # Standard Julian Date for 31.12.1899 12:00 (astronomical epoch 1900.0)
our $SOLAR_TO_SIDEREAL = 1.002737909350795
; # Difference in between Sidereal and Solar hour (the former is shorter)
our $GREGORIAN_START = 15821004; # Start of Gregorian calendar (YYYYMMDD)
our $JD_UNIX_EPOCH = _gmtime2jd( gmtime(0) )
; # Standard Julian date for the beginning of Unix epoch, Jan 1 1970 on most Unix systems
our %EXPORT_TAGS = (
all => [
qw/jd_cent after_gregorian cal2jd jd2cal jd0 unix2jd jd2mjd mjd2jd
jd2unix jdnow t1900 jd2gst jd2lst
is_leapyear day_of_year
$SEC_PER_DAY $SEC_PER_CEN $J2000 $J1900 $GREGORIAN_START $JD_UNIX_EPOCH/
],
);
our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
use POSIX;
use Astro::Montenbruck::MathUtils qw/polynome ddd frac to_range/;
sub after_gregorian {
my $y = shift;
my $m = shift;
my $d = shift;
my %arg = ( gregorian_start => $GREGORIAN_START, @_ );
return 0 unless defined $arg{gregorian_start};
polynome( 100, $d, $m, $y ) >= $arg{gregorian_start};
}
sub cal2jd {
my $ye = shift;
my $mo = shift;
my $da = shift;
my %arg = ( gregorian_start => $GREGORIAN_START, @_ );
my $j = $da + 1720996.5;
my ( $m, $y ) = ( $mo > 2 ) ? ( $mo, $ye ) : ( $mo + 12, $ye - 1 );
if ( after_gregorian( $ye, $mo, $da, %arg ) ) {
$j += int( $y / 400 ) - int( $y / 100 ) + int( $y / 4 );
}
else {
$j += int( ( $y + 4716 ) / 4 ) - 1181;
}
$j + 365 * $y + floor( 30.6001 * ( $m + 1 ) );
}
sub jd2cal {
my $jd = shift;
my %arg = ( gregorian => 1, @_ );
my ( $f, $i ) = modf( $jd - $J1900 + 0.5 );
if ( $arg{gregorian} && $i > -115860 ) {
my $a = floor( $i / 36524.25 + 9.9835726e-1 ) + 14;
$i += 1 + $a - floor( $a / 4 );
}
my $b = floor( $i / 365.25 + 8.02601e-1 );
my $c = $i - floor( 365.25 * $b + 7.50001e-1 ) + 416;
my $g = floor( $c / 30.6001 );
my $da = $c - floor( 30.6001 * $g ) + $f;
my $mo = $g - ( $g > 13.5 ? 13 : 1 );
my $ye = $b + ( $mo < 2.5 ? 1900 : 1899 );
$ye, $mo, $da;
}
sub jd0 {
my $j = shift;
floor( $j - 0.5 ) + 0.5;
}
sub unix2jd {
$JD_UNIX_EPOCH + $_[0] / $SEC_PER_DAY;
}
sub jd2unix {
int( ( $_[0] - $JD_UNIX_EPOCH ) * $SEC_PER_DAY );
}
sub _gmtime2jd {
cal2jd( $_[5] + 1900, $_[4] + 1, $_[3] + ddd( @_[ 2, 1, 0 ] ) / 24 );
}
sub jdnow {
_gmtime2jd( gmtime() );
}
sub jd2mjd {
$_[0] - $J2000;
}
sub mjd2jd {
$_[0] + $J2000;
}
# converts Julian date to period in centuries since epoch
# Arguments:
# julian date
# julian date corresponding to the epoch start
sub _t {
my ( $jd, $epoch ) = @_;
( $jd - $epoch ) / 36525;
}
sub jd_cent {
_t( $_[0], $J2000 );
}
sub t1900 {
_t( $_[0], $J1900 );
}
sub jd2gst {
my $jh = shift;
my $j0 = jd0($jh);
my $s0 = polynome( t1900($j0), 0.276919398, 100.0021359, 0.000001075 );
24 * ( frac($s0) + abs( $jh - $j0 ) * $SOLAR_TO_SIDEREAL );
}
sub jd2lst {
my ( $jd, $lon ) = @_;
$lon //= 0;
to_range( jd2gst($jd) - $lon / 15, 24 );
}
sub is_leapyear {
my $yr = shift;
my %arg = ( gregorian => 1, @_ );
$yr = int($yr);
return $arg{gregorian}
? ( $yr % 4 == 0 ) && ( ( $yr % 100 != 0 ) || ( $yr % 400 == 0 ) )
: $yr % 4 == 0;
}
sub day_of_year {
my $yr = shift;
my $mo = shift;
my $dy = shift;
my $k = is_leapyear($yr, @_) ? 1 : 2;
$dy = int($dy);
int(275 * $mo / 9.0) - ($k * int(($mo + 9) / 12.0)) + $dy - 30
}
1;
__END__
=pod
=encoding UTF-8
=head1 NAME
Astro::Montenbruck::Time - Time-related routines
=head1 VERSION
Version 0.01
=head1 SYNOPSIS
use Astro::Montenbruck::Time qw/:all/;
# Convert Gregorian (new-style) date to old-style date
my $j = cal2jd(1799, 6, 6); # Standard Julian date of A.Pushkin's birthday
my $d = jd2cal($j, gregorian => 0) # (1799, 5, 25) = May 26, 1799.
# Julian date in centuries since epoch 2000.0
my $t = jd_cent($j); # -2.0056810403833
...
=head1 DESCRIPTION
Library of date/time manipulation routines for practical astronomy. Most of them
are based on so called I<Julian date (JD)>, which is the number of days elapsed
since mean UT noon of B<January 1st 4713 BC>. This system of time measurement is
widely adopted by the astronomers.
=head2 JD and MJD
Many routines use Modified Julian date, which starts at B<2000 January 0>
(2000 January 1.0) as the starting point.
=head2 Civil year vs astronomical year
There is disagreement between astronomers and historians about how to count the
years preceding the year 1. Astronomers generally use zero-based system. The
year before the year +1, is the year zero, and the year preceding the latter is
the year -1. The year which the historians call 585 B.C. is actually the year
-584.
In this module all subroutines accepting year assume that B<there is year zero>.
Thus, the sequence of years is: C<BC -3, -2, -1, 0, 1, 2, 3, AD>.
=head2 Date and Time
Time is represented by fractional part of a day. For example, 7h30m UT
is C<(7 + 30 / 60) / 24 = 0.3125>.
=head3 Gregorian calendar
I<Civil calendar> in most cases means I<proleptic Gregorian calendar>. it is
assumed that Gregorian calendar started at Oct. 4, 1582, when it was first
adopted in several European countries. Many other countries still used the older
Julian calendar. In Soviet Russia, for instance, Gregorian system was accepted
on Jan 26, 1918. See:
L<https://en.wikipedia.org/wiki/Gregorian_calendar#Adoption_of_the_Gregorian_Calendar>
=head1 EXPORTED CONSTANTS
=over
=item * C<$SEC_PER_DAY> seconds per day (86400)
=item * C<$SEC_PER_CEN> seconds per century (3155760000)
=item * C<$J2000> Standard Julian date for start of epoch 2000,0 (2451545)
=item * C<$J1900> Standard Julian date for start of epoch 1900,0 (2415020)
=item * C<$GREGORIAN_START> Start of Gregorian calendar, YYYYMMDD (15821004)
=item * C<$JD_UNIX_EPOCH> Standard Julian date for start of the Unix epoch
=back
=head1 EXPORTED FUNCTIONS
=over
=item * L</jd_cent($jd)>
=item * L</after_gregorian($year, $month, $date)>
=item * L</cal2jd($year, $month, $date)>
=item * L</jd2cal($jd)>
=item * L</jd0($jd)>
=item * L</unix2jd($seconds)>
=item * L</jd2unix($jd)>
=item * L</jdnow()>
=item * L</jd2mjd($jd)>
=item * L</mjd2jd($mjd)>
=item * L</jd_cent($jd)>
=item * L</t1900($jd)>
=item * L</jd2dt($jd)>
=item * L</jd2te($jd)>
=item * L</jd2gst($jd)>
=item * L</jd2lst($jd, $lng)>
=back
=head1 FUNCTIONS
=head2 jd_cent($jd)
Convert Standard Julian Date to centuries passed since epoch 2000.0
=head2 after_gregorian($year, $month, $date, gregorian_start => $YYYYMMDD )
Does the given date fall to period after Gregorian calendar?
=head3 Positional Arguments
=over
=item * B<year> (astronomic, zero-based)
=item * B<month> (1-12)
=item * B<date> UTC date (1-31) with hours and minutes as decimal part
=back
=head3 Optional Named Arguments
=over
=item *
B<gregorian_start> — start of Gregorian calendar. Default value is
B<15821004> If the date is Julian ("old style"), use C<undef> value.
To provide non-standard start of Gregorian calendar, provide a number
in format YYYYMMDDD, e.g. C<19180126> for Jan 26, 1918.
=back
=head3 Returns
I<true> or I<false>=.
=head2 cal2jd($year, $month, $date)
Convert civil date/time to Standard Julian date.
If C<gregorian_start> argument is not provided, it is assumed that this is a date
of I<Proleptic Gregorian calendar>, which started at Oct. 4, 1582.
=head3 Positional Arguments:
=over
=item * B<year> (astronomic, zero-based)
=item * B<month> (1-12)
=item * B<date> UTC date (1-31) with hours and minutes as decimal part
=back
=head3 Optional Named Arguments
=over
=item *
gregorian_start — start of Gregorian calendar. Default value is
B<15821004> If the date is Julian ("old style"), use C<undef> value.
To provide non-standard start of Gregorian calendar, provide a number
in format YYYYMMDDD, e.g. C<19180126> for Jan 26, 1918.
=back
=head3 Returns
Standard Julian date
=head2 jd2cal($jd)
Convert Standard Julian date to civil date/time
=head3 Positional Arguments
Standard Julian Date
=head3 Optional Named Arguments
=over
=item * gregorian — if i<true>, the result will be old-style (Julian) date
=back
=head3 Returns
A list corresponding to the input values of L</cal2jd($year, $month, $date)> function.
The date is given in the proleptic Gregorian calendar system unless B<gregorian>
flag is set to I<true>.
=head2 jd0($jd)
Given Standard Julian Date, calculate Standard Julian date for midnight of the same date.
=head2 unix2jd($seconds)
Given Unix (POSIX) time, in seconds, convert it to Standard Julian date.
=head2 jd2unix($jd)
Given a Standard Julian Date, convert it to Unix time, in seconds since start of
Unix epoch.
If JD falls before start of the epoch, result will be negative and thus, unusable
for Unix-specific functions like B<localtime()>.
=head2 jdnow()
Standard Julian date for the current moment.
=head2 jd2mjd($jd)
Standard to Modified Julian date.
=head2 mjd2jd($mjd)
Modified to Standard Julian date.
=head2 jd_cent($jd)
Given aI<Standard Julian date>, calculate time in centuries since epoch 2000.0.
=head2 t1900($jd)
Given a I<Standard Julian date>, calculate time in centuries since epoch 1900.0.
=head2 jd2gst($jd)
Given I<Standard Julian date>, calculate I<True Greenwich Sidereal time>.
=head2 jd2lst($jd, $lng)
Givan I<Standard Julian date>, calculate true I<Local Sidereal time>.
=head3 Arguments
=over
=item * $jd — Standard Julian date
=item * $lng — Geographic longitude, negative for Eastern longitude, 0 by default
=back
=head1 AUTHOR
Sergey Krushinsky, C<< <krushi at cpan.org> >>
=head1 LICENSE AND COPYRIGHT
Copyright 2010-2019 Sergey Krushinsky.
This program is free software; you can redistribute it and/or modify it
under the terms of either: the GNU General Public License as published
by the Free Software Foundation; or the Artistic License.
See http://dev.perl.org/licenses/ for more information.
=cut