source: trunk/FACT++/sofa/src/d2dtf.c@ 18355

Last change on this file since 18355 was 18346, checked in by tbretz, 11 years ago
File size: 9.6 KB
Line 
1#include "sofa.h"
2#include <string.h>
3
4int iauD2dtf(const char *scale, int ndp, double d1, double d2,
5 int *iy, int *im, int *id, int ihmsf[4])
6/*
7** - - - - - - - - -
8** i a u 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** This function is part of the International Astronomical Union's
15** SOFA (Standards of Fundamental Astronomy) software collection.
16**
17** Status: support function.
18**
19** Given:
20** scale char[] time scale ID (Note 1)
21** ndp int resolution (Note 2)
22** d1,d2 double time as a 2-part Julian Date (Notes 3,4)
23**
24** Returned:
25** iy,im,id int year, month, day in Gregorian calendar (Note 5)
26** ihmsf int[4] hours, minutes, seconds, fraction (Note 1)
27**
28** Returned (function value):
29** int status: +1 = dubious year (Note 5)
30** 0 = OK
31** -1 = unacceptable date (Note 6)
32**
33** Notes:
34**
35** 1) scale identifies the time scale. Only the value "UTC" (in upper
36** case) is significant, and enables handling of leap seconds (see
37** Note 4).
38**
39** 2) ndp is the number of decimal places in the seconds field, and can
40** have negative as well as positive values, such as:
41**
42** ndp resolution
43** -4 1 00 00
44** -3 0 10 00
45** -2 0 01 00
46** -1 0 00 10
47** 0 0 00 01
48** 1 0 00 00.1
49** 2 0 00 00.01
50** 3 0 00 00.001
51**
52** The limits are platform dependent, but a safe range is -5 to +9.
53**
54** 3) d1+d2 is Julian Date, apportioned in any convenient way between
55** the two arguments, for example where d1 is the Julian Day Number
56** and d2 is the fraction of a day. In the case of UTC, where the
57** use of JD is problematical, special conventions apply: see the
58** next note.
59**
60** 4) JD cannot unambiguously represent UTC during a leap second unless
61** special measures are taken. The SOFA internal convention is that
62** the quasi-JD day represents UTC days whether the length is 86399,
63** 86400 or 86401 SI seconds. In the 1960-1972 era there were
64** smaller jumps (in either direction) each time the linear UTC(TAI)
65** expression was changed, and these "mini-leaps" are also included
66** in the SOFA convention.
67**
68** 5) The warning status "dubious year" flags UTCs that predate the
69** introduction of the time scale or that are too far in the future
70** to be trusted. See iauDat for further details.
71**
72** 6) For calendar conventions and limitations, see iauCal2jd.
73**
74** Called:
75** iauJd2cal JD to Gregorian calendar
76** iauD2tf decompose days to hms
77** iauDat delta(AT) = TAI-UTC
78**
79** This revision: 2014 February 15
80**
81** SOFA release 2015-02-09
82**
83** Copyright (C) 2015 IAU SOFA Board. See notes at end.
84*/
85{
86 int leap;
87 char s;
88 int iy1, im1, id1, js, iy2, im2, id2, ihmsf1[4], i;
89 double a1, b1, fd, dat0, dat12, w, dat24, dleap;
90
91/* The two-part JD. */
92 a1 = d1;
93 b1 = d2;
94
95/* Provisional calendar date. */
96 js = iauJd2cal(a1, b1, &iy1, &im1, &id1, &fd);
97 if ( js ) return -1;
98
99/* Is this a leap second day? */
100 leap = 0;
101 if ( ! strcmp(scale,"UTC") ) {
102
103 /* TAI-UTC at 0h today. */
104 js = iauDat(iy1, im1, id1, 0.0, &dat0);
105 if ( js < 0 ) return -1;
106
107 /* TAI-UTC at 12h today (to detect drift). */
108 js = iauDat(iy1, im1, id1, 0.5, &dat12);
109 if ( js < 0 ) return -1;
110
111 /* TAI-UTC at 0h tomorrow (to detect jumps). */
112 js = iauJd2cal(a1+1.5, b1-fd, &iy2, &im2, &id2, &w);
113 if ( js ) return -1;
114 js = iauDat(iy2, im2, id2, 0.0, &dat24);
115 if ( js < 0 ) return -1;
116
117 /* Any sudden change in TAI-UTC (seconds). */
118 dleap = dat24 - (2.0*dat12 - dat0);
119
120 /* If leap second day, scale the fraction of a day into SI. */
121 leap = (dleap != 0.0);
122 if (leap) fd += fd * dleap/DAYSEC;
123 }
124
125/* Provisional time of day. */
126 iauD2tf ( ndp, fd, &s, ihmsf1 );
127
128/* Has the (rounded) time gone past 24h? */
129 if ( ihmsf1[0] > 23 ) {
130
131 /* Yes. We probably need tomorrow's calendar date. */
132 js = iauJd2cal(a1+1.5, b1-fd, &iy2, &im2, &id2, &w);
133 if ( js ) return -1;
134
135 /* Is today a leap second day? */
136 if ( ! leap ) {
137
138 /* No. Use 0h tomorrow. */
139 iy1 = iy2;
140 im1 = im2;
141 id1 = id2;
142 ihmsf1[0] = 0;
143 ihmsf1[1] = 0;
144 ihmsf1[2] = 0;
145
146 } else {
147
148 /* Yes. Are we past the leap second itself? */
149 if ( ihmsf1[2] > 0 ) {
150
151 /* Yes. Use tomorrow but allow for the leap second. */
152 iy1 = iy2;
153 im1 = im2;
154 id1 = id2;
155 ihmsf1[0] = 0;
156 ihmsf1[1] = 0;
157 ihmsf1[2] = 0;
158
159 } else {
160
161 /* No. Use 23 59 60... today. */
162 ihmsf1[0] = 23;
163 ihmsf1[1] = 59;
164 ihmsf1[2] = 60;
165 }
166
167 /* If rounding to 10s or coarser always go up to new day. */
168 if ( ndp < 0 && ihmsf1[2] == 60 ) {
169 iy1 = iy2;
170 im1 = im2;
171 id1 = id2;
172 ihmsf1[0] = 0;
173 ihmsf1[1] = 0;
174 ihmsf1[2] = 0;
175 }
176 }
177 }
178
179/* Results. */
180 *iy = iy1;
181 *im = im1;
182 *id = id1;
183 for ( i = 0; i < 4; i++ ) {
184 ihmsf[i] = ihmsf1[i];
185 }
186
187/* Status. */
188 return js;
189
190/*----------------------------------------------------------------------
191**
192** Copyright (C) 2015
193** Standards Of Fundamental Astronomy Board
194** of the International Astronomical Union.
195**
196** =====================
197** SOFA Software License
198** =====================
199**
200** NOTICE TO USER:
201**
202** BY USING THIS SOFTWARE YOU ACCEPT THE FOLLOWING SIX TERMS AND
203** CONDITIONS WHICH APPLY TO ITS USE.
204**
205** 1. The Software is owned by the IAU SOFA Board ("SOFA").
206**
207** 2. Permission is granted to anyone to use the SOFA software for any
208** purpose, including commercial applications, free of charge and
209** without payment of royalties, subject to the conditions and
210** restrictions listed below.
211**
212** 3. You (the user) may copy and distribute SOFA source code to others,
213** and use and adapt its code and algorithms in your own software,
214** on a world-wide, royalty-free basis. That portion of your
215** distribution that does not consist of intact and unchanged copies
216** of SOFA source code files is a "derived work" that must comply
217** with the following requirements:
218**
219** a) Your work shall be marked or carry a statement that it
220** (i) uses routines and computations derived by you from
221** software provided by SOFA under license to you; and
222** (ii) does not itself constitute software provided by and/or
223** endorsed by SOFA.
224**
225** b) The source code of your derived work must contain descriptions
226** of how the derived work is based upon, contains and/or differs
227** from the original SOFA software.
228**
229** c) The names of all routines in your derived work shall not
230** include the prefix "iau" or "sofa" or trivial modifications
231** thereof such as changes of case.
232**
233** d) The origin of the SOFA components of your derived work must
234** not be misrepresented; you must not claim that you wrote the
235** original software, nor file a patent application for SOFA
236** software or algorithms embedded in the SOFA software.
237**
238** e) These requirements must be reproduced intact in any source
239** distribution and shall apply to anyone to whom you have
240** granted a further right to modify the source code of your
241** derived work.
242**
243** Note that, as originally distributed, the SOFA software is
244** intended to be a definitive implementation of the IAU standards,
245** and consequently third-party modifications are discouraged. All
246** variations, no matter how minor, must be explicitly marked as
247** such, as explained above.
248**
249** 4. You shall not cause the SOFA software to be brought into
250** disrepute, either by misuse, or use for inappropriate tasks, or
251** by inappropriate modification.
252**
253** 5. The SOFA software is provided "as is" and SOFA makes no warranty
254** as to its use or performance. SOFA does not and cannot warrant
255** the performance or results which the user may obtain by using the
256** SOFA software. SOFA makes no warranties, express or implied, as
257** to non-infringement of third party rights, merchantability, or
258** fitness for any particular purpose. In no event will SOFA be
259** liable to the user for any consequential, incidental, or special
260** damages, including any lost profits or lost savings, even if a
261** SOFA representative has been advised of such damages, or for any
262** claim by any third party.
263**
264** 6. The provision of any version of the SOFA software under the terms
265** and conditions specified herein does not imply that future
266** versions will also be made available under the same terms and
267** conditions.
268*
269** In any published work or commercial product which uses the SOFA
270** software directly, acknowledgement (see www.iausofa.org) is
271** appreciated.
272**
273** Correspondence concerning SOFA software should be addressed as
274** follows:
275**
276** By email: sofa@ukho.gov.uk
277** By post: IAU SOFA Center
278** HM Nautical Almanac Office
279** UK Hydrographic Office
280** Admiralty Way, Taunton
281** Somerset, TA1 2DN
282** United Kingdom
283**
284**--------------------------------------------------------------------*/
285}
Note: See TracBrowser for help on using the repository browser.