-
Notifications
You must be signed in to change notification settings - Fork 2
/
time.c
154 lines (141 loc) · 4 KB
/
time.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
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <time.h>
#include "d1cons.h"
#include "d1typ.h"
#include "d1glob.h"
#include "d1proto.h"
double gst(double ttime)
{
double secs, pdum;
int i;
// secs = (1999 - 1970) * 31536000.0 + 17.0 * 3600.0 + 16.0 * 60.0 + 20.1948;
secs = (2011 - 1970) * 31536000.0 + 17.0 * 3600.0 + 15.0 * 60.0 + 58.0778;
for (i = 1970; i < 2011; i++) {
if ((i % 4 == 0 && i % 100 != 0) || i % 400 == 0)
secs += 86400.0;
}
return (modf((ttime - secs) / 86164.09053, &pdum) * TWOPI);
/* 17 16 20.1948 UT at 0hr newyear1999 */
/* 17 15 58.0778 UT at 0hr newyear2011 */
}
/* Convert to Seconds since New Year 1970 */
double tosecs(int yr, int day, int hr, int min, int sec)
{
int i;
double secs;
secs = (yr - 1970) * 31536000.0 + (day - 1) * 86400.0 + hr * 3600.0 + min * 60.0 + sec;
for (i = 1970; i < yr; i++) {
if ((i % 4 == 0 && i % 100 != 0) || i % 400 == 0)
secs += 86400.0;
}
if (secs < 0.0)
secs = 0.0;
return secs;
}
/* Convert Seconds to Yr/Day/Hr/Min/Sec */
void toyrday(double secs, int *pyear, int *pday, int *phr, int *pmin, int *psec)
{
double days, day, sec;
int i;
day = floor(secs / 86400.0);
sec = secs - day * 86400.0;
for (i = 1970; day > 365; i++) {
days = ((i % 4 == 0 && i % 100 != 0) || i % 400 == 0) ? 366.0 : 365.0;
day -= days;
}
*phr = sec / 3600.0;
sec -= *phr * 3600.0;
*pmin = sec / 60.0;
*psec = sec - *pmin * 60;
*pyear = i;
day = day + 1;
*pday = day;
if (day == 366) // fix for problem with day 366
{
days = ((i % 4 == 0 && i % 100 != 0) || i % 400 == 0) ? 366 : 365;
if (days == 365) {
day -= 365;
*pday = day;
*pyear = i + 1;
}
}
}
int dayofyear(int year, int month, int day)
{
int i, leap;
int daytab[2][13] = {
{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
{0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
};
leap = (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
for (i = 1; i < month; i++)
day += daytab[leap][i];
return (day);
}
char *to_date(int year, int day)
{
int day_tab[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31,
0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
};
char *mon[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul",
"Aug", "Sep", "Oct", "Nov", "Dec"
};
int k, leap;
static char str[80];
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
leap = 1;
else
leap = 0;
for (k = 1; day > day_tab[leap * 13 + k]; k++)
day -= day_tab[leap * 13 + k];
sprintf(str, "UT %s %02d", mon[k - 1], day);
return str;
}
char *to_radecp(double ra, double dec)
{
int hr, min, sec, deg, dmin, dsec, sg;
static char str[80];
sec = (int) (ra * 12.0 * 3600.0 / PI);
hr = sec / 3600;
sec -= hr * 3600;
min = sec / 60;
sec = sec - min * 60;
sg = 1;
dsec = (int) (dec * 3600.0 * 180.0 / PI);
if (dsec < 0) {
sg = -1;
dsec = -dsec;
}
deg = dsec / 3600;
dsec -= deg * 3600;
dmin = dsec / 60;
dsec -= dmin * 60;
sprintf(str, "%02d:%02d:%02d %02d:%02d:%02d", hr, min, sec, deg * sg, dmin, dsec);
return str;
}
double readclock(void)
{
time_t now;
double secs;
struct tm *t;
now = time(NULL);
t = gmtime(&now);
// gmtime Jan 1 is day 0
secs = tosecs(t->tm_year + 1900, t->tm_yday + 1, t->tm_hour, t->tm_min, t->tm_sec);
if (d1.azelsim) {
if (d1.start_time == 0.0)
d1.start_time = secs;
if (d1.start_sec)
secs = d1.start_sec + (secs - d1.start_time) * d1.speed_up;
else {
if (d1.speed_up > 0)
secs = d1.start_time + (secs - d1.start_time) * d1.speed_up;
else
secs += -d1.speed_up * 3600.0; // advance by hours
}
}
sprintf(d1.timsource, "PC ");
return (secs);
}