forked from brandon-rhodes/pyephem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformats.c
267 lines (242 loc) · 6.81 KB
/
formats.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#include "Python.h"
#include "astro.h"
#include "preferences.h"
#include <stdio.h>
#include <math.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
double ascii_strtod(const char *s00, char **se); /* for PyEphem */
/* sprint the variable a in sexagesimal format into out[].
* w is the number of spaces for the whole part.
* fracbase is the number of pieces a whole is to broken into; valid options:
* 360000: <w>:mm:ss.ss
* 36000: <w>:mm:ss.s
* 3600: <w>:mm:ss
* 600: <w>:mm.m
* 60: <w>:mm
* return number of characters written to out, not counting final '\0'.
*/
int
fs_sexa (char *out, double a, int w, int fracbase)
{
char *out0 = out;
unsigned long n;
int d;
int f;
int m;
int s;
int isneg;
/* save whether it's negative but do all the rest with a positive */
isneg = (a < 0);
if (isneg)
a = -a;
/* convert to an integral number of whole portions */
n = (unsigned long)(a * fracbase + 0.5);
d = n/fracbase;
f = n%fracbase;
/* form the whole part; "negative 0" is a special case */
if (isneg && d == 0)
out += sprintf (out, "%*s-0", w-2, "");
else
out += sprintf (out, "%*d", w, isneg ? -d : d);
/* do the rest */
switch (fracbase) {
case 60: /* dd:mm */
m = f/(fracbase/60);
out += sprintf (out, ":%02d", m);
break;
case 600: /* dd:mm.m */
out += sprintf (out, ":%02d.%1d", f/10, f%10);
break;
case 3600: /* dd:mm:ss */
m = f/(fracbase/60);
s = f%(fracbase/60);
out += sprintf (out, ":%02d:%02d", m, s);
break;
case 36000: /* dd:mm:ss.s*/
m = f/(fracbase/60);
s = f%(fracbase/60);
out += sprintf (out, ":%02d:%02d.%1d", m, s/10, s%10);
break;
case 360000: /* dd:mm:ss.ss */
m = f/(fracbase/60);
s = f%(fracbase/60);
out += sprintf (out, ":%02d:%02d.%02d", m, s/100, s%100);
break;
default:
printf ("fs_sexa: unknown fracbase: %d\n", fracbase);
abort();
}
return (out - out0);
}
/* put the given modified Julian date, jd, in out[] according to the given
* preference format.
* return number of characters written to out, not counting final '\0'.
*/
int
fs_date (char out[], int format, double jd)
{
char *out0 = out;
int m, y;
double d;
mjd_cal (jd, &m, &d, &y);
/* beware of %g rounding day up */
if ((d < 1.0 && d - floor(d) >= .9999995)
|| (d < 10.0 && d - floor(d) >= .999995)
|| (d >= 10.0 && d - floor(d) >= .99995))
mjd_cal (mjd_day(jd+0.5), &m, &d, &y);
switch (format) {
case PREF_YMD:
out += sprintf (out, "%4d/%02d/%02.6g", y, m, d);
break;
case PREF_DMY:
out += sprintf (out, "%2.6g/%02d/%-4d", d, m, y);
break;
case PREF_MDY:
out += sprintf (out, "%2d/%02.6g/%-4d", m, d, y);
break;
default:
printf ("fs_date: bad date pref: %d\n", format);
abort();
}
return (out - out0);
}
/* convert sexagesimal string str A:B:C to double.
* Any missing A, B or C will be assumed 0.
* optional - and + can be anywhere.
* return 0 if ok, -1 if can't find a thing or A, B or C are invalid numbers.
*/
int
f_scansexa (
const char *str0, /* input string */
double *dp) /* cracked value, if return 0 */
{
double a, b, c;
char str[256];
char *neg, *s, *end;
int isneg, status;
/* copy str0 so we can play with it */
strncpy (str, str0, sizeof(str)-1);
str[sizeof(str)-1] = '\0';
/* note first negative (but not fooled by neg exponent) */
isneg = 0;
neg = strchr(str, '-');
if (neg && (neg == str || (neg[-1] != 'E' && neg[-1] != 'e'))) {
*neg = ' ';
isneg = 1;
}
/* These three calls replace an old, locale-sensitive sscanf.
Note that, per the semantics of the strtod call, if we run
out of valid numbers to parse, then the last few values will
just get zero. */
status = 0;
s = str;
a = ascii_strtod(s, &end);
if (end == s) { /* since a will be -1 */
a = 0.0;
/* don't fail if A is an empty string */
if ((*end != ':') && (*end != '\0')) status = -1;
}
s = end;
if (*s == ':') s++;
b = ascii_strtod(s, &end);
if (end == s) { /* since b will be -1 */
b = 0.0;
/* don't fail if B is an empty string */
if ((*end != ':') && (*end != '\0')) status = -1;
}
s = end;
if (*s == ':') s++;
c = ascii_strtod(s, &end);
if (end == s) { /* since c will be -1 */
c = 0.0;
/* don't fail if C is an empty string */
if ((*end != ':') && (*end != '\0')) status = -1;
}
/* back to one value, restoring neg */
*dp = a + b/60.0 + c/3600.0;
if (isneg)
*dp *= -1;
return status;
}
/* crack a floating date string, bp, of the form X/Y/Z determined by the
* PREF_DATE_FORMAT preference into its components. allow the day to be a
* floating point number,
* the slashes may also be spaces or colons.
* a lone component with a decimal point is considered a year.
* Brandon Rhodes: 2011-11-24 supplemented this to allow dash separators.
*/
void
f_sscandate (
char *bp,
int pref, /* one of PREF_X for PREF_DATE_FORMAT */
int *m,
double *d,
int *y)
{
double X,Y,Z; /* the three components */
char *s, *end;
int n;
X = Y = Z = 0.0;
/* This replaces an old, locale-sensitive sscanf(). */
X = ascii_strtod(bp, &end);
if (bp == end) {
n = 0;
X = 0.0; /* X will be -1 */
} else {
s = end;
if (*s == '-' || *s == '/' || *s == ':') s++;
Y = ascii_strtod(s, &end);
if (s == end) {
n = 1;
Y = 0.0; /* Y will be -1 */
} else {
s = end;
if (*s == '-' || *s == '/' || *s == ':') s++;
Z = ascii_strtod(s, &end);
if (s == end) {
n = 2;
Z = 0.0; /* Z will be -1 */
} else {
n = 3;
}
}
}
if (n == 1 && (strchr(bp, '.')
|| (pref == PREF_MDY && (X < 1 || X > 12))
|| (pref == PREF_DMY && (X < 1 || X > 31)))) {
double Mjd;
year_mjd (X, &Mjd);
mjd_cal (Mjd, m, d, y);
} else {
switch (pref) {
case PREF_MDY:
if (n > 0 && X != 0)
*m = (int)X;
if (n > 1 && Y != 0)
*d = Y;
if (n > 2 && Z != 0)
*y = (int)Z;
break;
case PREF_YMD:
if (n > 0 && X != 0)
*y = (int)X;
if (n > 1 && Y != 0)
*m = (int)Y;
if (n > 2 && Z != 0)
*d = Z;
break;
case PREF_DMY:
if (n > 0 && X != 0)
*d = X;
if (n > 1 && Y != 0)
*m = (int)Y;
if (n > 2 && Z != 0)
*y = (int)Z;
break;
}
}
}
/* For RCS Only -- Do Not Edit */
static char *rcsid[2] = {(char *)rcsid, "@(#) $RCSfile: formats.c,v $ $Date: 2006/04/10 09:00:06 $ $Revision: 1.17 $ $Name: $"};