-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.c
299 lines (258 loc) · 8.73 KB
/
utils.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
#ifdef WIN32
#include <windows.h>
void usleep(__int64 usec)
{
HANDLE timer;
LARGE_INTEGER ft;
ft.QuadPart = -(10*usec); // Convert to 100 nanosecond interval, negative value indicates relative time
timer = CreateWaitableTimer(NULL, TRUE, NULL);
SetWaitableTimer(timer, &ft, 0, NULL, NULL, 0);
WaitForSingleObject(timer, INFINITE);
CloseHandle(timer);
}
#ifdef _MSC_VER
int gettimeofday(struct timeval* tp, struct timezone* tzp)
{
// Note: some broken versions only have 8 trailing zero's, the correct epoch has 9 trailing zero's
// This magic number is the number of 100 nanosecond intervals since January 1, 1601 (UTC)
// until 00:00:00 January 1, 1970
static const ULONGLONG EPOCH = ((ULONGLONG)116444736000000000ULL);
SYSTEMTIME system_time;
FILETIME file_time;
ULONGLONG t;
GetSystemTime(&system_time);
SystemTimeToFileTime(&system_time, &file_time);
t = (ULONGLONG)file_time.dwHighDateTime;
t <<= 32;
t += (ULONGLONG)(file_time.dwLowDateTime);
tp->tv_sec = (long)((t - EPOCH) / 10000000L);
tp->tv_usec = (long)(system_time.wMilliseconds * 1000);
return 0;
}
#include <time.h>
#define _SEC_IN_MINUTE 60L
#define _SEC_IN_HOUR 3600L
#define _SEC_IN_DAY 86400L
static const int DAYS_IN_MONTH[12] =
{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
#define _DAYS_IN_MONTH(x) ((x == 1) ? days_in_feb : DAYS_IN_MONTH[x])
static const int _DAYS_BEFORE_MONTH[12] =
{0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
#define _ISLEAP(y) (((y) % 4) == 0 && (((y) % 100) != 0 || (((y)+1900) % 400) == 0))
#define _DAYS_IN_YEAR(year) (_ISLEAP(year) ? 366 : 365)
static void validate_structure(struct tm *tim_p)
{
div_t res;
int days_in_feb = 28;
/* calculate time & date to account for out of range values */
if (tim_p->tm_sec < 0 || tim_p->tm_sec > 59)
{
res = div (tim_p->tm_sec, 60);
tim_p->tm_min += res.quot;
if ((tim_p->tm_sec = res.rem) < 0)
{
tim_p->tm_sec += 60;
--tim_p->tm_min;
}
}
if (tim_p->tm_min < 0 || tim_p->tm_min > 59)
{
res = div (tim_p->tm_min, 60);
tim_p->tm_hour += res.quot;
if ((tim_p->tm_min = res.rem) < 0)
{
tim_p->tm_min += 60;
--tim_p->tm_hour;
}
}
if (tim_p->tm_hour < 0 || tim_p->tm_hour > 23)
{
res = div (tim_p->tm_hour, 24);
tim_p->tm_mday += res.quot;
if ((tim_p->tm_hour = res.rem) < 0)
{
tim_p->tm_hour += 24;
--tim_p->tm_mday;
}
}
if (tim_p->tm_mon > 11)
{
res = div (tim_p->tm_mon, 12);
tim_p->tm_year += res.quot;
if ((tim_p->tm_mon = res.rem) < 0)
{
tim_p->tm_mon += 12;
--tim_p->tm_year;
}
}
if (_DAYS_IN_YEAR (tim_p->tm_year) == 366)
days_in_feb = 29;
if (tim_p->tm_mday <= 0)
{
while (tim_p->tm_mday <= 0)
{
if (--tim_p->tm_mon == -1)
{
tim_p->tm_year--;
tim_p->tm_mon = 11;
days_in_feb =
((_DAYS_IN_YEAR (tim_p->tm_year) == 366) ?
29 : 28);
}
tim_p->tm_mday += _DAYS_IN_MONTH (tim_p->tm_mon);
}
}
else
{
while (tim_p->tm_mday > _DAYS_IN_MONTH (tim_p->tm_mon))
{
tim_p->tm_mday -= _DAYS_IN_MONTH (tim_p->tm_mon);
if (++tim_p->tm_mon == 12)
{
tim_p->tm_year++;
tim_p->tm_mon = 0;
days_in_feb =
((_DAYS_IN_YEAR (tim_p->tm_year) == 366) ?
29 : 28);
}
}
}
}
time_t mkgmtime(struct tm *tim_p)
{
time_t tim = 0;
long days = 0;
int year, isdst, tm_isdst;
/* validate structure */
validate_structure (tim_p);
/* compute hours, minutes, seconds */
tim += tim_p->tm_sec + (tim_p->tm_min * _SEC_IN_MINUTE) +
(tim_p->tm_hour * _SEC_IN_HOUR);
/* compute days in year */
days += tim_p->tm_mday - 1;
days += _DAYS_BEFORE_MONTH[tim_p->tm_mon];
if (tim_p->tm_mon > 1 && _DAYS_IN_YEAR (tim_p->tm_year) == 366)
days++;
/* compute day of the year */
tim_p->tm_yday = days;
if (tim_p->tm_year > 10000
|| tim_p->tm_year < -10000)
{
return (time_t) -1;
}
/* compute days in other years */
if (tim_p->tm_year > 70)
{
for (year = 70; year < tim_p->tm_year; year++)
days += _DAYS_IN_YEAR (year);
}
else if (tim_p->tm_year < 70)
{
for (year = 69; year > tim_p->tm_year; year--)
days -= _DAYS_IN_YEAR (year);
days -= _DAYS_IN_YEAR (year);
}
/* compute day of the week */
if ((tim_p->tm_wday = (days + 4) % 7) < 0)
tim_p->tm_wday += 7;
/* compute total seconds */
tim += (days * _SEC_IN_DAY);
/* Convert user positive into 1 */
tm_isdst = tim_p->tm_isdst > 0 ? 1 : tim_p->tm_isdst;
isdst = tm_isdst;
//screw this!
// if (_daylight)
// {
// int y = tim_p->tm_year + YEAR_BASE;
// if (y == tz->__tzyear || __tzcalc_limits (y))
//{
// /* calculate start of dst in dst local time and
// start of std in both std local time and dst local time */
// time_t startdst_dst = tz->__tzrule[0].change
// - (time_t) tz->__tzrule[1].offset;
// time_t startstd_dst = tz->__tzrule[1].change
// - (time_t) tz->__tzrule[1].offset;
// time_t startstd_std = tz->__tzrule[1].change
// - (time_t) tz->__tzrule[0].offset;
// /* if the time is in the overlap between dst and std local times */
// if (tim >= startstd_std && tim < startstd_dst)
// ; /* we let user decide or leave as -1 */
// else
// {
// isdst = (tz->__tznorth
// ? (tim >= startdst_dst && tim < startstd_std)
// : (tim >= startdst_dst || tim < startstd_std));
// /* if user committed and was wrong, perform correction, but not
// * if the user has given a negative value (which
// * asks mktime() to determine if DST is in effect or not) */
// if (tm_isdst >= 0 && (isdst ^ tm_isdst) == 1)
// {
// /* we either subtract or add the difference between
// time zone offsets, depending on which way the user got it
// wrong. The diff is typically one hour, or 3600 seconds,
// and should fit in a 16-bit int, even though offset
// is a long to accomodate 12 hours. */
// int diff = (int) (tz->__tzrule[0].offset
// - tz->__tzrule[1].offset);
// if (!isdst)
// diff = -diff;
// tim_p->tm_sec += diff;
// validate_structure (tim_p);
// tim += diff; /* we also need to correct our current time calculation */
// }
// }
//}
// }
//screw this also
/* add appropriate offset to put time in gmt format */
//if (isdst == 1)
// tim += (time_t) tz->__tzrule[1].offset;
//else /* otherwise assume std time */
// tim += (time_t) tz->__tzrule[0].offset;
//and screw this too
/* reset isdst flag to what we have calculated */
tim_p->tm_isdst = isdst;
return tim;
}
void timeradd(const struct timeval* tvp, const struct timeval* uvp, struct timeval* vvp)
{
(vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec;
(vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec;
while ((vvp)->tv_usec >= 1000000) {
(vvp)->tv_sec++;
(vvp)->tv_usec -= 1000000; \
}
}
void timersub(const struct timeval* tvp, const struct timeval* uvp, struct timeval* vvp)
{
(vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec;
(vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec;
while ((vvp)->tv_usec < 0) {
(vvp)->tv_sec--;
(vvp)->tv_usec += 1000000;
}
}
#endif // _MSC_VER
#endif
#include <math.h>
#include <string.h>
#include "fitsec_types.h"
static long _strtodegree(const char * s, char ** e)
{
long l = strtol(s, e, 10);
if (**e == '.') {
// decimal representation
double d = strtod(s, e);
l = (long)floor(d * 10000000.0);
}
return l;
}
int FS3DPositionFromString(FS3DLocation * pos, const char * str){
char * e;
pos->latitude = _strtodegree(str, &e);
if (e == str || ! strchr(":,; /", *e)) return 0;
str = e + 1;
pos->longitude = _strtodegree(str, &e);
if (e == str || *e != 0) return 0;
return 1;
}