source: trunk/FACT++/erfa/src/cal2jd.c@ 18679

Last change on this file since 18679 was 18348, checked in by tbretz, 9 years ago
File size: 5.4 KB
Line 
1#include "erfa.h"
2
3int eraCal2jd(int iy, int im, int id, double *djm0, double *djm)
4/*
5** - - - - - - - - - -
6** e r a C a l 2 j d
7** - - - - - - - - - -
8**
9** Gregorian Calendar to Julian Date.
10**
11** Given:
12** iy,im,id int year, month, day in Gregorian calendar (Note 1)
13**
14** Returned:
15** djm0 double MJD zero-point: always 2400000.5
16** djm double Modified Julian Date for 0 hrs
17**
18** Returned (function value):
19** int status:
20** 0 = OK
21** -1 = bad year (Note 3: JD not computed)
22** -2 = bad month (JD not computed)
23** -3 = bad day (JD computed)
24**
25** Notes:
26**
27** 1) The algorithm used is valid from -4800 March 1, but this
28** implementation rejects dates before -4799 January 1.
29**
30** 2) The Julian Date is returned in two pieces, in the usual ERFA
31** manner, which is designed to preserve time resolution. The
32** Julian Date is available as a single number by adding djm0 and
33** djm.
34**
35** 3) In early eras the conversion is from the "Proleptic Gregorian
36** Calendar"; no account is taken of the date(s) of adoption of
37** the Gregorian Calendar, nor is the AD/BC numbering convention
38** observed.
39**
40** Reference:
41**
42** Explanatory Supplement to the Astronomical Almanac,
43** P. Kenneth Seidelmann (ed), University Science Books (1992),
44** Section 12.92 (p604).
45**
46** Copyright (C) 2013-2015, NumFOCUS Foundation.
47** Derived, with permission, from the SOFA library. See notes at end of file.
48*/
49{
50 int j, ly, my;
51 long iypmy;
52
53/* Earliest year allowed (4800BC) */
54 const int IYMIN = -4799;
55
56/* Month lengths in days */
57 static const int mtab[]
58 = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
59
60/* Preset status. */
61 j = 0;
62
63/* Validate year and month. */
64 if (iy < IYMIN) return -1;
65 if (im < 1 || im > 12) return -2;
66
67/* If February in a leap year, 1, otherwise 0. */
68 ly = ((im == 2) && !(iy%4) && (iy%100 || !(iy%400)));
69
70/* Validate day, taking into account leap years. */
71 if ( (id < 1) || (id > (mtab[im-1] + ly))) j = -3;
72
73/* Return result. */
74 my = (im - 14) / 12;
75 iypmy = (long) (iy + my);
76 *djm0 = ERFA_DJM0;
77 *djm = (double)((1461L * (iypmy + 4800L)) / 4L
78 + (367L * (long) (im - 2 - 12 * my)) / 12L
79 - (3L * ((iypmy + 4900L) / 100L)) / 4L
80 + (long) id - 2432076L);
81
82/* Return status. */
83 return j;
84
85}
86/*----------------------------------------------------------------------
87**
88**
89** Copyright (C) 2013-2015, NumFOCUS Foundation.
90** All rights reserved.
91**
92** This library is derived, with permission, from the International
93** Astronomical Union's "Standards of Fundamental Astronomy" library,
94** available from http://www.iausofa.org.
95**
96** The ERFA version is intended to retain identical functionality to
97** the SOFA library, but made distinct through different function and
98** file names, as set out in the SOFA license conditions. The SOFA
99** original has a role as a reference standard for the IAU and IERS,
100** and consequently redistribution is permitted only in its unaltered
101** state. The ERFA version is not subject to this restriction and
102** therefore can be included in distributions which do not support the
103** concept of "read only" software.
104**
105** Although the intent is to replicate the SOFA API (other than
106** replacement of prefix names) and results (with the exception of
107** bugs; any that are discovered will be fixed), SOFA is not
108** responsible for any errors found in this version of the library.
109**
110** If you wish to acknowledge the SOFA heritage, please acknowledge
111** that you are using a library derived from SOFA, rather than SOFA
112** itself.
113**
114**
115** TERMS AND CONDITIONS
116**
117** Redistribution and use in source and binary forms, with or without
118** modification, are permitted provided that the following conditions
119** are met:
120**
121** 1 Redistributions of source code must retain the above copyright
122** notice, this list of conditions and the following disclaimer.
123**
124** 2 Redistributions in binary form must reproduce the above copyright
125** notice, this list of conditions and the following disclaimer in
126** the documentation and/or other materials provided with the
127** distribution.
128**
129** 3 Neither the name of the Standards Of Fundamental Astronomy Board,
130** the International Astronomical Union nor the names of its
131** contributors may be used to endorse or promote products derived
132** from this software without specific prior written permission.
133**
134** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
135** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
136** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
137** FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
138** COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
139** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
140** BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
141** LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
142** CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
143** LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
144** ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
145** POSSIBILITY OF SUCH DAMAGE.
146**
147*/
Note: See TracBrowser for help on using the repository browser.