1 /* unixtime.c
2 * Written by Daniel Foote.
3 * Started Feb 2005.
4 *
5 * This file contains a function that converts a string
6 * to a unix time, given a format string.
7 */
8
9 /* Copyright 2005 Daniel Foote.
10 *
11 * This file is part of gpscorrelate.
12 *
13 * gpscorrelate is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * gpscorrelate is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with gpscorrelate; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 */
27
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <time.h>
31 #include <string.h>
32
33 #include "unixtime.h"
34
ConvertToUnixTime(char * StringTime,char * Format,int TZOffsetHours,int TZOffsetMinutes)35 time_t ConvertToUnixTime(char* StringTime, char* Format,
36 int TZOffsetHours, int TZOffsetMinutes)
37 {
38 /* Read the time using the specified format.
39 * The format and string being read from must
40 * have the most significant time on the left,
41 * and the least significant on the right:
42 * ie, Year on the left, seconds on the right. */
43
44 /* Sanity check... */
45 if (StringTime == NULL || Format == NULL)
46 {
47 return 0;
48 }
49
50 /* Define and set up our structure. */
51 struct tm Time;
52 Time.tm_wday = 0;
53 Time.tm_yday = 0;
54 Time.tm_isdst = -1;
55
56 /* Read out the time from the string using our format. */
57 sscanf(StringTime, Format, &Time.tm_year, &Time.tm_mon,
58 &Time.tm_mday, &Time.tm_hour,
59 &Time.tm_min, &Time.tm_sec);
60
61 /* Adjust the years for the mktime function to work. */
62 Time.tm_year -= 1900;
63 Time.tm_mon -= 1;
64
65 /* Add our timezone offset to the time.
66 * We don't check to see if it overflowed anything;
67 * mktime does this and fixes it for us. */
68 /* Note also that we SUBTRACT these times. We want the
69 * result to be in UTC. */
70
71 Time.tm_hour -= TZOffsetHours;
72 Time.tm_min -= TZOffsetMinutes;
73
74 /* Calculate and return the unix time. */
75 return mktime(&Time);
76 };
77
78