1 #include "erfa.h"
2 
eraD2tf(int ndp,double days,char * sign,int ihmsf[4])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-2014, 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 
59 /* Handle sign. */
60    *sign = (char) ( ( days >= 0.0 ) ? '+' : '-' );
61 
62 /* Interval in seconds. */
63    a = ERFA_DAYSEC * fabs(days);
64 
65 /* Pre-round if resolution coarser than 1s (then pretend ndp=1). */
66    if (ndp < 0) {
67       nrs = 1;
68       for (n = 1; n <= -ndp; n++) {
69           nrs *= (n == 2 || n == 4) ? 6 : 10;
70       }
71       rs = (double) nrs;
72       w = a / rs;
73       a = rs * ERFA_DNINT(w);
74    }
75 
76 /* Express the unit of each field in resolution units. */
77    nrs = 1;
78    for (n = 1; n <= ndp; n++) {
79       nrs *= 10;
80    }
81    rs = (double) nrs;
82    rm = rs * 60.0;
83    rh = rm * 60.0;
84 
85 /* Round the interval and express in resolution units. */
86    a = ERFA_DNINT(rs * a);
87 
88 /* Break into fields. */
89    ah = a / rh;
90    ah = ERFA_DINT(ah);
91    a -= ah * rh;
92    am = a / rm;
93    am = ERFA_DINT(am);
94    a -= am * rm;
95    as = a / rs;
96    as = ERFA_DINT(as);
97    af = a - as * rs;
98 
99 /* Return results. */
100    ihmsf[0] = (int) ah;
101    ihmsf[1] = (int) am;
102    ihmsf[2] = (int) as;
103    ihmsf[3] = (int) af;
104 
105    return;
106 
107 }
108 /*----------------------------------------------------------------------
109 **
110 **
111 **  Copyright (C) 2013-2014, NumFOCUS Foundation.
112 **  All rights reserved.
113 **
114 **  This library is derived, with permission, from the International
115 **  Astronomical Union's "Standards of Fundamental Astronomy" library,
116 **  available from http://www.iausofa.org.
117 **
118 **  The ERFA version is intended to retain identical functionality to
119 **  the SOFA library, but made distinct through different function and
120 **  file names, as set out in the SOFA license conditions.  The SOFA
121 **  original has a role as a reference standard for the IAU and IERS,
122 **  and consequently redistribution is permitted only in its unaltered
123 **  state.  The ERFA version is not subject to this restriction and
124 **  therefore can be included in distributions which do not support the
125 **  concept of "read only" software.
126 **
127 **  Although the intent is to replicate the SOFA API (other than
128 **  replacement of prefix names) and results (with the exception of
129 **  bugs;  any that are discovered will be fixed), SOFA is not
130 **  responsible for any errors found in this version of the library.
131 **
132 **  If you wish to acknowledge the SOFA heritage, please acknowledge
133 **  that you are using a library derived from SOFA, rather than SOFA
134 **  itself.
135 **
136 **
137 **  TERMS AND CONDITIONS
138 **
139 **  Redistribution and use in source and binary forms, with or without
140 **  modification, are permitted provided that the following conditions
141 **  are met:
142 **
143 **  1 Redistributions of source code must retain the above copyright
144 **    notice, this list of conditions and the following disclaimer.
145 **
146 **  2 Redistributions in binary form must reproduce the above copyright
147 **    notice, this list of conditions and the following disclaimer in
148 **    the documentation and/or other materials provided with the
149 **    distribution.
150 **
151 **  3 Neither the name of the Standards Of Fundamental Astronomy Board,
152 **    the International Astronomical Union nor the names of its
153 **    contributors may be used to endorse or promote products derived
154 **    from this software without specific prior written permission.
155 **
156 **  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
157 **  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
158 **  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
159 **  FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
160 **  COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
161 **  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
162 **  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
163 **  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
164 **  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
165 **  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
166 **  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
167 **  POSSIBILITY OF SUCH DAMAGE.
168 **
169 */
170