Blame view

src/TimeTableCatalog/TimeTableCatalogUtil.cc 6.75 KB
fbe3c2bb   Benjamin Renard   First commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/*
 * TimeTableCatalogUtil.cc
 *
 *  Created on: 6 août 2013
 *      Author: CS
 */

#include <boost/algorithm/string.hpp>
#include <boost/algorithm/string/predicate.hpp>
#include <boost/lexical_cast.hpp>

#include "TimeTableCatalogUtil.hh"
#include "TimeUtil.hh"
#include "DD_time.hh"
#include <sstream>
#include <iomanip>
818d2ea6   Benjamin Renard   Support new time ...
17
#include <time.h>
fbe3c2bb   Benjamin Renard   First commit
18
19
20
21

namespace TimeTableCatalog {

double readISOTime(const std::string& ptime) {
818d2ea6   Benjamin Renard   Support new time ...
22
23
	int size = 0;
	return readISOTime(ptime, getTimeFormat(ptime, size));
fbe3c2bb   Benjamin Renard   First commit
24
25
}

818d2ea6   Benjamin Renard   Support new time ...
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
TimeTable::TIME_FORMAT getTimeFormat(const std::string& ptime, int& size) {
	struct tm tm;
	char* pos = NULL;
	memset(&tm, 0, sizeof(struct tm));

	std::string time = ptime;
	boost::algorithm::trim(time);
	std::replace( time.begin(), time.end(), '-', ' ');
	std::replace( time.begin(), time.end(), '/', ' ');
	std::replace( time.begin(), time.end(), ':', ' ');
	std::replace( time.begin(), time.end(), 'T', ' ');
	std::replace( time.begin(), time.end(), 'Z', ' ');
	std::replace( time.begin(), time.end(), '.', ' ');

	std::vector<std::string> parts;
	split(time, ' ', parts);

	int index = 0;
	size = 0;

	bool isDOY = (pos != NULL);
	bool haveSec = false;
	bool haveMls = false;

	// Year
	if ((int)parts.size() <= index) {
		return TimeTable::TIME_FORMAT::UNKNOWN;
fbe3c2bb   Benjamin Renard   First commit
53
	}
818d2ea6   Benjamin Renard   Support new time ...
54
55
56
	boost::algorithm::trim(parts[index]);
	if ((int)parts[index].size() != 4) {
		return TimeTable::TIME_FORMAT::UNKNOWN;
68c29629   Benjamin Renard   Fix in TT/Catalog...
57
	}
818d2ea6   Benjamin Renard   Support new time ...
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
	++index;

	// Month or Day of year
	if ((int)parts.size() <= index) {
		return TimeTable::TIME_FORMAT::UNKNOWN;
	}
	boost::algorithm::trim(parts[index]);
	if ((int)parts[index].size() == 3) {
		isDOY = true;
	}
	else if ((int)parts[index].size() != 2) {
		return TimeTable::TIME_FORMAT::UNKNOWN;
	}
	++index;

	if (!isDOY) {
		//Day of month
		if ((int)parts.size() <= index) {
			return TimeTable::TIME_FORMAT::UNKNOWN;
		}
		boost::algorithm::trim(parts[index]);
		if ((int)parts[index].size() != 2) {
			return TimeTable::TIME_FORMAT::UNKNOWN;
		}
		++index;
	}

	// Hour
	if ((int)parts.size() <= index) {
		return TimeTable::TIME_FORMAT::UNKNOWN;
	}
	boost::algorithm::trim(parts[index]);
	if ((int)parts[index].size() != 2) {
		return TimeTable::TIME_FORMAT::UNKNOWN;
	}
	++index;

	// Min
	if ((int)parts.size() <= index) {
		return TimeTable::TIME_FORMAT::UNKNOWN;
	}
	boost::algorithm::trim(parts[index]);
	if ((int)parts[index].size() != 2) {
		return TimeTable::TIME_FORMAT::UNKNOWN;
	}
	++index;

	// Sec
	if ((int)parts.size() > index) {
		boost::algorithm::trim(parts[index]);
		if ((int)parts[index].size() == 2) {
			haveSec = true;
		}
		++index;
68c29629   Benjamin Renard   Fix in TT/Catalog...
112
	}
818d2ea6   Benjamin Renard   Support new time ...
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131

	// Mls
	if (haveSec && ((int)parts.size() > index)) {
		boost::algorithm::trim(parts[index]);
		if ((int)parts[index].size() == 3) {
			haveMls = true;
		}
		++index;
	}

	if (haveMls) {
		if (isDOY) {
			size = 21;
			return TimeTable::TIME_FORMAT::YYYYDOYThhmmssmsk;
		}
		else {
			size = 23;
			return TimeTable::TIME_FORMAT::YYYYMMDDThhmmssmsk;
		}
68c29629   Benjamin Renard   Fix in TT/Catalog...
132
	}
818d2ea6   Benjamin Renard   Support new time ...
133
134
135
136
137
138
139
140
141
	else if (haveSec) {
		if (isDOY) {
			size = 17;
			return TimeTable::TIME_FORMAT::YYYYDOYThhmmss;
		}
		else {
			size = 19;
			return TimeTable::TIME_FORMAT::YYYYMMDDThhmmss;
		}
68c29629   Benjamin Renard   Fix in TT/Catalog...
142
	}
818d2ea6   Benjamin Renard   Support new time ...
143
144
145
146
147
148
149
150
151
	else {
		if (isDOY) {
			size = 14;
			return TimeTable::TIME_FORMAT::YYYYDOYThhmm;
		}
		else {
			size = 16;
			return TimeTable::TIME_FORMAT::YYYYMMDDThhmm;
		}
68c29629   Benjamin Renard   Fix in TT/Catalog...
152
153
154
	}

	return TimeTable::TIME_FORMAT::UNKNOWN;
fbe3c2bb   Benjamin Renard   First commit
155
156
157
158
}

double readISOTime(const std::string& ptime,
		const TimeTable::TIME_FORMAT pformat) {
68c29629   Benjamin Renard   Fix in TT/Catalog...
159
	if (pformat == TimeTable::TIME_FORMAT::UNKNOWN) {
fbe3c2bb   Benjamin Renard   First commit
160
161
162
163
164
		return -1;
	}

	dd_tmstr_t *UT;
	unsigned tabTime[7];
68c29629   Benjamin Renard   Fix in TT/Catalog...
165

818d2ea6   Benjamin Renard   Support new time ...
166
167
168
169
170
171
172
173
174
175
176
	int y = 0, m = 0, d = 0, doy = 0, h = 0, mi = 0, s = 0, ms = 0;
	bool isDOY = false;

	std::string time = ptime;
	std::replace( time.begin(), time.end(), '-', ' ');
	std::replace( time.begin(), time.end(), '/', ' ');
	std::replace( time.begin(), time.end(), ':', ' ');
	std::replace( time.begin(), time.end(), 'T', ' ');
	std::replace( time.begin(), time.end(), 'Z', ' ');
	std::replace( time.begin(), time.end(), '.', ' ');

68c29629   Benjamin Renard   Fix in TT/Catalog...
177
178
	switch (pformat) {
		case TimeTable::TIME_FORMAT::YYYYMMDDThhmmssmsk :
818d2ea6   Benjamin Renard   Support new time ...
179
			sscanf(time.c_str(), "%04d %02d %02d %02d %02d %02d %03d", &y, &m, &d, &h, &mi, &s, &ms);
68c29629   Benjamin Renard   Fix in TT/Catalog...
180
			break;
818d2ea6   Benjamin Renard   Support new time ...
181
182
183
		case TimeTable::TIME_FORMAT::YYYYDOYThhmmssmsk :
			isDOY = true;
			sscanf(time.c_str(), "%04d %03d %02d %02d %02d %03d", &y, &doy, &h, &mi, &s, &ms);
68c29629   Benjamin Renard   Fix in TT/Catalog...
184
			break;
818d2ea6   Benjamin Renard   Support new time ...
185
186
		case TimeTable::TIME_FORMAT::YYYYMMDDThhmmss :
			sscanf(time.c_str(), "%04d %02d %02d %02d %02d %02d", &y, &m, &d, &h, &mi, &s);
68c29629   Benjamin Renard   Fix in TT/Catalog...
187
			break;
818d2ea6   Benjamin Renard   Support new time ...
188
189
190
		case TimeTable::TIME_FORMAT::YYYYDOYThhmmss :
			isDOY = true;
			sscanf(time.c_str(), "%04d %03d %02d %02d %02d", &y, &doy, &h, &mi, &s);
68c29629   Benjamin Renard   Fix in TT/Catalog...
191
			break;
818d2ea6   Benjamin Renard   Support new time ...
192
193
		case TimeTable::TIME_FORMAT::YYYYMMDDThhmm :
			sscanf(time.c_str(), "%04d %02d %02d %02d %02d", &y, &m, &d, &h, &mi);
68c29629   Benjamin Renard   Fix in TT/Catalog...
194
			break;
818d2ea6   Benjamin Renard   Support new time ...
195
196
197
		case TimeTable::TIME_FORMAT::YYYYDOYThhmm :
			isDOY = true;
			sscanf(time.c_str(), "%04d %03d %02d %02d", &y, &doy, &h, &mi);
68c29629   Benjamin Renard   Fix in TT/Catalog...
198
199
200
			break;
		default:
			return 0.;
fbe3c2bb   Benjamin Renard   First commit
201
202
	}

818d2ea6   Benjamin Renard   Support new time ...
203
204
205
206
207
208
209
210
211
212
213
214
215
	if (isDOY) {
		dd_tmstr_t ut;
		memset(&ut, 0, sizeof(dd_tmstr_t));
		ut.msec = ms;
		ut.sec  = s;
		ut.min  = mi;
		ut.hour = h;
		ut.day  = doy-1;
		ut.year = y;
		SetDouble(&ut);
		return ut.times;
	}

68c29629   Benjamin Renard   Fix in TT/Catalog...
216
217
218
219
220
221
222
223
	tabTime[0] = y;
	tabTime[1] = m;
	tabTime[2] = d;
	tabTime[3] = h;
	tabTime[4] = mi;
	tabTime[5] = s;
	tabTime[6] = ms;

fbe3c2bb   Benjamin Renard   First commit
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
	UT = UT2double(tabTime);

	return UT->times;
}

void writeISOTime(const double pTime, const TimeTable::TIME_FORMAT pFormat,
		std::ostream& pOut) {
	if (pTime == -1) {
		return;
	}

	std::ostringstream os;
	AMDA::TimeUtil::formatTimeDateInIso(pTime, os);
	if (TimeTable::TIME_FORMAT::YYYYMMDDThhmmss == pFormat) {
		// remove milliseconds
		pOut << os.str().substr(0, os.str().length() - 4);
	} else { // UNKNOWN or with millisecond
		pOut << os.str();
	}
}

// ----------------- EXTERN --------------------------

bool contains(const std::string & pline, const std::string & pkeyword,
		const std::string& pprefix) {
	std::string simpleline(pline);
	// remove all whitespace
	boost::algorithm::replace_all(simpleline, " ", "");
	// to lower case
	std::transform(simpleline.begin(), simpleline.end(), simpleline.begin(),
			::tolower);

	std::string simpleKeyword(pprefix + pkeyword);
	// remove all whitespace
	boost::algorithm::replace_all(simpleKeyword, " ", "");
	// to lower case
	std::transform(simpleKeyword.begin(), simpleKeyword.end(),
			simpleKeyword.begin(), ::tolower);

	return std::string::npos != simpleline.find(simpleKeyword);
}

void extractvalue(const std::string & pline, std::string & pvalue) {
	// get value after ":"
	pvalue = pline.substr(pline.find(":") + 1);
	// remove trailing with spaces
	boost::algorithm::trim(pvalue);
	// remove ; character at the end of the line
	boost::algorithm::replace_last(pvalue, ";", "");
}

std::vector<std::string> &split(const std::string &s, char delim,
		std::vector<std::string> &elems) {
	std::stringstream ss(s);
	std::string item;
	while (std::getline(ss, item, delim)) {
		elems.push_back(item);
	}
	return elems;
}

} /* namespace TimeTableCatalog */