blib/lib/Time/UTC_SLS.pm

filter: Statementstmt 100% 52 / 52 Branchbran 90.9% 20 / 22 Conditioncond 66.6% 8 / 12 MC/DCmcdc 50.0% 4 / 8 Subroutinesub 100% 11 / 11 Podpod 100% 2 / 2 totaltotal 90.6% 97 / 107 CC 16 SCAR 18.1
CC
16
Cov
93%
utc_to_utcsls
10.1
utcsls_to_utc
7.1
BEGIN
1.0
CRAP 6.1
↑ Summary
1 =head1 NAME
2  
3 Time::UTC_SLS - UTC with Smoothed Leap Seconds
4  
5 =head1 SYNOPSIS
6  
7 use Time::UTC_SLS qw(utc_to_utcsls utcsls_to_utc);
8  
9 $mjd = utc_to_utcsls($day, $secs);
10 ($day, $secs) = utcsls_to_day($mjd);
11  
12 use Time::UTC_SLS qw(
13 utc_day_to_mjdn utc_mjdn_to_day
14 utc_day_to_cjdn utc_cjdn_to_day);
15  
16 $mjdn = utc_day_to_mjdn($day);
17 $day = utc_mjdn_to_day($mjdn);
18  
19 $cjdn = utc_day_to_cjdn($day);
20 $day = utc_cjdn_to_day($cjdn);
21  
22 =head1 DESCRIPTION
23  
24 Coordinated Universal Time (UTC) is a time scale with days of unequal
25 lengths, due to leap seconds, in order to keep in step with both Terran
26 rotation (Universal Time, UT) and International Atomic Time (TAI).
27 Some applications that wish to use a time scale that maintains both of
28 these relations can't cope with unequal day lengths, and so cannot use
29 UTC properly. UTC with Smoothed Leap Seconds (UTC-SLS) is another option
30 in such cases. UTC-SLS is a time scale that usually matches UTC exactly
31 but changes rate in the time leading up to a leap second in order to
32 make every day appear to be exactly the same length.
33  
34 On a normal UTC day, of length 86400 UTC seconds, UTC and UTC-SLS
35 behave identically. On a day with a leap second, thus having 86401 or
36 (theoretically) 86399 UTC seconds, UTC and UTC-SLS behave identically
37 for most of the day, but the last 1000 UTC seconds correspond to 999 or
38 (theoretically) 1001 UTC-SLS seconds. Thus every UTC-SLS day has exactly
39 86400 UTC-SLS seconds. UTC and UTC-SLS are equal on every half hour,
40 and in particular the day boundaries (at midnight) are in the same place
41 on both time scales. See L<http://www.cl.cam.ac.uk/~mgk25/time/utc-sls/>
42 for further explanation.
43  
44 UTC-SLS is defined for the post-1972 form of UTC, using leap seconds.
45 The prior form, from 1961, using `rubber seconds' as well as leaps,
46 could be treated in a similar manner, but the exact algorithm has not
47 been defined. The rubber seconds system was itself trying to achieve
48 part of what UTC-SLS does.
49  
50 This module represents instants on the UTC scale by the combination of
51 a day number and a number of seconds since midnight within the day.
52 In this module the day number is the integral number of days since
53 1958-01-01, which is the epoch of TAI. This is the convention used by
54 the C<Time::UTC> module. Instants on the UTC-SLS scale are represented
55 by a Modified Julian Date, which is a fractional count of days since
56 1858-11-17T00Z. The MJD is a suitable interchange format between
57 date-manipulation modules.
58  
59 All numbers in this API are C<Math::BigRat> objects. All numeric function
60 arguments must be C<Math::BigRat>s, and all numeric values returned are
61 likewise C<Math::BigRat>s.
62  
63 =cut
64  
65 package Time::UTC_SLS;
66  
67 4 ▶{ use 5.006; }
Subroutine BEGIN: called CC=1 SCAR=0.0
68 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
69 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
70  
71 4 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
72 4 ▶use Math::BigRat 0.04;
Subroutine BEGIN: called CC=1 SCAR=0.0
73 4 ▶use Time::UTC 0.007 qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
74 utc_day_seconds
75 utc_day_to_mjdn utc_mjdn_to_day
76 utc_day_to_cjdn utc_cjdn_to_day
77 4 );
78  
79 4 our $VERSION = "0.005";
80  
81 4 ▶use parent "Exporter";
Subroutine BEGIN: called CC=1 SCAR=0.0
82 4 our @EXPORT_OK = qw(
83 utc_to_utcsls utcsls_to_utc
84 utc_day_to_mjdn utc_mjdn_to_day
85 utc_day_to_cjdn utc_cjdn_to_day
86 );
87  
88 =head1 FUNCTIONS
89  
90 =over
91  
92 =item utc_to_utcsls(DAY, SECS)
93  
94 Converts from UTC to UTC-SLS. The input is a UTC instant expressed as a
95 day number and a number of seconds since midnight, both as C<Math::BigRat>
96 objects. Returns the corresponding UTC-SLS instant expressed as a
97 Modified Julian Date, as a C<Math::BigRat> object.
98  
99 =cut
100  
101 4 ▶use constant UTCSLS_START_DAY => Math::BigRat->new(5113);
Subroutine BEGIN: called CC=1 SCAR=0.0
102 4 ▶use constant TAI_EPOCH_MJD => Math::BigRat->new(36204);
Subroutine BEGIN: called CC=1 SCAR=0.0
103  
104 26 ▶sub utc_to_utcsls($$) {
Subroutine utc_to_utcsls: called CC=10 SCAR=23.1
Pod documented
105 26 my($day, $secs) = @_;
106 26 ▶ croak "day $day precedes the start of UTC-SLS"
Branch: 1 branch
BranchTrueFalseTotal
unless $day >= bless( {"_d" => bless( [1], 'Math::BigInt::Calc' ),"_n" => bless( [5113], 'Math::BigInt::Calc' ),"sign" => "+"}, 'Math::BigRat' ) 4 22 26
107 unless $day >= UTCSLS_START_DAY;
108 22 ▶ unless($secs >= 0 && $secs <= 85399) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($secs >= 0 and $secs <= 85399) 13 9 22
Condition100%3/3
$secs >= 0 and $secs <= 85399
!ll&&!rl&&r
2119
MC/DC100%2/2
$secs >= 0 and $secs <= 85399
$secs >= 0 $secs <= 85399
Truth table100%3/3
A and B
A= $secs >= 0B= $secs <= 85399
ABresult
0X0
100
111
109 13 my $day_len = utc_day_seconds($day);
110 13 ▶ croak "$secs seconds is out of range for a $day_len second day"
Branch: 1 branch
BranchTrueFalseTotal
if $secs < 0 or $secs >= $day_len 4 9 13
Condition100%3/3
$secs < 0 or $secs >= $day_len
l!l&&r!l&&!r
229
MC/DC100%2/2
$secs < 0 or $secs >= $day_len
$secs < 0 $secs >= $day_len
Truth table100%3/3
A or B
A= $secs < 0B= $secs >= $day_len
ABresult
1X1
011
000
111 if $secs < 0 || $secs >= $day_len;
112 9 ▶ if($day_len != 86400) {
Branch: 1 branch
BranchTrueFalseTotal
if ($day_len != 86400) 7 2 9
113 7 ▶ croak "UTC-SLS is not defined for a $day_len ".
Branch: 1 branch
BranchTrueFalseTotal
unless $day_len == 86399 or $day_len == 86401 0 7 7
Condition33%1/3
$day_len == 86399 or $day_len == 86401
l!l&&r!l&&!r
070
MC/DC0%0/2
$day_len == 86399 or $day_len == 86401
$day_len == 86399 $day_len == 86401
Truth table33%1/3
A or B
A= $day_len == 86399B= $day_len == 86401
ABresult
1X1
011
000
114 "second day"
115 unless $day_len == 86399 || $day_len == 86401;
116 7 my $slew_from = $day_len - 1000;
117 7 ▶ $secs = $slew_from + (86400 - $slew_from) *
Branch: 1 branch
BranchTrueFalseTotal
if $secs > $slew_from 5 2 7
118 ($secs - $slew_from)/1000
119 if $secs > $slew_from;
120 }
121 }
122 18 return utc_day_to_mjdn($day) + $secs/86400;
123 }
124  
125 =item utcsls_to_utc(MJD)
126  
127 Converts from UTC-SLS to UTC. The input is a UTC-SLS instant expressed
128 as a Modified Julian Date, as a C<Math::BigRat> object. Returns a list of
129 two values, giving the corresponding UTC instant expressed as a day number
130 and a number of seconds since midnight, both as C<Math::BigRat> objects.
131  
132 =cut
133  
134 19 ▶sub utcsls_to_utc($) {
Subroutine utcsls_to_utc: called CC=7 SCAR=19.6
Pod documented
135 19 my($mjd) = @_;
136 19 my $mjdn = $mjd->copy->bfloor;
137 19 my $secs = ($mjd - $mjdn) * 86400;
138 19 my $day = $mjdn - TAI_EPOCH_MJD;
139 19 ▶ croak "day $day precedes the start of UTC-SLS"
Branch: 1 branch
BranchTrueFalseTotal
unless $day >= bless( {"_d" => bless( [1], 'Math::BigInt::Calc' ),"_n" => bless( [5113], 'Math::BigInt::Calc' ),"sign" => "+"}, 'Math::BigRat' ) 1 18 19
140 unless $day >= UTCSLS_START_DAY;
141 18 ▶ unless($secs <= 85399) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($secs <= 85399) 9 9 18
142 9 my $day_len = utc_day_seconds($day);
143 9 ▶ if($day_len != 86400) {
Branch: 1 branch
BranchTrueFalseTotal
if ($day_len != 86400) 7 2 9
144 7 ▶ croak "UTC-SLS is not defined for a $day_len ".
Branch: 1 branch
BranchTrueFalseTotal
unless $day_len == 86399 or $day_len == 86401 0 7 7
Condition33%1/3
$day_len == 86399 or $day_len == 86401
l!l&&r!l&&!r
070
MC/DC0%0/2
$day_len == 86399 or $day_len == 86401
$day_len == 86399 $day_len == 86401
Truth table33%1/3
A or B
A= $day_len == 86399B= $day_len == 86401
ABresult
1X1
011
000
145 "second day"
146 unless $day_len == 86399 || $day_len == 86401;
147 7 my $slew_from = $day_len - 1000;
148 7 ▶ $secs = $slew_from + 1000 * ($secs - $slew_from)/
Branch: 1 branch
BranchTrueFalseTotal
if $secs > $slew_from 5 2 7
149 (86400 - $slew_from)
150 if $secs > $slew_from;
151 }
152 }
153 18 return ($day, $secs);
154 }
155  
156 =item utc_day_to_mjdn(DAY)
157  
158 Takes a day number (days since the TAI epoch), as a C<Math::BigRat>
159 object, and returns the corresponding Modified Julian Day Number
160 (a number of days since 1858-11-17 UT), as a C<Math::BigRat> object.
161 MJDN is a standard numbering for days in Universal Time. There is no
162 bound on the permissible day numbers; the function is not limited to
163 days for which UTC-SLS is defined.
164  
165 =item utc_mjdn_to_day(MJDN)
166  
167 This performs the reverse of the translation that C<utc_day_to_mjdn> does.
168 It takes a Modified Julian Day Number, as a C<Math::BigRat> object,
169 and returns the number of days since the TAI epoch, as a C<Math::BigRat>
170 object. It does not impose any limit on the range.
171  
172 =item utc_day_to_cjdn(DAY)
173  
174 Takes a day number (days since the TAI epoch), as a C<Math::BigRat>
175 object, and returns the corresponding Chronological Julian Day Number
176 (a number of days since -4713-11-24), as a C<Math::BigRat> object.
177 CJDN is a standard day numbering that is useful as an interchange format
178 between implementations of different calendars. There is no bound on
179 the permissible day numbers; the function is not limited to days for
180 which UTC-SLS is defined.
181  
182 =item utc_cjdn_to_day(CJDN)
183  
184 This performs the reverse of the translation that C<utc_day_to_cjdn> does.
185 It takes a Chronological Julian Day Number, as a C<Math::BigRat> object,
186 and returns the number of days since the TAI epoch, as a C<Math::BigRat>
187 object. It does not impose any limit on the range.
188  
189 =back
190  
191 =head1 SEE ALSO
192  
193 L<Date::JD>,
194 L<Time::UTC>,
195 L<http://www.cl.cam.ac.uk/~mgk25/time/utc-sls/>
196  
197 =head1 AUTHOR
198  
199 Andrew Main (Zefram) <zefram@fysh.org>
200  
201 =head1 COPYRIGHT
202  
203 Copyright (C) 2006, 2007, 2009, 2010, 2012, 2017
204 Andrew Main (Zefram) <zefram@fysh.org>
205  
206 =head1 LICENSE
207  
208 This module is free software; you can redistribute it and/or modify it
209 under the same terms as Perl itself.
210  
211 =cut
212  
213 4 1;
↑ Summary