forked from freefoote/gpscorrelate
-
Notifications
You must be signed in to change notification settings - Fork 8
/
unixtime.c
126 lines (107 loc) · 3.45 KB
/
unixtime.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
/* unixtime.c
* Originally written by Daniel Foote.
*
* This file contains functions that converts a string
* to a unix time, given a format string and compare times.
*/
/* Copyright 2005-2024 Daniel Foote, Dan Fandrich.
*
* This file is part of gpscorrelate.
*
* gpscorrelate is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* gpscorrelate is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with gpscorrelate; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <string.h>
#include "unixtime.h"
#define MIN(a,b) (((a)<(b))?(a):(b))
#ifdef _WIN32
/* Unfortunately, portable_timegm below isn't portable to Windows */
#define portable_timegm _mkgmtime
#else
/* Some systems have a version of this called timegm(), but it's not portable */
static time_t portable_timegm(struct tm *tm)
{
static const char *tz;
if (!tz) {
tz = getenv("TZ");
if (tz)
/* Copy the string, since it's only guaranteed to be
* valid until the next getenv or setenv */
tz = strdup(tz);
}
/* Set an empty TZ to force UTC */
setenv("TZ", "", 1);
tzset();
time_t ret = mktime(tm);
/* Restore the original TZ */
if (tz)
setenv("TZ", tz, 1);
else
unsetenv("TZ");
tzset();
return ret;
}
#endif
struct timespec ConvertToUnixTime(const char* StringTime, const char* Format,
int TZOffsetHours, int TZOffsetMinutes)
{
/* Read the time using the specified format.
* The format and string being read from must
* have the most significant time on the left,
* and the least significant on the right:
* ie, Year on the left, seconds on the right. */
/* Sanity check... */
if (StringTime == NULL || Format == NULL)
{
return (struct timespec){0, 0};
}
/* Define and set up our structure. */
struct tm Time;
Time.tm_wday = 0;
Time.tm_yday = 0;
Time.tm_isdst = 0; // there is no DST in UTC
char Subsecond[10] = "";
/* Read out the time from the string using our format. */
sscanf(StringTime, Format, &Time.tm_year, &Time.tm_mon,
&Time.tm_mday, &Time.tm_hour,
&Time.tm_min, &Time.tm_sec, Subsecond);
/* Adjust the years for the mktime function to work. */
Time.tm_year -= 1900;
Time.tm_mon -= 1;
/* Calculate and return the Unix time. */
time_t thetime = portable_timegm(&Time);
/* Add our timezone offset to the time.
* Note also that we SUBTRACT these times. We want the
* result to be in UTC. */
thetime -= TZOffsetHours * 60 * 60;
thetime -= TZOffsetMinutes * 60;
/* Convert subseconds to nanoseconds */
char Subsecbuf[] = "000000000";
memcpy(Subsecbuf, Subsecond, MIN(sizeof(Subsecbuf)-1, strlen(Subsecond)));
long Nanoseconds = atol(Subsecbuf);
return (struct timespec){thetime, Nanoseconds};
}
/* Compare two times and return -1, 0 or 1 if a is <, == or > b, respectively */
int CompareTimes(struct timespec a, time_t b)
{
if (a.tv_sec > b)
return 1;
if (a.tv_sec < b)
return -1;
// Seconds are equal; any nanoseconds in a means it's greater
return !!a.tv_nsec;
}