source: trunk/FACT++/erfa/src/d2dtf.c@ 19813

Last change on this file since 19813 was 18921, checked in by tbretz, 7 years ago
Updated to ERFA 1.4.0
File size: 8.1 KB
Line 
1#include "erfa.h"
2#include <string.h>
3
4int eraD2dtf(const char *scale, int ndp, double d1, double d2,
5 int *iy, int *im, int *id, int ihmsf[4])
6/*
7** - - - - - - - - -
8** e r a D 2 d t f
9** - - - - - - - - -
10**
11** Format for output a 2-part Julian Date (or in the case of UTC a
12** quasi-JD form that includes special provision for leap seconds).
13**
14** Given:
15** scale char[] time scale ID (Note 1)
16** ndp int resolution (Note 2)
17** d1,d2 double time as a 2-part Julian Date (Notes 3,4)
18**
19** Returned:
20** iy,im,id int year, month, day in Gregorian calendar (Note 5)
21** ihmsf int[4] hours, minutes, seconds, fraction (Note 1)
22**
23** Returned (function value):
24** int status: +1 = dubious year (Note 5)
25** 0 = OK
26** -1 = unacceptable date (Note 6)
27**
28** Notes:
29**
30** 1) scale identifies the time scale. Only the value "UTC" (in upper
31** case) is significant, and enables handling of leap seconds (see
32** Note 4).
33**
34** 2) ndp is the number of decimal places in the seconds field, and can
35** have negative as well as positive values, such as:
36**
37** ndp resolution
38** -4 1 00 00
39** -3 0 10 00
40** -2 0 01 00
41** -1 0 00 10
42** 0 0 00 01
43** 1 0 00 00.1
44** 2 0 00 00.01
45** 3 0 00 00.001
46**
47** The limits are platform dependent, but a safe range is -5 to +9.
48**
49** 3) d1+d2 is Julian Date, apportioned in any convenient way between
50** the two arguments, for example where d1 is the Julian Day Number
51** and d2 is the fraction of a day. In the case of UTC, where the
52** use of JD is problematical, special conventions apply: see the
53** next note.
54**
55** 4) JD cannot unambiguously represent UTC during a leap second unless
56** special measures are taken. The ERFA internal convention is that
57** the quasi-JD day represents UTC days whether the length is 86399,
58** 86400 or 86401 SI seconds. In the 1960-1972 era there were
59** smaller jumps (in either direction) each time the linear UTC(TAI)
60** expression was changed, and these "mini-leaps" are also included
61** in the ERFA convention.
62**
63** 5) The warning status "dubious year" flags UTCs that predate the
64** introduction of the time scale or that are too far in the future
65** to be trusted. See eraDat for further details.
66**
67** 6) For calendar conventions and limitations, see eraCal2jd.
68**
69** Called:
70** eraJd2cal JD to Gregorian calendar
71** eraD2tf decompose days to hms
72** eraDat delta(AT) = TAI-UTC
73**
74** Copyright (C) 2013-2017, NumFOCUS Foundation.
75** Derived, with permission, from the SOFA library. See notes at end of file.
76*/
77{
78 int leap;
79 char s;
80 int iy1, im1, id1, js, iy2, im2, id2, ihmsf1[4], i;
81 double a1, b1, fd, dat0, dat12, w, dat24, dleap;
82
83
84/* The two-part JD. */
85 a1 = d1;
86 b1 = d2;
87
88/* Provisional calendar date. */
89 js = eraJd2cal(a1, b1, &iy1, &im1, &id1, &fd);
90 if ( js ) return -1;
91
92/* Is this a leap second day? */
93 leap = 0;
94 if ( ! strcmp(scale,"UTC") ) {
95
96 /* TAI-UTC at 0h today. */
97 js = eraDat(iy1, im1, id1, 0.0, &dat0);
98 if ( js < 0 ) return -1;
99
100 /* TAI-UTC at 12h today (to detect drift). */
101 js = eraDat(iy1, im1, id1, 0.5, &dat12);
102 if ( js < 0 ) return -1;
103
104 /* TAI-UTC at 0h tomorrow (to detect jumps). */
105 js = eraJd2cal(a1+1.5, b1-fd, &iy2, &im2, &id2, &w);
106 if ( js ) return -1;
107 js = eraDat(iy2, im2, id2, 0.0, &dat24);
108 if ( js < 0 ) return -1;
109
110 /* Any sudden change in TAI-UTC (seconds). */
111 dleap = dat24 - (2.0*dat12 - dat0);
112
113 /* If leap second day, scale the fraction of a day into SI. */
114 leap = (dleap != 0.0);
115 if (leap) fd += fd * dleap/ERFA_DAYSEC;
116 }
117
118/* Provisional time of day. */
119 eraD2tf ( ndp, fd, &s, ihmsf1 );
120
121/* Has the (rounded) time gone past 24h? */
122 if ( ihmsf1[0] > 23 ) {
123
124 /* Yes. We probably need tomorrow's calendar date. */
125 js = eraJd2cal(a1+1.5, b1-fd, &iy2, &im2, &id2, &w);
126 if ( js ) return -1;
127
128 /* Is today a leap second day? */
129 if ( ! leap ) {
130
131 /* No. Use 0h tomorrow. */
132 iy1 = iy2;
133 im1 = im2;
134 id1 = id2;
135 ihmsf1[0] = 0;
136 ihmsf1[1] = 0;
137 ihmsf1[2] = 0;
138
139 } else {
140
141 /* Yes. Are we past the leap second itself? */
142 if ( ihmsf1[2] > 0 ) {
143
144 /* Yes. Use tomorrow but allow for the leap second. */
145 iy1 = iy2;
146 im1 = im2;
147 id1 = id2;
148 ihmsf1[0] = 0;
149 ihmsf1[1] = 0;
150 ihmsf1[2] = 0;
151
152 } else {
153
154 /* No. Use 23 59 60... today. */
155 ihmsf1[0] = 23;
156 ihmsf1[1] = 59;
157 ihmsf1[2] = 60;
158 }
159
160 /* If rounding to 10s or coarser always go up to new day. */
161 if ( ndp < 0 && ihmsf1[2] == 60 ) {
162 iy1 = iy2;
163 im1 = im2;
164 id1 = id2;
165 ihmsf1[0] = 0;
166 ihmsf1[1] = 0;
167 ihmsf1[2] = 0;
168 }
169 }
170 }
171
172/* Results. */
173 *iy = iy1;
174 *im = im1;
175 *id = id1;
176 for ( i = 0; i < 4; i++ ) {
177 ihmsf[i] = ihmsf1[i];
178 }
179
180/* Status. */
181 return js;
182
183}
184/*----------------------------------------------------------------------
185**
186**
187** Copyright (C) 2013-2017, NumFOCUS Foundation.
188** All rights reserved.
189**
190** This library is derived, with permission, from the International
191** Astronomical Union's "Standards of Fundamental Astronomy" library,
192** available from http://www.iausofa.org.
193**
194** The ERFA version is intended to retain identical functionality to
195** the SOFA library, but made distinct through different function and
196** file names, as set out in the SOFA license conditions. The SOFA
197** original has a role as a reference standard for the IAU and IERS,
198** and consequently redistribution is permitted only in its unaltered
199** state. The ERFA version is not subject to this restriction and
200** therefore can be included in distributions which do not support the
201** concept of "read only" software.
202**
203** Although the intent is to replicate the SOFA API (other than
204** replacement of prefix names) and results (with the exception of
205** bugs; any that are discovered will be fixed), SOFA is not
206** responsible for any errors found in this version of the library.
207**
208** If you wish to acknowledge the SOFA heritage, please acknowledge
209** that you are using a library derived from SOFA, rather than SOFA
210** itself.
211**
212**
213** TERMS AND CONDITIONS
214**
215** Redistribution and use in source and binary forms, with or without
216** modification, are permitted provided that the following conditions
217** are met:
218**
219** 1 Redistributions of source code must retain the above copyright
220** notice, this list of conditions and the following disclaimer.
221**
222** 2 Redistributions in binary form must reproduce the above copyright
223** notice, this list of conditions and the following disclaimer in
224** the documentation and/or other materials provided with the
225** distribution.
226**
227** 3 Neither the name of the Standards Of Fundamental Astronomy Board,
228** the International Astronomical Union nor the names of its
229** contributors may be used to endorse or promote products derived
230** from this software without specific prior written permission.
231**
232** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
233** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
234** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
235** FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
236** COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
237** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
238** BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
239** LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
240** CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
241** LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
242** ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
243** POSSIBILITY OF SUCH DAMAGE.
244**
245*/
Note: See TracBrowser for help on using the repository browser.