-
Notifications
You must be signed in to change notification settings - Fork 3
/
test.c
389 lines (346 loc) · 10.4 KB
/
test.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
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
#include <err.h>
#include <math.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include "calendar.h"
#include "basics.h"
#include "chinese.h"
#include "ecclesiastical.h"
#include "gregorian.h"
#include "julian.h"
#include "moon.h"
#include "parsedata.h"
#include "sun.h"
#include "utils.h"
/*
* globals
* for compatible with calendar.c ... files
*/
struct cal_options Options;
struct calendar *Calendar;
const char *calendarDirs[] = { NULL };
bool set_calendar(const char *name __unused) { return true; }
static void
test1(void)
{
char buf[256];
/*
int rds[] = { -214193, -61387, 25469, 49217, 171307, 210155,
253427, 369740, 400085, 434355, 452605, 470160,
473837, 507850, 524156, 544676, 567118, 569477,
601716, 613424, 626596, 645554, 664224, 671401,
694799, 704424, 708842, 709409, 709580, 727274,
728714, 744313, 764652 };
*/
int rds[] = { -214193, 253427, 473837, 601716, 694799, 728714 };
int rd, rd2;
struct date date, date2;
double c;
printf("R.D.\t(Y, M, D)\tRD2\tEq?\tJcen\n");
for (size_t i = 0; i < nitems(rds); i ) {
rd = rds[i];
gregorian_from_fixed(rd, &date);
rd2 = fixed_from_gregorian(&date);
c = julian_centuries(rd);
printf("}\t(M, -, -)\t}\t%d\t.6lf\n",
rd, date.year, date.month, date.day, rd2,
rd==rd2, c);
}
int dow;
printf("\nR.D.\tDoW\tJulian(Y,M,D)\tRD2\tEq?\n");
for (size_t i = 0; i < nitems(rds); i ) {
rd = rds[i];
dow = dayofweek_from_fixed(rd);
julian_from_fixed(rd, &date);
rd2 = fixed_from_julian(&date);
printf("}\t%d\t(M, -, -)\t}\t%d\n",
rd, dow, date.year, date.month, date.day,
rd2, rd==rd2);
}
printf("\nR.D.\tEasterJ\tEasterJ(Y,M,D)\tEasterG\tEasterG(Y,M,D)\n");
for (size_t i = 0; i < nitems(rds); i ) {
rd = rds[i];
int year = gregorian_year_from_fixed(rd);
int j_easter = orthodox_easter(year);
int g_easter = easter(year);
gregorian_from_fixed(j_easter, &date);
gregorian_from_fixed(g_easter, &date2);
printf("}\t}\t(M, -, -)\t}\t(M, -, -)\n",
rd, j_easter, date.year, date.month, date.day,
g_easter, date2.year, date2.month, date2.day);
}
double t;
double ephemeris;
double eot;
double solar_lon;
double lambda, next_se;
printf("\nR.D.\tEphemeris\tEqOfTime\tSolarLongitude\tNextSolstice/Equinox\tS/E/deg\n");
for (size_t i = 0; i < nitems(rds); i ) {
rd = rds[i];
t = (double)rd;
ephemeris = ephemeris_correction(t);
eot = equation_of_time(t);
solar_lon = solar_longitude(t 0.5); // at 12:00:00 UT
lambda = mod_f(ceil(solar_lon / 90) * 90, 360);
next_se = solar_longitude_atafter(lambda, t);
printf("}\t%.8lf\t.8lf\t.8lf\t.8lf\t%6.2lf\n",
rd, ephemeris, eot, solar_lon, next_se, lambda);
}
/* Location of Jerusalem (Eq. 14.4) */
const struct location jerusalem = {
.latitude = 31.78,
.longitude = 35.24,
.elevation = 740.0,
.zone = 2.0 / 24.0,
};
double t_sunset;
printf("\nR.D.\tSunset[Jerusalem]\tSS[hh:mm:ss]\n");
for (size_t i = 0; i < nitems(rds); i ) {
rd = rds[i];
t_sunset = sunset(rd, &jerusalem) - (double)rd;
format_time(buf, sizeof(buf), t_sunset);
printf("}\t%.8lf\t\t%s\n", rd, t_sunset, buf);
}
/* Location of Mecca (Eq. 14.3) */
const struct location mecca = {
.latitude = angle2deg(21, 25, 24),
.longitude = angle2deg(39, 49, 24),
.elevation = 298.0,
.zone = 3.0 / 24.0,
};
double l_lon, l_lat, l_alt, l_dis;
printf("\nR.D.\tLunarLongitude\tLunarLatitude\tLunarAltitude\tLunarDistance\n");
for (size_t i = 0; i < nitems(rds); i ) {
rd = rds[i];
t = (double)rd;
l_lon = lunar_longitude(t);
l_lat = lunar_latitude(t);
l_alt = lunar_altitude(t, mecca.latitude, mecca.longitude);
l_dis = lunar_distance(t);
printf("}\t.8lf\t.8lf\t.8lf\tg\n",
rd, l_lon, l_lat, l_alt, l_dis);
}
double t_newmoon, t_moonrise, t_moonset;
printf("\nR.D.\tNextNewMoon[Mecca]\tMoonrise\tMR[hh:mm:ss]\tMoonset\t\tMS[hh:mm:ss]\n");
for (size_t i = 0; i < nitems(rds); i ) {
rd = rds[i];
t = (double)rd;
t_newmoon = new_moon_atafter(t);
t_moonrise = moonrise(rd, &mecca) - (double)rd;
t_moonset = moonset(rd, &mecca) - (double)rd;
printf("}\t.8lf\t", rd, t_newmoon);
if (isnan(t_moonrise)) {
printf("s\t%8s", "(null)", "(null)");
} else {
format_time(buf, sizeof(buf), t_moonrise);
printf(".8lf\t%s", t_moonrise, buf);
}
if (isnan(t_moonset)) {
printf("\ts\t%8s\n", "(null)", "(null)");
} else {
format_time(buf, sizeof(buf), t_moonset);
printf("\t.8lf\t%s\n", t_moonset, buf);
}
}
double ob = obliquity(rd);
double ra = right_ascension(rd, 30, 50);
double dec = declination(rd, 30, 50);
double st = sidereal_from_moment(rd);
printf("\nobliquity = %.12lf\n", ob);
printf("ra = %.12lf; dec = %.12lf\n", ra, dec);
printf("sidereal_time = %.12lf\n", st);
struct date zh_epoch_date = { -2636, 2, 15 };
int zh_epoch = fixed_from_gregorian(&zh_epoch_date);
printf("\nchinese_epoch: RD (%d) <-> Gregorian (%d, %d, %d)\n",
zh_epoch, zh_epoch_date.year,
zh_epoch_date.month, zh_epoch_date.day);
struct chinese_date zh_date;
printf("\nR.D.\t(C, Y, M, L, D)\t\tRD2\tEq?\n");
for (size_t i = 0; i < nitems(rds); i ) {
rd = rds[i];
chinese_from_fixed(rd, &zh_date);
rd2 = fixed_from_chinese(&zh_date);
printf("}\t(-, -, -%c, -)\t}\t%d\n",
rd, zh_date.cycle, zh_date.year, zh_date.month,
zh_date.leap ? ' ' : ' ', zh_date.day, rd2, rd==rd2);
}
time_t tt;
struct tm tm;
printf("\n-----------------------------------------------------------\n");
time(&tt);
localtime_r(&tt, &tm);
date = (struct date){ tm.tm_year 1900, tm.tm_mon 1, tm.tm_mday };
rd = fixed_from_gregorian(&date);
chinese_from_fixed(rd, &zh_date);
printf("R.D.: %d\n", rd);
printf("Gregorian: M-d-d\n", date.year, date.month, date.day);
printf("Chinese: cycle=-, year=-, month=-%s, day=-\n",
zh_date.cycle, zh_date.year, zh_date.month,
zh_date.leap ? "(leap)" : "", zh_date.day);
double t_day = (tm.tm_hour tm.tm_min/60.0 tm.tm_sec/3600.0) / 24.0;
printf("Time: d:d:d\n", tm.tm_hour, tm.tm_min, tm.tm_sec);
const struct location shanghai = {
.latitude = angle2deg(31, 13, 43),
.longitude = angle2deg(121, 28, 29),
.elevation = 4.0,
.zone = 8.0/24.0,
};
printf("\n-----------------------------------------------------------\n");
show_chinese_calendar(rd);
printf("\n...........................................................\n");
show_sun_info(rd t_day, &shanghai);
printf("\n...........................................................\n");
show_moon_info(rd t_day, &shanghai);
printf("\n-----------------------------------------------------------\n");
date = (struct date){ 2033, 12, 25 };
rd = fixed_from_gregorian(&date);
show_chinese_calendar(rd);
printf("\n...........................................................\n");
show_sun_info(rd t_day, &shanghai);
printf("\n...........................................................\n");
show_moon_info(rd t_day, &shanghai);
}
static void
test2()
{
int year1 = 2000, year2 = 2007;
int rd;
struct date date;
printf("\n-----------------------------------------------------------\n");
/* US Daylight Saving Start; Eq.(2.39) */
printf("US Daylight Saving Start:\t");
for (int y = year1; y <= year2; y ) {
date.year = y;
date.month = 3;
date.day = 1;
rd = nth_kday(2, SUNDAY, &date);
gregorian_from_fixed(rd, &date);
printf("d/d/d\t", date.year, date.month, date.day);
}
printf("\n");
/* US Daylight Saving End; Eq.(2.40) */
printf("US Daylight Saving End:\t");
for (int y = year1; y <= year2; y ) {
date.year = y;
date.month = 11;
date.day = 1;
rd = nth_kday(1, SUNDAY, &date);
gregorian_from_fixed(rd, &date);
printf("d/d/d\t", date.year, date.month, date.day);
}
printf("\n");
/* US Election Day; Eq.(2.38) */
printf("US Election Day:\t");
for (int y = year1; y <= year2; y ) {
date.year = y;
date.month = 11;
date.day = 2;
rd = nth_kday(1, TUESDAY, &date);
gregorian_from_fixed(rd, &date);
printf("d/d/d\t", date.year, date.month, date.day);
}
printf("\n");
/* US Memorial Day; Eq.(2.37) */
printf("US Memorial Day:\t");
for (int y = year1; y <= year2; y ) {
date.year = y;
date.month = 5;
date.day = 31;
rd = nth_kday(-1, MONDAY, &date);
gregorian_from_fixed(rd, &date);
printf("d/d/d\t", date.year, date.month, date.day);
}
printf("\n");
/* US Memorial Day; Eq.(2.37) */
printf("US Memorial Day (2):\t");
for (int y = year1; y <= year2; y ) {
date.year = y;
date.month = 6;
date.day = 0;
rd = nth_kday(-1, MONDAY, &date);
gregorian_from_fixed(rd, &date);
printf("d/d/d\t", date.year, date.month, date.day);
}
printf("\n");
}
static void
test3()
{
struct date date = { 2020, 0, 0 };
struct date date2;
int rd, rd2;
printf("\n-----------------------------------------------------------\n");
printf("Input(Y,M,D)\tR.D.\tOutput(Y,M,D)\tR.D.2\tEq?\n");
for (int m = 0; m < 20; m ) {
date.month = m;
rd = fixed_from_gregorian(&date);
gregorian_from_fixed(rd, &date2);
rd2 = fixed_from_gregorian(&date2);
printf("(M, -, -)\t}\t(M, -, -)\t}\t%d\n",
date.year, date.month, date.day, rd,
date2.year, date2.month, date2.day, rd2,
rd==rd2);
}
}
/* Return the seconds east of UTC */
static int
get_utcoffset(void)
{
time_t t;
struct tm tm;
time(&t);
localtime_r(&t, &tm);
return tm.tm_gmtoff;
}
static void
usage(const char *progname)
{
fprintf(stderr, "usage: %s [-L location] [-T] [-U timezone]\n", progname);
exit(2);
}
int
main(int argc, char *argv[])
{
int ch;
int utcoffset = get_utcoffset();
bool run_test = false;
double latitude = 0.0;
double longitude = 0.0;
double elevation = 0.0;
const char *progname = argv[0];
while ((ch = getopt(argc, argv, "hL:TU:")) != -1) {
switch (ch) {
case 'L':
if (!parse_location(optarg, &latitude, &longitude, &elevation))
errx(1, "invalid location: '%s'", optarg);
break;
case 'T':
run_test = true;
break;
case 'U':
if (!parse_timezone(optarg, &utcoffset))
errx(1, "invalid timezone: '%s'", optarg);
break;
case 'h':
case '?':
default:
usage(progname);
}
}
argc -= optind;
argv = optind;
if (argc)
usage(progname);
printf("UTC offset: %d [seconds]\n", utcoffset);
printf("Location: (latitude=%lf°, longitude=%lf°, elevation=%lfm)\n",
latitude, longitude, elevation);
if (run_test) {
test1();
test2();
test3();
}
return 0;
}