source: branches/FACT++_part_filenames/erfa/src/dtf2d.c@ 18796

Last change on this file since 18796 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: 7.7 KB
Line 
1#include "erfa.h"
2#include <string.h>
3
4int eraDtf2d(const char *scale, int iy, int im, int id,
5 int ihr, int imn, double sec, double *d1, double *d2)
6/*
7** - - - - - - - - -
8** e r a D t f 2 d
9** - - - - - - - - -
10**
11** Encode date and time fields into 2-part Julian Date (or in the case
12** of UTC a quasi-JD form that includes special provision for leap
13** seconds).
14**
15** Given:
16** scale char[] time scale ID (Note 1)
17** iy,im,id int year, month, day in Gregorian calendar (Note 2)
18** ihr,imn int hour, minute
19** sec double seconds
20**
21** Returned:
22** d1,d2 double 2-part Julian Date (Notes 3,4)
23**
24** Returned (function value):
25** int status: +3 = both of next two
26** +2 = time is after end of day (Note 5)
27** +1 = dubious year (Note 6)
28** 0 = OK
29** -1 = bad year
30** -2 = bad month
31** -3 = bad day
32** -4 = bad hour
33** -5 = bad minute
34** -6 = bad second (<0)
35**
36** Notes:
37**
38** 1) scale identifies the time scale. Only the value "UTC" (in upper
39** case) is significant, and enables handling of leap seconds (see
40** Note 4).
41**
42** 2) For calendar conventions and limitations, see eraCal2jd.
43**
44** 3) The sum of the results, d1+d2, is Julian Date, where normally d1
45** is the Julian Day Number and d2 is the fraction of a day. In the
46** case of UTC, where the use of JD is problematical, special
47** conventions apply: see the next note.
48**
49** 4) JD cannot unambiguously represent UTC during a leap second unless
50** special measures are taken. The ERFA internal convention is that
51** the quasi-JD day represents UTC days whether the length is 86399,
52** 86400 or 86401 SI seconds. In the 1960-1972 era there were
53** smaller jumps (in either direction) each time the linear UTC(TAI)
54** expression was changed, and these "mini-leaps" are also included
55** in the ERFA convention.
56**
57** 5) The warning status "time is after end of day" usually means that
58** the sec argument is greater than 60.0. However, in a day ending
59** in a leap second the limit changes to 61.0 (or 59.0 in the case
60** of a negative leap second).
61**
62** 6) The warning status "dubious year" flags UTCs that predate the
63** introduction of the time scale or that are too far in the future
64** to be trusted. See eraDat for further details.
65**
66** 7) Only in the case of continuous and regular time scales (TAI, TT,
67** TCG, TCB and TDB) is the result d1+d2 a Julian Date, strictly
68** speaking. In the other cases (UT1 and UTC) the result must be
69** used with circumspection; in particular the difference between
70** two such results cannot be interpreted as a precise time
71** interval.
72**
73** Called:
74** eraCal2jd Gregorian calendar to JD
75** eraDat delta(AT) = TAI-UTC
76** eraJd2cal JD to Gregorian calendar
77**
78** Copyright (C) 2013-2016, NumFOCUS Foundation.
79** Derived, with permission, from the SOFA library. See notes at end of file.
80*/
81{
82 int js, iy2, im2, id2;
83 double dj, w, day, seclim, dat0, dat12, dat24, dleap, time;
84
85
86/* Today's Julian Day Number. */
87 js = eraCal2jd(iy, im, id, &dj, &w);
88 if ( js ) return js;
89 dj += w;
90
91/* Day length and final minute length in seconds (provisional). */
92 day = ERFA_DAYSEC;
93 seclim = 60.0;
94
95/* Deal with the UTC leap second case. */
96 if ( ! strcmp(scale,"UTC") ) {
97
98 /* TAI-UTC at 0h today. */
99 js = eraDat(iy, im, id, 0.0, &dat0);
100 if ( js < 0 ) return js;
101
102 /* TAI-UTC at 12h today (to detect drift). */
103 js = eraDat(iy, im, id, 0.5, &dat12);
104 if ( js < 0 ) return js;
105
106 /* TAI-UTC at 0h tomorrow (to detect jumps). */
107 js = eraJd2cal ( dj, 1.5, &iy2, &im2, &id2, &w);
108 if ( js ) return js;
109 js = eraDat(iy2, im2, id2, 0.0, &dat24);
110 if ( js < 0 ) return js;
111
112 /* Any sudden change in TAI-UTC between today and tomorrow. */
113 dleap = dat24 - (2.0*dat12 - dat0);
114
115 /* If leap second day, correct the day and final minute lengths. */
116 day += dleap;
117 if ( ihr == 23 && imn == 59 ) seclim += dleap;
118
119 /* End of UTC-specific actions. */
120 }
121
122/* Validate the time. */
123 if ( ihr >= 0 && ihr <= 23 ) {
124 if ( imn >= 0 && imn <= 59 ) {
125 if ( sec >= 0 ) {
126 if ( sec >= seclim ) {
127 js += 2;
128 }
129 } else {
130 js = -6;
131 }
132 } else {
133 js = -5;
134 }
135 } else {
136 js = -4;
137 }
138 if ( js < 0 ) return js;
139
140/* The time in days. */
141 time = ( 60.0 * ( (double) ( 60 * ihr + imn ) ) + sec ) / day;
142
143/* Return the date and time. */
144 *d1 = dj;
145 *d2 = time;
146
147/* Status. */
148 return js;
149
150}
151/*----------------------------------------------------------------------
152**
153**
154** Copyright (C) 2013-2016, NumFOCUS Foundation.
155** All rights reserved.
156**
157** This library is derived, with permission, from the International
158** Astronomical Union's "Standards of Fundamental Astronomy" library,
159** available from http://www.iausofa.org.
160**
161** The ERFA version is intended to retain identical functionality to
162** the SOFA library, but made distinct through different function and
163** file names, as set out in the SOFA license conditions. The SOFA
164** original has a role as a reference standard for the IAU and IERS,
165** and consequently redistribution is permitted only in its unaltered
166** state. The ERFA version is not subject to this restriction and
167** therefore can be included in distributions which do not support the
168** concept of "read only" software.
169**
170** Although the intent is to replicate the SOFA API (other than
171** replacement of prefix names) and results (with the exception of
172** bugs; any that are discovered will be fixed), SOFA is not
173** responsible for any errors found in this version of the library.
174**
175** If you wish to acknowledge the SOFA heritage, please acknowledge
176** that you are using a library derived from SOFA, rather than SOFA
177** itself.
178**
179**
180** TERMS AND CONDITIONS
181**
182** Redistribution and use in source and binary forms, with or without
183** modification, are permitted provided that the following conditions
184** are met:
185**
186** 1 Redistributions of source code must retain the above copyright
187** notice, this list of conditions and the following disclaimer.
188**
189** 2 Redistributions in binary form must reproduce the above copyright
190** notice, this list of conditions and the following disclaimer in
191** the documentation and/or other materials provided with the
192** distribution.
193**
194** 3 Neither the name of the Standards Of Fundamental Astronomy Board,
195** the International Astronomical Union nor the names of its
196** contributors may be used to endorse or promote products derived
197** from this software without specific prior written permission.
198**
199** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
200** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
201** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
202** FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
203** COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
204** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
205** BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
206** LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
207** CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
208** LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
209** ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
210** POSSIBILITY OF SUCH DAMAGE.
211**
212*/
Note: See TracBrowser for help on using the repository browser.