-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsun.c
493 lines (396 loc) · 9.89 KB
/
sun.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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
/*
Simple SUNRISET front-end application
(c) Joachim Nilsson, 2017
Released to the public domain by Joachim Nilsson, December 2017
*/
#include "config.h"
#include <getopt.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include "tzalias.h"
#include "sunriset.h"
#define TIMEZONE "/etc/timezone"
#define ZONETAB "/usr/share/zoneinfo/zone.tab"
/* From The Practice of Programming, by Kernighan and Pike */
#define NELEMS(array) (sizeof(array) / sizeof(array[0]))
#define PRINTF(fmt, args...) if (verbose > 0) printf(fmt, ##args)
static time_t now;
static struct tm *tm;
static time_t offset = 0;
static int utc = 0;
static int verbose = 1;
static int do_wait = 0;
extern char *__progname;
static time_t timediff(void)
{
static int done = 0;
static time_t diff;
if (done)
return diff;
// tzset();
// diff = -timezone;
diff = tm->tm_gmtoff;
done = 1;
return diff;
}
/*
* Converts the optional `-w TIME` offset to a relative offset.
* Supports e.g.. 30m, -15m, 13s, 1h ... or just 3600
*/
static time_t convert_offset(char *arg)
{
int mult = 1;
long long val = 0;
if (!arg)
return 0;
if (strpbrk(arg, "hH"))
mult = 3600;
if (strpbrk(arg, "mM"))
mult = 60;
sscanf(arg, "%lld", &val);
val *= mult;
/* MAX offset == +/- 6h, otherwise your location is wrong */
if (val < -6 * 3600)
val = -6 * 3600;
if (val > 6 * 3600)
val = 6 * 3600;
return val;
}
static void convert(double ut, int *h, int *m)
{
*h = (int)floor(ut);
*m = (int)(60 * (ut - floor(ut)));
*m += (timediff() % 3600)/60;
*h += timediff() / 3600;
}
static char *lctime_r(double ut, char *buf, size_t len)
{
int h, m;
convert(ut, &h, &m);
snprintf(buf, len, "%02d:%02d", h, m);
return buf;
}
static char *lctime(double ut)
{
static char buf[10];
return lctime_r(ut, buf, sizeof(buf));
}
static int riset(int mode, double lat, double lon, int year, int month, int day)
{
double rise, set;
// char bufr[10], bufs[10];
sun_rise_set(year, month, day, lon, lat, &rise, &set);
if (mode)
PRINTF("Sun rises %s", lctime(rise));
if (!mode)
PRINTF("Sun sets %s", lctime(set));
if (mode == -1)
PRINTF(", sets %s", lctime(set));
PRINTF(" %s\n", tm->tm_zone);
// printf("Sun rises %s, sets %s %s\n", lctime_r(rise, bufr, sizeof(bufr)),
// lctime_r(set, bufs, sizeof(bufs)), tm->tm_zone);
if (do_wait > 0) {
int h, m, s;
time_t then, sec;
if (mode)
convert(rise, &h, &m);
else
convert(set, &h, &m);
/* Adjust for sunset/sunrise regardless of timezone */
h = h - tm->tm_hour;
if (h < 0)
h += 24;
m = m - tm->tm_min;
if (m < 0)
m += 60;
then = now + 3600 * h + 60 * m;
sec = then - now + offset;
/* Pretty printing */
h = sec / 60 / 60;
m = sec / 60 - h * 60;
s = sec - m * 60 - h * 60 * 60;
PRINTF("Sleeping %dh%dm%ds ...\n", h, m, s);
sleep(sec);
}
return 0;
}
static int sunrise(double lat, double lon, int year, int month, int day)
{
return riset(1, lat, lon, year, month, day);
}
static int sunset(double lat, double lon, int year, int month, int day)
{
return riset(0, lat, lon, year, month, day);
}
static int all(double lat, double lon, int year, int month, int day)
{
double daylen, civlen, nautlen, astrlen;
double rise, set, civ_start, civ_end, naut_start, naut_end;
double astr_start, astr_end;
int rs, civ, naut, astr;
char bufr[10], bufs[10];
daylen = day_length(year, month, day, lon, lat);
civlen = day_civil_twilight_length(year, month, day, lon, lat);
nautlen = day_nautical_twilight_length(year, month, day, lon, lat);
astrlen = day_astronomical_twilight_length(year, month, day, lon, lat);
PRINTF("Day length: %5.2f hours\n", daylen);
PRINTF("With civil twilight %5.2f hours\n", civlen);
PRINTF("With nautical twilight %5.2f hours\n", nautlen);
PRINTF("With astronomical twilight %5.2f hours\n", astrlen);
PRINTF("Length of twilight: civil %5.2f hours\n", (civlen - daylen) / 2.0);
PRINTF(" nautical %5.2f hours\n", (nautlen - daylen) / 2.0);
PRINTF(" astronomical %5.2f hours\n", (astrlen - daylen) / 2.0);
rs = sun_rise_set(year, month, day, lon, lat, &rise, &set);
civ = civil_twilight(year, month, day, lon, lat, &civ_start, &civ_end);
naut = nautical_twilight(year, month, day, lon, lat, &naut_start, &naut_end);
astr = astronomical_twilight(year, month, day, lon, lat, &astr_start, &astr_end);
PRINTF("Sun at south %s %s\n", lctime((rise + set) / 2.0), tm->tm_zone);
switch (rs) {
case 0:
printf("Sun rises %s, sets %s %s\n",
lctime_r(rise, bufr, sizeof(bufr)),
lctime_r(set, bufs, sizeof(bufs)), tm->tm_zone);
break;
case +1:
PRINTF("Sun above horizon\n");
break;
case -1:
PRINTF("Sun below horizon\n");
break;
}
switch (civ) {
case 0:
printf("Civil twilight starts %s, ends %s %s\n",
lctime_r(civ_start, bufr, sizeof(bufr)),
lctime_r(civ_end, bufs, sizeof(bufs)), tm->tm_zone);
break;
case +1:
PRINTF("Never darker than civil twilight\n");
break;
case -1:
PRINTF("Never as bright as civil twilight\n");
break;
}
switch (naut) {
case 0:
printf("Nautical twilight starts %s, ends %s %s\n",
lctime_r(naut_start, bufr, sizeof(bufr)),
lctime_r(naut_end, bufs, sizeof(bufs)), tm->tm_zone);
break;
case +1:
PRINTF("Never darker than nautical twilight\n");
break;
case -1:
PRINTF("Never as bright as nautical twilight\n");
break;
}
switch (astr) {
case 0:
printf("Astronomical twilight starts %s, ends %s %s\n",
lctime_r(astr_start, bufr, sizeof(bufr)),
lctime_r(astr_end, bufs, sizeof(bufs)), tm->tm_zone);
break;
case +1:
PRINTF("Never darker than astronomical twilight\n");
break;
case -1:
PRINTF("Never as bright as astronomical twilight\n");
break;
}
return 0;
}
static void chomp(char *str)
{
size_t len;
if (!str)
return;
len = strlen(str) - 1;
while (len && str[len] == '\n')
str[len--] = 0;
}
static int alias(char *tz, size_t len)
{
size_t i;
for (i = 0; i < NELEMS(tzalias); i++) {
if (strcmp(tzalias[i].name, tz))
continue;
strncpy(tz, tzalias[i].alias, len);
return 1;
}
return 0;
}
static int probe(double *lat, double *lon)
{
int found = 0, again = 0;
FILE *fp;
char *ptr, tz[42], buf[80];
ptr = getenv("TZ");
if (!ptr) {
fp = fopen(TIMEZONE, "r");
if (!fp)
return 0;
if (fgets(tz, sizeof(tz), fp))
chomp(tz);
fclose(fp);
} else {
strncpy(tz, ptr, sizeof(tz));
tz[sizeof(tz) - 1] = 0;
}
retry:
// printf("tz: '%s'\n", tz);
fp = fopen(ZONETAB, "r");
if (!fp)
return 0;
while ((fgets(buf, sizeof(buf), fp))) {
ptr = strstr(buf, tz);
if (!ptr)
continue;
*ptr = 0;
ptr = buf;
while (*ptr != ' ' && *ptr != ' ')
ptr++;
if (sscanf(ptr, "%lf%lf", lat, lon) == 2) {
*lat /= 100.0;
*lon /= 100.0;
found = 1;
}
// printf("buf: '%s', lat: %lf, lon: %lf\n", ptr, *lat, *lon);
break;
}
fclose(fp);
if (!found) {
// printf("Cannot find your coordinates using time zone: %s\n", tz);
if (!again) {
again++;
if (alias(tz, sizeof(tz)))
goto retry;
}
// puts("");
}
return found;
}
static int interactive(double *lat, double *lon, int *year, int *month, int *day)
{
char buf[80];
printf("Latitude (+ is north) and longitude (+ is east) : ");
if (fgets(buf, sizeof(buf), stdin))
sscanf(buf, "%lf %lf", lat, lon);
printf("Input date ( yyyy mm dd ) (ctrl-C exits): ");
if (fgets(buf, 80, stdin))
sscanf(buf, "%d %d %d", year, month, day);
return 1;
}
static int usage(int code)
{
printf("Usage:\n"
" %s [-ahirsw] [-o OFFSET] [+/-latitude +/-longitude]\n"
"\n"
"Options:\n"
" -a Show all relevant times and exit\n"
" -l Increased verbosity, enable log messages\n"
" -h This help text\n"
" -i Interactive mode\n"
" -r Sunrise mode\n"
" -s Sunset mode\n"
" -u Use UTC everywhere, not local time\n"
" -v Show program version and exit\n"
" -w Wait until sunset or sunrise\n"
" -o ARG Time offset to adjust wait, e.g. -o -30m\n"
" maximum allowed offset: +/- 6h\n"
"\n"
"Bug report address: %s\n",
__progname, PACKAGE_BUGREPORT);
return code;
}
int main(int argc, char *argv[])
{
int c, op = 0, ok = 0;
int year, month, day;
double lon = 0.0, lat;
while ((c = getopt(argc, argv, "ahilo:rsuvw")) != EOF) {
switch (c) {
case 'h':
return usage(0);
case 'i':
verbose++;
ok = interactive(&lat, &lon, &year, &month, &day);
break;
case 'l':
verbose++;
break;
case 'a':
verbose++;
do_wait--;
/* fallthrough */
case 'r':
case 's':
op = c;
break;
case 'u':
utc = 1;
break;
case 'v':
puts(PACKAGE_VERSION);
return 0;
case 'o':
offset = convert_offset(optarg);
break;
case 'w':
verbose--;
do_wait++;
break;
case ':': /* missing param for option */
case '?': /* unknown option */
default:
return usage(1);
}
}
now = time(NULL);
if (utc)
tm = gmtime(&now);
else
tm = localtime(&now);
if (!ok) {
if (optind < argc)
lat = atof(argv[optind++]);
if (optind < argc)
lon = atof(argv[optind]);
year = 1900 + tm->tm_year;
month = 1 + tm->tm_mon;
day = tm->tm_mday;
// PRINTF("latitude %f longitude %f date %d-%02d-%02d %d:%d (%s)\n",
// lat, lon, year, month, day, tm->tm_hour, tm->tm_min, tm->tm_zone);
if (lon != 0.0)
ok = 1;
if (!ok)
ok = probe(&lat, &lon);
} else {
tm->tm_year = year - 1900;
tm->tm_mon = month - 1;
tm->tm_mday = day;
}
if (!ok)
return usage(1);
switch (op) {
case 'a':
return all(lat, lon, year, month, day);
case 'r':
return sunrise(lat, lon, year, month, day);
case 's':
return sunset(lat, lon, year, month, day);
default:
verbose++;
break;
}
return riset(-1, lat, lon, year, month, day);
}
/**
* Local Variables:
* indent-tabs-mode: t
* c-file-style: "linux"
* End:
*/