FACT++  1.0
dtf2d.c
Go to the documentation of this file.
1 #include "erfa.h"
2 #include <string.h>
3 
4 int 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-2015, 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 /* Today's Julian Day Number. */
86  js = eraCal2jd(iy, im, id, &dj, &w);
87  if ( js ) return js;
88  dj += w;
89 
90 /* Day length and final minute length in seconds (provisional). */
91  day = ERFA_DAYSEC;
92  seclim = 60.0;
93 
94 /* Deal with the UTC leap second case. */
95  if ( ! strcmp(scale,"UTC") ) {
96 
97  /* TAI-UTC at 0h today. */
98  js = eraDat(iy, im, id, 0.0, &dat0);
99  if ( js < 0 ) return js;
100 
101  /* TAI-UTC at 12h today (to detect drift). */
102  js = eraDat(iy, im, id, 0.5, &dat12);
103  if ( js < 0 ) return js;
104 
105  /* TAI-UTC at 0h tomorrow (to detect jumps). */
106  js = eraJd2cal ( dj, 1.5, &iy2, &im2, &id2, &w);
107  if ( js ) return js;
108  js = eraDat(iy2, im2, id2, 0.0, &dat24);
109  if ( js < 0 ) return js;
110 
111  /* Any sudden change in TAI-UTC between today and tomorrow. */
112  dleap = dat24 - (2.0*dat12 - dat0);
113 
114  /* If leap second day, correct the day and final minute lengths. */
115  day += dleap;
116  if ( ihr == 23 && imn == 59 ) seclim += dleap;
117 
118  /* End of UTC-specific actions. */
119  }
120 
121 /* Validate the time. */
122  if ( ihr >= 0 && ihr <= 23 ) {
123  if ( imn >= 0 && imn <= 59 ) {
124  if ( sec >= 0 ) {
125  if ( sec >= seclim ) {
126  js += 2;
127  }
128  } else {
129  js = -6;
130  }
131  } else {
132  js = -5;
133  }
134  } else {
135  js = -4;
136  }
137  if ( js < 0 ) return js;
138 
139 /* The time in days. */
140  time = ( 60.0 * ( (double) ( 60 * ihr + imn ) ) + sec ) / day;
141 
142 /* Return the date and time. */
143  *d1 = dj;
144  *d2 = time;
145 
146 /* Status. */
147  return js;
148 
149 }
150 /*----------------------------------------------------------------------
151 **
152 **
153 ** Copyright (C) 2013-2015, NumFOCUS Foundation.
154 ** All rights reserved.
155 **
156 ** This library is derived, with permission, from the International
157 ** Astronomical Union's "Standards of Fundamental Astronomy" library,
158 ** available from http://www.iausofa.org.
159 **
160 ** The ERFA version is intended to retain identical functionality to
161 ** the SOFA library, but made distinct through different function and
162 ** file names, as set out in the SOFA license conditions. The SOFA
163 ** original has a role as a reference standard for the IAU and IERS,
164 ** and consequently redistribution is permitted only in its unaltered
165 ** state. The ERFA version is not subject to this restriction and
166 ** therefore can be included in distributions which do not support the
167 ** concept of "read only" software.
168 **
169 ** Although the intent is to replicate the SOFA API (other than
170 ** replacement of prefix names) and results (with the exception of
171 ** bugs; any that are discovered will be fixed), SOFA is not
172 ** responsible for any errors found in this version of the library.
173 **
174 ** If you wish to acknowledge the SOFA heritage, please acknowledge
175 ** that you are using a library derived from SOFA, rather than SOFA
176 ** itself.
177 **
178 **
179 ** TERMS AND CONDITIONS
180 **
181 ** Redistribution and use in source and binary forms, with or without
182 ** modification, are permitted provided that the following conditions
183 ** are met:
184 **
185 ** 1 Redistributions of source code must retain the above copyright
186 ** notice, this list of conditions and the following disclaimer.
187 **
188 ** 2 Redistributions in binary form must reproduce the above copyright
189 ** notice, this list of conditions and the following disclaimer in
190 ** the documentation and/or other materials provided with the
191 ** distribution.
192 **
193 ** 3 Neither the name of the Standards Of Fundamental Astronomy Board,
194 ** the International Astronomical Union nor the names of its
195 ** contributors may be used to endorse or promote products derived
196 ** from this software without specific prior written permission.
197 **
198 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
199 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
200 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
201 ** FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
202 ** COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
203 ** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
204 ** BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
205 ** LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
206 ** CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
207 ** LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
208 ** ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
209 ** POSSIBILITY OF SUCH DAMAGE.
210 **
211 */
int eraDtf2d(const char *scale, int iy, int im, int id, int ihr, int imn, double sec, double *d1, double *d2)
Definition: dtf2d.c:4
#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
Warning because the service this data corrsponds to might have been last updated longer ago than Local time
Definition: smartfact.txt:92
int eraCal2jd(int iy, int im, int id, double *djm0, double *djm)
Definition: cal2jd.c:3