source: branches/FACT++_part_filenames/erfa/src/jd2cal.c@ 19365

Last change on this file since 19365 was 18711, checked in by tbretz, 8 years ago
Updated to ERFA 1.3.0 (no relevant code change except the leap second at the beginning of 2017)
File size: 5.6 KB
Line 
1#include "erfa.h"
2
3int eraJd2cal(double dj1, double dj2,
4 int *iy, int *im, int *id, double *fd)
5/*
6** - - - - - - - - - -
7** e r a J d 2 c a l
8** - - - - - - - - - -
9**
10** Julian Date to Gregorian year, month, day, and fraction of a day.
11**
12** Given:
13** dj1,dj2 double Julian Date (Notes 1, 2)
14**
15** Returned (arguments):
16** iy int year
17** im int month
18** id int day
19** fd double fraction of day
20**
21** Returned (function value):
22** int status:
23** 0 = OK
24** -1 = unacceptable date (Note 3)
25**
26** Notes:
27**
28** 1) The earliest valid date is -68569.5 (-4900 March 1). The
29** largest value accepted is 1e9.
30**
31** 2) The Julian Date is apportioned in any convenient way between
32** the arguments dj1 and dj2. For example, JD=2450123.7 could
33** be expressed in any of these ways, among others:
34**
35** dj1 dj2
36**
37** 2450123.7 0.0 (JD method)
38** 2451545.0 -1421.3 (J2000 method)
39** 2400000.5 50123.2 (MJD method)
40** 2450123.5 0.2 (date & time method)
41**
42** 3) In early eras the conversion is from the "proleptic Gregorian
43** calendar"; no account is taken of the date(s) of adoption of
44** the Gregorian calendar, nor is the AD/BC numbering convention
45** observed.
46**
47** Reference:
48**
49** Explanatory Supplement to the Astronomical Almanac,
50** P. Kenneth Seidelmann (ed), University Science Books (1992),
51** Section 12.92 (p604).
52**
53** Copyright (C) 2013-2016, NumFOCUS Foundation.
54** Derived, with permission, from the SOFA library. See notes at end of file.
55*/
56{
57/* Minimum and maximum allowed JD */
58 const double DJMIN = -68569.5;
59 const double DJMAX = 1e9;
60
61 long jd, l, n, i, k;
62 double dj, d1, d2, f1, f2, f, d;
63
64
65/* Verify date is acceptable. */
66 dj = dj1 + dj2;
67 if (dj < DJMIN || dj > DJMAX) return -1;
68
69/* Copy the date, big then small, and re-align to midnight. */
70 if (dj1 >= dj2) {
71 d1 = dj1;
72 d2 = dj2;
73 } else {
74 d1 = dj2;
75 d2 = dj1;
76 }
77 d2 -= 0.5;
78
79/* Separate day and fraction. */
80 f1 = fmod(d1, 1.0);
81 f2 = fmod(d2, 1.0);
82 f = fmod(f1 + f2, 1.0);
83 if (f < 0.0) f += 1.0;
84 d = floor(d1 - f1) + floor(d2 - f2) + floor(f1 + f2 - f);
85 jd = (long) floor(d) + 1L;
86
87/* Express day in Gregorian calendar. */
88 l = jd + 68569L;
89 n = (4L * l) / 146097L;
90 l -= (146097L * n + 3L) / 4L;
91 i = (4000L * (l + 1L)) / 1461001L;
92 l -= (1461L * i) / 4L - 31L;
93 k = (80L * l) / 2447L;
94 *id = (int) (l - (2447L * k) / 80L);
95 l = k / 11L;
96 *im = (int) (k + 2L - 12L * l);
97 *iy = (int) (100L * (n - 49L) + i + l);
98 *fd = f;
99
100 return 0;
101
102}
103/*----------------------------------------------------------------------
104**
105**
106** Copyright (C) 2013-2016, NumFOCUS Foundation.
107** All rights reserved.
108**
109** This library is derived, with permission, from the International
110** Astronomical Union's "Standards of Fundamental Astronomy" library,
111** available from http://www.iausofa.org.
112**
113** The ERFA version is intended to retain identical functionality to
114** the SOFA library, but made distinct through different function and
115** file names, as set out in the SOFA license conditions. The SOFA
116** original has a role as a reference standard for the IAU and IERS,
117** and consequently redistribution is permitted only in its unaltered
118** state. The ERFA version is not subject to this restriction and
119** therefore can be included in distributions which do not support the
120** concept of "read only" software.
121**
122** Although the intent is to replicate the SOFA API (other than
123** replacement of prefix names) and results (with the exception of
124** bugs; any that are discovered will be fixed), SOFA is not
125** responsible for any errors found in this version of the library.
126**
127** If you wish to acknowledge the SOFA heritage, please acknowledge
128** that you are using a library derived from SOFA, rather than SOFA
129** itself.
130**
131**
132** TERMS AND CONDITIONS
133**
134** Redistribution and use in source and binary forms, with or without
135** modification, are permitted provided that the following conditions
136** are met:
137**
138** 1 Redistributions of source code must retain the above copyright
139** notice, this list of conditions and the following disclaimer.
140**
141** 2 Redistributions in binary form must reproduce the above copyright
142** notice, this list of conditions and the following disclaimer in
143** the documentation and/or other materials provided with the
144** distribution.
145**
146** 3 Neither the name of the Standards Of Fundamental Astronomy Board,
147** the International Astronomical Union nor the names of its
148** contributors may be used to endorse or promote products derived
149** from this software without specific prior written permission.
150**
151** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
152** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
153** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
154** FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
155** COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
156** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
157** BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
158** LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
159** CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
160** LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
161** ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
162** POSSIBILITY OF SUCH DAMAGE.
163**
164*/
Note: See TracBrowser for help on using the repository browser.