FACT++  1.0
d2tf.c
Go to the documentation of this file.
1 #include "erfa.h"
2 
3 void eraD2tf(int ndp, double days, char *sign, int ihmsf[4])
4 /*
5 ** - - - - - - - -
6 ** e r a D 2 t f
7 ** - - - - - - - -
8 **
9 ** Decompose days to hours, minutes, seconds, fraction.
10 **
11 ** Given:
12 ** ndp int resolution (Note 1)
13 ** days double interval in days
14 **
15 ** Returned:
16 ** sign char '+' or '-'
17 ** ihmsf int[4] hours, minutes, seconds, fraction
18 **
19 ** Notes:
20 **
21 ** 1) The argument ndp is interpreted as follows:
22 **
23 ** ndp resolution
24 ** : ...0000 00 00
25 ** -7 1000 00 00
26 ** -6 100 00 00
27 ** -5 10 00 00
28 ** -4 1 00 00
29 ** -3 0 10 00
30 ** -2 0 01 00
31 ** -1 0 00 10
32 ** 0 0 00 01
33 ** 1 0 00 00.1
34 ** 2 0 00 00.01
35 ** 3 0 00 00.001
36 ** : 0 00 00.000...
37 **
38 ** 2) The largest positive useful value for ndp is determined by the
39 ** size of days, the format of double on the target platform, and
40 ** the risk of overflowing ihmsf[3]. On a typical platform, for
41 ** days up to 1.0, the available floating-point precision might
42 ** correspond to ndp=12. However, the practical limit is typically
43 ** ndp=9, set by the capacity of a 32-bit int, or ndp=4 if int is
44 ** only 16 bits.
45 **
46 ** 3) The absolute value of days may exceed 1.0. In cases where it
47 ** does not, it is up to the caller to test for and handle the
48 ** case where days is very nearly 1.0 and rounds up to 24 hours,
49 ** by testing for ihmsf[0]=24 and setting ihmsf[0-3] to zero.
50 **
51 ** Copyright (C) 2013-2015, NumFOCUS Foundation.
52 ** Derived, with permission, from the SOFA library. See notes at end of file.
53 */
54 {
55  int nrs, n;
56  double rs, rm, rh, a, w, ah, am, as, af;
57 
58 /* Handle sign. */
59  *sign = (char) ( ( days >= 0.0 ) ? '+' : '-' );
60 
61 /* Interval in seconds. */
62  a = ERFA_DAYSEC * fabs(days);
63 
64 /* Pre-round if resolution coarser than 1s (then pretend ndp=1). */
65  if (ndp < 0) {
66  nrs = 1;
67  for (n = 1; n <= -ndp; n++) {
68  nrs *= (n == 2 || n == 4) ? 6 : 10;
69  }
70  rs = (double) nrs;
71  w = a / rs;
72  a = rs * ERFA_DNINT(w);
73  }
74 
75 /* Express the unit of each field in resolution units. */
76  nrs = 1;
77  for (n = 1; n <= ndp; n++) {
78  nrs *= 10;
79  }
80  rs = (double) nrs;
81  rm = rs * 60.0;
82  rh = rm * 60.0;
83 
84 /* Round the interval and express in resolution units. */
85  a = ERFA_DNINT(rs * a);
86 
87 /* Break into fields. */
88  ah = a / rh;
89  ah = ERFA_DINT(ah);
90  a -= ah * rh;
91  am = a / rm;
92  am = ERFA_DINT(am);
93  a -= am * rm;
94  as = a / rs;
95  as = ERFA_DINT(as);
96  af = a - as * rs;
97 
98 /* Return results. */
99  ihmsf[0] = (int) ah;
100  ihmsf[1] = (int) am;
101  ihmsf[2] = (int) as;
102  ihmsf[3] = (int) af;
103 
104  return;
105 
106 }
107 /*----------------------------------------------------------------------
108 **
109 **
110 ** Copyright (C) 2013-2015, NumFOCUS Foundation.
111 ** All rights reserved.
112 **
113 ** This library is derived, with permission, from the International
114 ** Astronomical Union's "Standards of Fundamental Astronomy" library,
115 ** available from http://www.iausofa.org.
116 **
117 ** The ERFA version is intended to retain identical functionality to
118 ** the SOFA library, but made distinct through different function and
119 ** file names, as set out in the SOFA license conditions. The SOFA
120 ** original has a role as a reference standard for the IAU and IERS,
121 ** and consequently redistribution is permitted only in its unaltered
122 ** state. The ERFA version is not subject to this restriction and
123 ** therefore can be included in distributions which do not support the
124 ** concept of "read only" software.
125 **
126 ** Although the intent is to replicate the SOFA API (other than
127 ** replacement of prefix names) and results (with the exception of
128 ** bugs; any that are discovered will be fixed), SOFA is not
129 ** responsible for any errors found in this version of the library.
130 **
131 ** If you wish to acknowledge the SOFA heritage, please acknowledge
132 ** that you are using a library derived from SOFA, rather than SOFA
133 ** itself.
134 **
135 **
136 ** TERMS AND CONDITIONS
137 **
138 ** Redistribution and use in source and binary forms, with or without
139 ** modification, are permitted provided that the following conditions
140 ** are met:
141 **
142 ** 1 Redistributions of source code must retain the above copyright
143 ** notice, this list of conditions and the following disclaimer.
144 **
145 ** 2 Redistributions in binary form must reproduce the above copyright
146 ** notice, this list of conditions and the following disclaimer in
147 ** the documentation and/or other materials provided with the
148 ** distribution.
149 **
150 ** 3 Neither the name of the Standards Of Fundamental Astronomy Board,
151 ** the International Astronomical Union nor the names of its
152 ** contributors may be used to endorse or promote products derived
153 ** from this software without specific prior written permission.
154 **
155 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
156 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
157 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
158 ** FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
159 ** COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
160 ** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
161 ** BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
162 ** LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
163 ** CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
164 ** LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
165 ** ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
166 ** POSSIBILITY OF SUCH DAMAGE.
167 **
168 */
#define ERFA_DINT(A)
Definition: erfam.h:125
#define ERFA_DAYSEC
Definition: erfam.h:75
#define ERFA_DNINT(A)
Definition: erfam.h:128
void eraD2tf(int ndp, double days, char *sign, int ihmsf[4])
Definition: d2tf.c:3