File Coverage

erfasrc/src/jd2cal.c
Criterion Covered Total %
statement 37 45 82.2
branch 14 26 53.8
condition n/a
subroutine n/a
pod n/a
total 51 71 71.8


line stmt bran cond sub pod time code
1             #include "erfa.h"
2             #include
3              
4 5           int eraJd2cal(double dj1, double dj2,
5             int *iy, int *im, int *id, double *fd)
6             /*
7             ** - - - - - - - - - -
8             ** e r a J d 2 c a l
9             ** - - - - - - - - - -
10             **
11             ** Julian Date to Gregorian year, month, day, and fraction of a day.
12             **
13             ** Given:
14             ** dj1,dj2 double Julian Date (Notes 1, 2)
15             **
16             ** Returned (arguments):
17             ** iy int year
18             ** im int month
19             ** id int day
20             ** fd double fraction of day
21             **
22             ** Returned (function value):
23             ** int status:
24             ** 0 = OK
25             ** -1 = unacceptable date (Note 1)
26             **
27             ** Notes:
28             **
29             ** 1) The earliest valid date is -68569.5 (-4900 March 1). The
30             ** largest value accepted is 1e9.
31             **
32             ** 2) The Julian Date is apportioned in any convenient way between
33             ** the arguments dj1 and dj2. For example, JD=2450123.7 could
34             ** be expressed in any of these ways, among others:
35             **
36             ** dj1 dj2
37             **
38             ** 2450123.7 0.0 (JD method)
39             ** 2451545.0 -1421.3 (J2000 method)
40             ** 2400000.5 50123.2 (MJD method)
41             ** 2450123.5 0.2 (date & time method)
42             **
43             ** Separating integer and fraction uses the "compensated summation"
44             ** algorithm of Kahan-Neumaier to preserve as much precision as
45             ** possible irrespective of the jd1+jd2 apportionment.
46             **
47             ** 3) In early eras the conversion is from the "proleptic Gregorian
48             ** calendar"; no account is taken of the date(s) of adoption of
49             ** the Gregorian calendar, nor is the AD/BC numbering convention
50             ** observed.
51             **
52             ** References:
53             **
54             ** Explanatory Supplement to the Astronomical Almanac,
55             ** P. Kenneth Seidelmann (ed), University Science Books (1992),
56             ** Section 12.92 (p604).
57             **
58             ** Klein, A., A Generalized Kahan-Babuska-Summation-Algorithm.
59             ** Computing 76, 279-293 (2006), Section 3.
60             **
61             ** Copyright (C) 2013-2020, NumFOCUS Foundation.
62             ** Derived, with permission, from the SOFA library. See notes at end of file.
63             */
64             {
65             /* Minimum and maximum allowed JD */
66             const double DJMIN = -68569.5;
67             const double DJMAX = 1e9;
68              
69             long jd, i, l, n, k;
70             double dj, f1, f2, d, s, cs, v[2], x, t, f;
71              
72              
73             /* Verify date is acceptable. */
74 5           dj = dj1 + dj2;
75 5 50         if (dj < DJMIN || dj > DJMAX) return -1;
    50          
76              
77             /* Separate day and fraction (where -0.5 <= fraction < 0.5). */
78 5 50         d = ERFA_DNINT(dj1);
    50          
79 5           f1 = dj1 - d;
80 5           jd = (long) d;
81 5 50         d = ERFA_DNINT(dj2);
    50          
82 5           f2 = dj2 - d;
83 5           jd += (long) d;
84              
85             /* Compute f1+f2+0.5 using compensated summation (Klein 2006). */
86             s = 0.5;
87             cs = 0.0;
88 5           v[0] = f1;
89 5           v[1] = f2;
90 15 100         for ( i = 0; i < 2; i++ ) {
91 10           x = v[i];
92 10           t = s + x;
93 10 100         cs += fabs(s) >= fabs(x) ? (s-t) + x : (x-t) + s;
94             s = t;
95 10 50         if ( s >= 1.0 ) {
96 0           jd++;
97 0           s -= 1.0;
98             }
99             }
100 5           f = s + cs;
101 5           cs = f - s;
102              
103             /* Deal with negative f. */
104 5 100         if ( f < 0.0 ) {
105              
106             /* Compensated summation: assume that |s| <= 1.0. */
107 4           f = s + 1.0;
108 4           cs += (1.0-f) + s;
109             s = f;
110 4           f = s + cs;
111 4           cs = f - s;
112 4           jd--;
113             }
114              
115             /* Deal with f that is 1.0 or more (when rounded to double). */
116 5 50         if ( (f-1.0) >= -DBL_EPSILON/4.0 ) {
117              
118             /* Compensated summation: assume that |s| <= 1.0. */
119 0           t = s - 1.0;
120 0           cs += (s-t) - 1.0;
121             s = t;
122 0           f = s + cs;
123 0 0         if ( -DBL_EPSILON/2.0 < f ) {
124 0           jd++;
125 0 0         f = ERFA_GMAX(f, 0.0);
126             }
127             }
128              
129             /* Express day in Gregorian calendar. */
130 5           l = jd + 68569L;
131 5           n = (4L * l) / 146097L;
132 5           l -= (146097L * n + 3L) / 4L;
133 5           i = (4000L * (l + 1L)) / 1461001L;
134 5           l -= (1461L * i) / 4L - 31L;
135 5           k = (80L * l) / 2447L;
136 5           *id = (int) (l - (2447L * k) / 80L);
137 5           l = k / 11L;
138 5           *im = (int) (k + 2L - 12L * l);
139 5           *iy = (int) (100L * (n - 49L) + i + l);
140 5           *fd = f;
141              
142 5           return 0;
143              
144             }
145             /*----------------------------------------------------------------------
146             **
147             **
148             ** Copyright (C) 2013-2020, NumFOCUS Foundation.
149             ** All rights reserved.
150             **
151             ** This library is derived, with permission, from the International
152             ** Astronomical Union's "Standards of Fundamental Astronomy" library,
153             ** available from http://www.iausofa.org.
154             **
155             ** The ERFA version is intended to retain identical functionality to
156             ** the SOFA library, but made distinct through different function and
157             ** file names, as set out in the SOFA license conditions. The SOFA
158             ** original has a role as a reference standard for the IAU and IERS,
159             ** and consequently redistribution is permitted only in its unaltered
160             ** state. The ERFA version is not subject to this restriction and
161             ** therefore can be included in distributions which do not support the
162             ** concept of "read only" software.
163             **
164             ** Although the intent is to replicate the SOFA API (other than
165             ** replacement of prefix names) and results (with the exception of
166             ** bugs; any that are discovered will be fixed), SOFA is not
167             ** responsible for any errors found in this version of the library.
168             **
169             ** If you wish to acknowledge the SOFA heritage, please acknowledge
170             ** that you are using a library derived from SOFA, rather than SOFA
171             ** itself.
172             **
173             **
174             ** TERMS AND CONDITIONS
175             **
176             ** Redistribution and use in source and binary forms, with or without
177             ** modification, are permitted provided that the following conditions
178             ** are met:
179             **
180             ** 1 Redistributions of source code must retain the above copyright
181             ** notice, this list of conditions and the following disclaimer.
182             **
183             ** 2 Redistributions in binary form must reproduce the above copyright
184             ** notice, this list of conditions and the following disclaimer in
185             ** the documentation and/or other materials provided with the
186             ** distribution.
187             **
188             ** 3 Neither the name of the Standards Of Fundamental Astronomy Board,
189             ** the International Astronomical Union nor the names of its
190             ** contributors may be used to endorse or promote products derived
191             ** from this software without specific prior written permission.
192             **
193             ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
194             ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
195             ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
196             ** FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
197             ** COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
198             ** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
199             ** BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
200             ** LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
201             ** CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
202             ** LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
203             ** ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
204             ** POSSIBILITY OF SUCH DAMAGE.
205             **
206             */