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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
|
#include <math.h>
#include "project.h"
static int is_leap (unsigned year)
{
if (year % 4)
return 0;
if (year % 100)
return 1;
if (year % 400)
return 0;
return 1;
}
UTC time_epoch_to_utc (EPOCH epoch)
{
UTC u = {0};
uint64_t day;
unsigned y400, y100, y4;
day = epoch.s / 86400;
epoch.s -= day * 86400;
day += 134774;
u.wday = day % 7;
u.wday++;
y400 = day / 146097; /*146097 days in 400 years */
day -= (y400 * 146097);
y100 = day / 36524; /*36524 days in 100 years */
day -= (y100 * 36524);
y4 = day / 1461; /*1461 days in 4 years */
day -= (y4 * 1461);
/* This may look redundant but 31 Dec in year 4 is special case */
if (day < 1095) { /*1095 days in 3 years */
u.year = day / 365; /*365 days in a year */
day -= (365 * u.year);
} else {
u.year = 3;
day -= 1095;
}
/* Now put it all back together */
u.year += 1601;
u.year += 4 * y4;
u.year += 100 * y100;
u.year += 400 * y400;
u.jday = day + 1;
u.is_leap = is_leap (u.year);
if (!u.is_leap) {
/*Days and months for ordinary years */
if (u.jday < 32) {
u.month = 1;
u.mday = u.jday;
} else if (u.jday < 60) {
u.month = 2;
u.mday = u.jday - 31;
} else if (u.jday < 91) {
u.month = 3;
u.mday = u.jday - 59;
} else if (u.jday < 121) {
u.month = 4;
u.mday = u.jday - 90;
} else if (u.jday < 152) {
u.month = 5;
u.mday = u.jday - 120;
} else if (u.jday < 182) {
u.month = 6;
u.mday = u.jday - 151;
} else if (u.jday < 213) {
u.month = 7;
u.mday = u.jday - 181;
} else if (u.jday < 244) {
u.month = 8;
u.mday = u.jday - 212;
} else if (u.jday < 274) {
u.month = 9;
u.mday = u.jday - 243;
} else if (u.jday < 305) {
u.month = 10;
u.mday = u.jday - 273;
} else if (u.jday < 335) {
u.month = 11;
u.mday = u.jday - 304;
} else {
u.month = 12;
u.mday = u.jday - 334;
}
} else {
/*And leap years */
if (u.jday < 32) {
u.month = 1;
u.mday = u.jday;
} else if (u.jday < 61) {
u.month = 2;
u.mday = u.jday - 31;
} else if (u.jday < 92) {
u.month = 3;
u.mday = u.jday - 60;
} else if (u.jday < 122) {
u.month = 4;
u.mday = u.jday - 91;
} else if (u.jday < 153) {
u.month = 5;
u.mday = u.jday - 121;
} else if (u.jday < 183) {
u.month = 6;
u.mday = u.jday - 152;
} else if (u.jday < 214) {
u.month = 7;
u.mday = u.jday - 182;
} else if (u.jday < 245) {
u.month = 8;
u.mday = u.jday - 213;
} else if (u.jday < 275) {
u.month = 9;
u.mday = u.jday - 244;
} else if (u.jday < 306) {
u.month = 10;
u.mday = u.jday - 274;
} else if (u.jday < 336) {
u.month = 11;
u.mday = u.jday - 305;
} else {
u.month = 12;
u.mday = u.jday - 335;
}
}
u.hour = epoch.s / 3600;
epoch.s -= u.hour * 3600;
u.minute = epoch.s / 60;
epoch.s -= u.minute * 60;
u.second = epoch.s;
u.nanosecond = epoch.ns;
return u;
}
EPOCH time_utc_to_epoch (UTC u)
{
unsigned y400;
unsigned y100;
unsigned y4;
EPOCH ret;
static int const mdays[] =
{ 0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
static int const lmdays[] =
{ 0, 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335 };
u.is_leap = is_leap (u.year);
if (u.year < 100) u.year += 2000;
if (!u.jday) {
if (u.is_leap)
u.jday = u.mday + lmdays[u.month];
else
u.jday = u.mday + mdays[u.month];
}
u.year -= 1601;
y400 = u.year / 400;
u.year -= y400 * 400;
y100 = u.year / 100;
u.year -= y100 * 100;
y4 = u.year / 4;
u.year -= y4 * 4;
ret.s = u.jday - 1;
ret.s += u.year * 365;
ret.s += y4 * 1461;
ret.s += y100 * 36524;
ret.s += y400 * 146097;
ret.s -= 134774;
ret.s *= 86400;
ret.s += u.second;
ret.s += u.minute * 60;
ret.s += u.hour * 3600;
ret.ns = u.nanosecond;
return ret;
}
void utc_to_str (char *dst, UTC u)
{
const char *dname[] = {"Sun", "Mon", "Tue", "Wed", "Thr", "Fri", "Sat", "Sun"};
const char *mname[] = {"", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
sprintf (dst, "%s %04d-%s-%02d %02d:%02d:%02d.%09d", dname[u.wday], u.year, mname[u.month], u.mday, u.hour, u.minute, u.second, u.nanosecond);
}
void time_print_utc (const char *p, UTC u, const char *t)
{
const char *dname[] = {"Sun", "Mon", "Tue", "Wed", "Thr", "Fri", "Sat", "Sun"};
const char *mname[] = {"", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
printf ("%s%s %04d-%s-%02d %02d:%02d:%02d.%09d %s\r\n", p ? p : "", dname[u.wday], u.year, mname[u.month], u.mday, u.hour, u.minute, u.second, u.nanosecond, t ? t : "");
}
void time_print_epoch (const char *p, EPOCH e, const char *t)
{
UTC u = time_epoch_to_utc (e);
time_print_utc (p, u, t);
}
double
time_utc_to_tjd (UTC u)
{
unsigned y400;
unsigned y100;
unsigned y4;
double ret;
unsigned jd;
static int const mdays[] =
{ 0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
static int const lmdays[] =
{ 0, 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335 };
u.is_leap = is_leap (u.year);
if (u.year < 100)
u.year += 2000;
if (!u.jday) {
if (u.is_leap)
u.jday = u.mday + lmdays[u.month];
else
u.jday = u.mday + mdays[u.month];
}
u.year -= 1601;
y400 = u.year / 400;
u.year -= y400 * 400;
y100 = u.year / 100;
u.year -= y100 * 100;
y4 = u.year / 4;
u.year -= y4 * 4;
jd = u.jday - 1;
jd += u.year * 365;
jd += y4 * 1461;
jd += y100 * 36524;
jd += y400 * 146097;
jd += 2305813;
jd -= 2451545;
ret = (double) u.nanosecond;
ret /= 1000000000.;
ret += (double) u.second;
ret /= 60.;
ret += (double) u.minute;
ret /= 60.;
ret += (double) u.hour;
ret /= 24.;
ret += .5;
ret += (double) jd;
return ret;
}
double
time_utc_to_ra (UTC u)
{
double tjd = time_utc_to_tjd (u);
double T = tjd / 36525.;
double theta0 =
280.46061837 + (360.98564736629 * tjd) + (0.000387933 * T * T) -
(T * T * T / 38710000.0);
return remainder (theta0, 360.);
}
ST
time_ra_to_st (double ra)
{
ST ret;
unsigned i;
while (ra < 0.)
ra += 360.;
if (ra >= 360.0)
ra = remainder (ra, 360.);
ra *= 240.0;
i = (int) (floor (ra) + .5);
ra -= (double) i;
ra *= 1000000000.;
ret.nanosecond = (unsigned) (ra + .5);
ret.second = i % 60;
i /= 60;
ret.minute = i % 60;
i /= 60;
ret.hour = i;
return ret;
}
ST time_utc_to_lst (UTC u, double lon)
{
double ra;
ra = time_utc_to_ra (u);
return time_ra_to_st (ra + lon);
}
|