TimeTableCatalogUtil.cc
4.43 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
* 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>
namespace TimeTableCatalog {
double readISOTime(const std::string& ptime) {
return readISOTime(ptime, getTimeFormat(ptime));
}
TimeTable::TIME_FORMAT getTimeFormat(const std::string& ptime) {
int y = 0, m = 0, d = 0, h = 0, mi = 0, s = 0, ms = 0;
if (sscanf(ptime.c_str(), "%04d-%02d-%02dT%02d:%02d:%02d.%03d", &y, &m, &d, &h, &mi, &s, &ms) == 7) {
return TimeTable::TIME_FORMAT::YYYYMMDDThhmmssmsk;
}
else if (sscanf(ptime.c_str(), "%04d-%02d-%02dT%02d:%02d:%02d", &y, &m, &d, &h, &mi, &s) == 6) {
return TimeTable::TIME_FORMAT::YYYYMMDDThhmmss;
}
else if (sscanf(ptime.c_str(), "%04d-%02d-%02dT%02d:%02d", &y, &m, &d, &h, &mi) == 5) {
return TimeTable::TIME_FORMAT::YYYYMMDDThhmm;
}
else if (sscanf(ptime.c_str(), "%04d/%02d/%02d %02d:%02d:%02d.%03d", &y, &m, &d, &h, &mi, &s, &ms) == 7) {
return TimeTable::TIME_FORMAT::YYYYMMDDThhmmssmsk2;
}
else if (sscanf(ptime.c_str(), "%04d/%02d/%02d %02d:%02d:%02d", &y, &m, &d, &h, &mi, &s) == 6) {
return TimeTable::TIME_FORMAT::YYYYMMDDThhmmss2;
}
else if (sscanf(ptime.c_str(), "%04d/%02d/%02d %02d:%02d", &y, &m, &d, &h, &mi) == 5) {
return TimeTable::TIME_FORMAT::YYYYMMDDThhmm2;
}
return TimeTable::TIME_FORMAT::UNKNOWN;
}
double readISOTime(const std::string& ptime,
const TimeTable::TIME_FORMAT pformat) {
if (pformat == TimeTable::TIME_FORMAT::UNKNOWN) {
return -1;
}
dd_tmstr_t *UT;
unsigned tabTime[7];
int y = 0, m = 0, d = 0, h = 0, mi = 0, s = 0, ms = 0;
switch (pformat) {
case TimeTable::TIME_FORMAT::YYYYMMDDThhmmssmsk :
sscanf(ptime.c_str(), "%04d-%02d-%02dT%02d:%02d:%02d.%03d", &y, &m, &d, &h, &mi, &s, &ms);
break;
case TimeTable::TIME_FORMAT::YYYYMMDDThhmmss :
sscanf(ptime.c_str(), "%04d-%02d-%02dT%02d:%02d:%02d", &y, &m, &d, &h, &mi, &s);
break;
case TimeTable::TIME_FORMAT::YYYYMMDDThhmm :
sscanf(ptime.c_str(), "%04d-%02d-%02dT%02d:%02d", &y, &m, &d, &h, &mi);
break;
case TimeTable::TIME_FORMAT::YYYYMMDDThhmmssmsk2 :
sscanf(ptime.c_str(), "%04d/%02d/%02d %02d:%02d:%02d.%03d", &y, &m, &d, &h, &mi, &s, &ms);
break;
case TimeTable::TIME_FORMAT::YYYYMMDDThhmmss2:
sscanf(ptime.c_str(), "%04d/%02d/%02d %02d:%02d:%02d", &y, &m, &d, &h, &mi, &s);
break;
case TimeTable::TIME_FORMAT::YYYYMMDDThhmm2:
sscanf(ptime.c_str(), "%04d/%02d/%02d %02d:%02d", &y, &m, &d, &h, &mi);
break;
default:
return 0.;
}
tabTime[0] = y;
tabTime[1] = m;
tabTime[2] = d;
tabTime[3] = h;
tabTime[4] = mi;
tabTime[5] = s;
tabTime[6] = ms;
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 */