-
Notifications
You must be signed in to change notification settings - Fork 1
/
sun.c
53 lines (42 loc) · 1.23 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <time.h>
int main(int argc, char *argv[]) {
enum sun mode;
enum out output;
if (argc < 4 || argc > 6) goto usage;
if (strcmp(argv[1], "rise") == 0) mode = RISE;
else if (strcmp(argv[1], "set") == 0) mode = SET;
else goto usage;
if (argc >= 6) {
if (strcmp(argv[5], "human") == 0) output = HUMAN;
else if (strcmp(argv[5], "unix") == 0) output = UNIX;
else goto usage;
}
else {
output = HUMAN;
}
int timezone = (argc >= 5) ? atoi(argv[4]) : 0;
double lat = deg2rad(strtod(argv[2], NULL));
double lon = deg2rad(strtod(argv[3], NULL));
double time = sun(mode, lat, lon, timezone);
double intpart;
switch (output) {
case HUMAN:
printf("%d:%02d\n", (int) floor(time), (int) (modf(time, &intpart)*60));
break;
case UNIX:
printf("%d\n", (int) round(time*60*60));
break;
}
return 0;
usage:
fprintf(stderr, "usage: sun MODE LAT LON [TIMEZONE] [OUTPUT]\n");
fprintf(stderr, " MODE\t\t'rise' or 'set'\n");
fprintf(stderr, " LAT, LON\tthe geografical postition in degree\n");
fprintf(stderr, " TIMEZONE\tan integer describing the timezone offset\n");
fprintf(stderr, " OUTPUT\t'human' (default) or 'unix'\n");
return -1;
}