-
Notifications
You must be signed in to change notification settings - Fork 18
/
unixtime.c
103 lines (87 loc) · 2.71 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
/* unixtime.c
* Written by Daniel Foote.
* Started Feb 2005.
*
* This file contains a function that converts a string
* to a unix time, given a format string.
*/
/* Copyright 2005 Daniel Foote.
*
* 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"
/* 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;
}
time_t 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 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
/* 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);
/* 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;
return thetime;
}