FACT++  1.0
d2dtf.c
Go to the documentation of this file.
1 #include "erfa.h"
2 #include <string.h>
3 
4 int 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-2015, 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 /* The two-part JD. */
84  a1 = d1;
85  b1 = d2;
86 
87 /* Provisional calendar date. */
88  js = eraJd2cal(a1, b1, &iy1, &im1, &id1, &fd);
89  if ( js ) return -1;
90 
91 /* Is this a leap second day? */
92  leap = 0;
93  if ( ! strcmp(scale,"UTC") ) {
94 
95  /* TAI-UTC at 0h today. */
96  js = eraDat(iy1, im1, id1, 0.0, &dat0);
97  if ( js < 0 ) return -1;
98 
99  /* TAI-UTC at 12h today (to detect drift). */
100  js = eraDat(iy1, im1, id1, 0.5, &dat12);
101  if ( js < 0 ) return -1;
102 
103  /* TAI-UTC at 0h tomorrow (to detect jumps). */
104  js = eraJd2cal(a1+1.5, b1-fd, &iy2, &im2, &id2, &w);
105  if ( js ) return -1;
106  js = eraDat(iy2, im2, id2, 0.0, &dat24);
107  if ( js < 0 ) return -1;
108 
109  /* Any sudden change in TAI-UTC (seconds). */
110  dleap = dat24 - (2.0*dat12 - dat0);
111 
112  /* If leap second day, scale the fraction of a day into SI. */
113  leap = (dleap != 0.0);
114  if (leap) fd += fd * dleap/ERFA_DAYSEC;
115  }
116 
117 /* Provisional time of day. */
118  eraD2tf ( ndp, fd, &s, ihmsf1 );
119 
120 /* Has the (rounded) time gone past 24h? */
121  if ( ihmsf1[0] > 23 ) {
122 
123  /* Yes. We probably need tomorrow's calendar date. */
124  js = eraJd2cal(a1+1.5, b1-fd, &iy2, &im2, &id2, &w);
125  if ( js ) return -1;
126 
127  /* Is today a leap second day? */
128  if ( ! leap ) {
129 
130  /* No. Use 0h tomorrow. */
131  iy1 = iy2;
132  im1 = im2;
133  id1 = id2;
134  ihmsf1[0] = 0;
135  ihmsf1[1] = 0;
136  ihmsf1[2] = 0;
137 
138  } else {
139 
140  /* Yes. Are we past the leap second itself? */
141  if ( ihmsf1[2] > 0 ) {
142 
143  /* Yes. Use tomorrow but allow for the leap second. */
144  iy1 = iy2;
145  im1 = im2;
146  id1 = id2;
147  ihmsf1[0] = 0;
148  ihmsf1[1] = 0;
149  ihmsf1[2] = 0;
150 
151  } else {
152 
153  /* No. Use 23 59 60... today. */
154  ihmsf1[0] = 23;
155  ihmsf1[1] = 59;
156  ihmsf1[2] = 60;
157  }
158 
159  /* If rounding to 10s or coarser always go up to new day. */
160  if ( ndp < 0 && ihmsf1[2] == 60 ) {
161  iy1 = iy2;
162  im1 = im2;
163  id1 = id2;
164  ihmsf1[0] = 0;
165  ihmsf1[1] = 0;
166  ihmsf1[2] = 0;
167  }
168  }
169  }
170 
171 /* Results. */
172  *iy = iy1;
173  *im = im1;
174  *id = id1;
175  for ( i = 0; i < 4; i++ ) {
176  ihmsf[i] = ihmsf1[i];
177  }
178 
179 /* Status. */
180  return js;
181 
182 }
183 /*----------------------------------------------------------------------
184 **
185 **
186 ** Copyright (C) 2013-2015, NumFOCUS Foundation.
187 ** All rights reserved.
188 **
189 ** This library is derived, with permission, from the International
190 ** Astronomical Union's "Standards of Fundamental Astronomy" library,
191 ** available from http://www.iausofa.org.
192 **
193 ** The ERFA version is intended to retain identical functionality to
194 ** the SOFA library, but made distinct through different function and
195 ** file names, as set out in the SOFA license conditions. The SOFA
196 ** original has a role as a reference standard for the IAU and IERS,
197 ** and consequently redistribution is permitted only in its unaltered
198 ** state. The ERFA version is not subject to this restriction and
199 ** therefore can be included in distributions which do not support the
200 ** concept of "read only" software.
201 **
202 ** Although the intent is to replicate the SOFA API (other than
203 ** replacement of prefix names) and results (with the exception of
204 ** bugs; any that are discovered will be fixed), SOFA is not
205 ** responsible for any errors found in this version of the library.
206 **
207 ** If you wish to acknowledge the SOFA heritage, please acknowledge
208 ** that you are using a library derived from SOFA, rather than SOFA
209 ** itself.
210 **
211 **
212 ** TERMS AND CONDITIONS
213 **
214 ** Redistribution and use in source and binary forms, with or without
215 ** modification, are permitted provided that the following conditions
216 ** are met:
217 **
218 ** 1 Redistributions of source code must retain the above copyright
219 ** notice, this list of conditions and the following disclaimer.
220 **
221 ** 2 Redistributions in binary form must reproduce the above copyright
222 ** notice, this list of conditions and the following disclaimer in
223 ** the documentation and/or other materials provided with the
224 ** distribution.
225 **
226 ** 3 Neither the name of the Standards Of Fundamental Astronomy Board,
227 ** the International Astronomical Union nor the names of its
228 ** contributors may be used to endorse or promote products derived
229 ** from this software without specific prior written permission.
230 **
231 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
232 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
233 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
234 ** FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
235 ** COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
236 ** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
237 ** BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
238 ** LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
239 ** CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
240 ** LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
241 ** ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
242 ** POSSIBILITY OF SUCH DAMAGE.
243 **
244 */
int eraD2dtf(const char *scale, int ndp, double d1, double d2, int *iy, int *im, int *id, int ihmsf[4])
Definition: d2dtf.c:4
int i
Definition: db_dim_client.c:21
#define ERFA_DAYSEC
Definition: erfam.h:75
int eraJd2cal(double dj1, double dj2, int *iy, int *im, int *id, double *fd)
Definition: jd2cal.c:3
int eraDat(int iy, int im, int id, double fd, double *deltat)
Definition: dat.c:3
void eraD2tf(int ndp, double days, char *sign, int ihmsf[4])
Definition: d2tf.c:3