source: branches/FACT++_part_filenames/erfa/src/cal2jd.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.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-2016, 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
61/* Preset status. */
62 j = 0;
63
64/* Validate year and month. */
65 if (iy < IYMIN) return -1;
66 if (im < 1 || im > 12) return -2;
67
68/* If February in a leap year, 1, otherwise 0. */
69 ly = ((im == 2) && !(iy%4) && (iy%100 || !(iy%400)));
70
71/* Validate day, taking into account leap years. */
72 if ( (id < 1) || (id > (mtab[im-1] + ly))) j = -3;
73
74/* Return result. */
75 my = (im - 14) / 12;
76 iypmy = (long) (iy + my);
77 *djm0 = ERFA_DJM0;
78 *djm = (double)((1461L * (iypmy + 4800L)) / 4L
79 + (367L * (long) (im - 2 - 12 * my)) / 12L
80 - (3L * ((iypmy + 4900L) / 100L)) / 4L
81 + (long) id - 2432076L);
82
83/* Return status. */
84 return j;
85
86}
87/*----------------------------------------------------------------------
88**
89**
90** Copyright (C) 2013-2016, NumFOCUS Foundation.
91** All rights reserved.
92**
93** This library is derived, with permission, from the International
94** Astronomical Union's "Standards of Fundamental Astronomy" library,
95** available from http://www.iausofa.org.
96**
97** The ERFA version is intended to retain identical functionality to
98** the SOFA library, but made distinct through different function and
99** file names, as set out in the SOFA license conditions. The SOFA
100** original has a role as a reference standard for the IAU and IERS,
101** and consequently redistribution is permitted only in its unaltered
102** state. The ERFA version is not subject to this restriction and
103** therefore can be included in distributions which do not support the
104** concept of "read only" software.
105**
106** Although the intent is to replicate the SOFA API (other than
107** replacement of prefix names) and results (with the exception of
108** bugs; any that are discovered will be fixed), SOFA is not
109** responsible for any errors found in this version of the library.
110**
111** If you wish to acknowledge the SOFA heritage, please acknowledge
112** that you are using a library derived from SOFA, rather than SOFA
113** itself.
114**
115**
116** TERMS AND CONDITIONS
117**
118** Redistribution and use in source and binary forms, with or without
119** modification, are permitted provided that the following conditions
120** are met:
121**
122** 1 Redistributions of source code must retain the above copyright
123** notice, this list of conditions and the following disclaimer.
124**
125** 2 Redistributions in binary form must reproduce the above copyright
126** notice, this list of conditions and the following disclaimer in
127** the documentation and/or other materials provided with the
128** distribution.
129**
130** 3 Neither the name of the Standards Of Fundamental Astronomy Board,
131** the International Astronomical Union nor the names of its
132** contributors may be used to endorse or promote products derived
133** from this software without specific prior written permission.
134**
135** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
136** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
137** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
138** FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
139** COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
140** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
141** BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
142** LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
143** CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
144** LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
145** ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
146** POSSIBILITY OF SUCH DAMAGE.
147**
148*/
Note: See TracBrowser for help on using the repository browser.