forked from RTMilliken/EDFbrowser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
date_time_stamp_parser.c
147 lines (100 loc) · 3.73 KB
/
date_time_stamp_parser.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
/*
***************************************************************************
*
* Author: Teunis van Beelen
*
* Copyright (C) 2014, 2015 Teunis van Beelen
*
*
***************************************************************************
*
* This program 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 version 2 of the License.
*
* This program 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 this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
***************************************************************************
*
* This version of GPL is at http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt
*
***************************************************************************
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "utc_date_time.h"
#include "date_time_stamp_parser.h"
#define DATE_TIME_STAMP_LEN 20
long long parse_date_time_stamp(const char *src)
{
int i, len, probes;
char str[1024],
*ptr;
len = strlen(src);
if(len < DATE_TIME_STAMP_LEN + 4) goto ERROR;
strcpy(str, src);
len = strlen(str);
if(len < DATE_TIME_STAMP_LEN) goto ERROR;
probes = len - DATE_TIME_STAMP_LEN + 1;
for(i=0; i<probes; i++)
{
ptr = str + i;
if((ptr[2] != '-') || (ptr[6] != '-') || (ptr[11] != '_') || (ptr[14] != 'h') || (ptr[17] != 'm')) continue;
if((ptr[0] < '0') || (ptr[0] > '9') || (ptr[1] < '0') || (ptr[1] > '9') ||
(ptr[7] < '0') || (ptr[7] > '9') || (ptr[8] < '0') || (ptr[8] > '9') ||
(ptr[9] < '0') || (ptr[9] > '9') || (ptr[10] < '0') || (ptr[10] > '9')) continue;
if((ptr[12] < '0') || (ptr[12] > '9') || (ptr[13] < '0') || (ptr[13] > '9') ||
(ptr[15] < '0') || (ptr[15] > '9') || (ptr[16] < '0') || (ptr[16] > '9') ||
(ptr[18] < '0') || (ptr[18] > '9') || (ptr[19] < '0') || (ptr[19] > '9')) continue;
break;
}
if(i == probes) goto ERROR;
/*
* We are looking for a string formatted like this:
* 03-MAY-2013_14h45m49
*
*/
ptr[DATE_TIME_STAMP_LEN] = 0;
ptr[2] = 0;
ptr[6] = 0;
ptr[11] = 0;
ptr[14] = 0;
ptr[17] = 0;
struct date_time_struct dat_time;
dat_time.day = atoi(ptr);
dat_time.year = atoi(ptr + 7);
dat_time.hour = atoi(ptr + 12);
dat_time.minute = atoi(ptr + 15);
dat_time.second = atoi(ptr + 18);
if((dat_time.day < 1) || (dat_time.day > 31) || (dat_time.year < 1970) || (dat_time.year > 2999) ||
(dat_time.hour < 0) || (dat_time.hour > 23) || (dat_time.minute < 0) || (dat_time.minute > 59) ||
(dat_time.second < 0) || (dat_time.second > 59)) goto ERROR;
dat_time.month = -1;
if(!strcmp(ptr + 3, "JAN")) dat_time.month = 1;
if(!strcmp(ptr + 3, "FEB")) dat_time.month = 2;
if(!strcmp(ptr + 3, "MAR")) dat_time.month = 3;
if(!strcmp(ptr + 3, "APR")) dat_time.month = 4;
if(!strcmp(ptr + 3, "MAY")) dat_time.month = 5;
if(!strcmp(ptr + 3, "JUN")) dat_time.month = 6;
if(!strcmp(ptr + 3, "JUL")) dat_time.month = 7;
if(!strcmp(ptr + 3, "AUG")) dat_time.month = 8;
if(!strcmp(ptr + 3, "SEP")) dat_time.month = 9;
if(!strcmp(ptr + 3, "OCT")) dat_time.month = 10;
if(!strcmp(ptr + 3, "NOV")) dat_time.month = 11;
if(!strcmp(ptr + 3, "DEC")) dat_time.month = 12;
if(dat_time.month == -1) goto ERROR;
long long utc_time;
date_time_to_utc(&utc_time, dat_time);
return utc_time;
ERROR:
return -1LL;
}