TimePattern.cpp
8.54 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
#include "TimePattern.h"
#include <cstring>
#include <cstdio>
#include "../Common/Toolbox.h"
using namespace TREPS::Common;
namespace TREPS
{
namespace TimeManager
{
TimePatternClass::TimePatternClass(void) : app(NULL), pattern("")
{
this->app = ApplicationClass::getInstance();
}
bool TimePatternClass::init(const char *pattern)
{
if (this->pattern.compare(pattern) == 0)
return true;
this->partList.clear();
this->pattern = "";
string patStr = getTRIMStr(pattern);
//LOG4CXX_INFO(this->app->getLog()->getPtr(),"Set time pattern : " << patStr);
while (patStr.compare("") != 0)
{
t_TimePatternPart part;
size_t p = patStr.find("[");
if (p == string::npos)
{
//if cannot find [ char => patStr considerate as a fixed string
part.type = TPPT_STRING;
part.length = patStr.length();
part.strVal = patStr;
this->partList.push_back(part);
patStr = "";
continue;
}
else if (p != 0)
{
//if [ not at the first position => content before [ considerate as a fixed string
string s = patStr.substr(0,p);
part.type = TPPT_STRING;
part.length = s.length();
part.strVal = s;
this->partList.push_back(part);
}
patStr = patStr.substr(p+1,patStr.length()-p-1);
//the pattern part must be closed by ]
p = patStr.find("]");
if (p == string::npos)
{
LOG4CXX_ERROR(this->app->getLog()->getPtr(),"Missing ] in time pattern");
this->partList.clear();
return false;
}
//get pattern part type
string s = patStr.substr(0,p);
patStr = patStr.substr(p+1,patStr.length()-p-1);
if (s.compare("YYYY") == 0)
{
//full year - 4 digits
part.type = TPPT_YEAR_4D;
part.length = 4;
}
else if (s.compare("YY") == 0)
{
//two last digits of the year
part.type = TPPT_YEAR_2D;
part.length = 2;
}
else if (s.compare("MM") == 0)
{
//month of the year - 2 digits
part.type = TPPT_MONTH;
part.length = 2;
}
else if (s.compare("DD") == 0)
{
//day of month - 2 digits
part.type = TPPT_DAY;
part.length = 2;
}
else if (s.compare("DOY") == 0)
{
//day of year - 3 digits
part.type = TPPT_DOY;
part.length = 3;
}
else if (s.compare("hh") == 0)
{
//hour - 2 digits
part.type = TPPT_HOUR;
part.length = 2;
}
else if (s.compare("mm") == 0)
{
//minutes - 2 digits
part.type = TPPT_MIN;
part.length = 2;
}
else if (s.compare("ss") == 0)
{
//seconds - 2 digits
part.type = TPPT_SEC;
part.length = 2;
}
else if (s.compare("mls") == 0)
{
//milliseconds - 3 digits
part.type = TPPT_MLS;
part.length = 3;
}
else
{
//unknown pattern part type
LOG4CXX_ERROR(this->app->getLog()->getPtr(),"Unknown pattern arg : " << s);
this->partList.clear();
return false;
}
this->partList.push_back(part);
}
this->pattern = pattern;
return true;
}
bool TimePatternClass::load(const char *time, t_Time &t, bool verbose)
{
if (this->pattern.compare("") == 0)
{
if (verbose)
LOG4CXX_ERROR(this->app->getLog()->getPtr(),"No time pattern defined");
return false;
}
int crtPos = 0;
int timeLength = strlen(time);
//set DOY at the end, to be sure to have year loaded before
bool addDOY = false;
long int doy = 0;
for (t_TimePatternPartList::iterator part = this->partList.begin(); part != this->partList.end(); ++part)
{
if (crtPos >= timeLength)
{
if (verbose)
LOG4CXX_ERROR(this->app->getLog()->getPtr(),"Cannot load time : " << time);
return false;
}
const char *p = time;
p += crtPos;
switch ((*part).type)
{
case TPPT_STRING :
//nothing to do
break;
case TPPT_YEAR_4D :
//get year in 4 digits
if (sscanf(p,"%04ld",&t.ye) != 1)
{
if (verbose)
LOG4CXX_ERROR(this->app->getLog()->getPtr(),"Error to extract 4-digits year in " << time);
return false;
}
break;
case TPPT_YEAR_2D :
//get year in 2 digits
if (sscanf(p,"%02ld",&t.ye) != 1)
{
if (verbose)
LOG4CXX_ERROR(this->app->getLog()->getPtr(),"Error to extract 2-digits year in " << time);
return false;
}
t.ye += 2000;
break;
case TPPT_MONTH :
//get month in 2 digits
if (sscanf(p,"%02ld",&t.mo) != 1)
{
if (verbose)
LOG4CXX_ERROR(this->app->getLog()->getPtr(),"Error to extract month in " << time);
return false;
}
break;
case TPPT_DAY :
//get day in 2 digits
if (sscanf(p,"%02ld",&t.da) != 1)
{
if (verbose)
LOG4CXX_ERROR(this->app->getLog()->getPtr(),"Error to extract day in " << p);
return false;
}
break;
case TPPT_DOY :
{
//get day of year in 3 digits
if ((p[0] > 9) || (p[1] > 9) || (p[2] > 9) || (sscanf(p,"%03ld",&doy) != 1))
{
if (verbose)
LOG4CXX_ERROR(this->app->getLog()->getPtr(),"Error to extract day of year in " << time);
return false;
}
addDOY = true;
break;
}
case TPPT_HOUR :
//get hour in 2 digits
if (sscanf(p,"%02ld",&t.ho) != 1)
{
if (verbose)
LOG4CXX_ERROR(this->app->getLog()->getPtr(),"Error to extract hour in " << time);
return false;
}
break;
case TPPT_MIN :
//get minutes in 2 digits
if (sscanf(p,"%02ld",&t.mi) != 1)
{
if (verbose)
LOG4CXX_ERROR(this->app->getLog()->getPtr(),"Error to extract minute in " << time);
return false;
}
break;
case TPPT_SEC :
//get seconds in 2 digits
if (sscanf(p,"%02ld",&t.se) != 1)
{
if (verbose)
LOG4CXX_ERROR(this->app->getLog()->getPtr(),"Error to extract second in " << time);
return false;
}
break;
case TPPT_MLS :
//get milliseconds in 3 digits
if (sscanf(p,"%03ld",&t.ml) != 1)
{
if (verbose)
LOG4CXX_ERROR(this->app->getLog()->getPtr(),"Error to extract milli second in " << time);
return false;
}
break;
}
crtPos += (*part).length;
}
if (addDOY)
setDOY(t,doy);
return true;
}
bool TimePatternClass::write(t_Time &t, string &time)
{
time = "";
if (this->pattern.compare("") == 0)
{
LOG4CXX_ERROR(this->app->getLog()->getPtr(),"No time pattern defined");
return false;
}
char tmp[26];
for (t_TimePatternPartList::iterator part = this->partList.begin(); part != this->partList.end(); ++part)
{
switch ((*part).type)
{
case TPPT_STRING :
//write a string (ie separator)
time += (*part).strVal;
break;
case TPPT_YEAR_4D :
//write year in 4 digits
sprintf(tmp,"%04ld",t.ye);
tmp[4] = '\0';
time += tmp;
break;
case TPPT_YEAR_2D :
{
//write year in 2 digits
long int y = (t.ye%100);
sprintf(tmp,"%02ld",y);
tmp[2] = '\0';
time += tmp;
break;
}
case TPPT_MONTH :
//write month in 2 digits
sprintf(tmp,"%02ld",t.mo);
tmp[2] = '\0';
time += tmp;
break;
case TPPT_DAY :
//write day in 2 digits
sprintf(tmp,"%02ld",t.da);
tmp[2] = '\0';
time += tmp;
break;
case TPPT_DOY :
{
//write day of year in 3 digits
long int doy = getDOY(t);
sprintf(tmp,"%03ld",doy);
tmp[3] = '\0';
time += tmp;
break;
}
case TPPT_HOUR :
//write hour in 2 digits
sprintf(tmp,"%02ld",t.ho);
tmp[2] = '\0';
time += tmp;
break;
case TPPT_MIN :
//write minutes in 2 digits
sprintf(tmp,"%02ld",t.mi);
tmp[2] = '\0';
time += tmp;
break;
case TPPT_SEC :
//write seconds in 2 digits
sprintf(tmp,"%02ld",t.se);
tmp[2] = '\0';
time += tmp;
break;
case TPPT_MLS :
//write milliseconds in 3 digits
sprintf(tmp,"%03ld",t.ml);
tmp[3] = '\0';
time += tmp;
break;
}
}
return true;
}
int TimePatternClass::getSize(void)
{
//get size of the generated string by a pattern
if (this->pattern.compare("") == 0)
{
LOG4CXX_ERROR(this->app->getLog()->getPtr(),"No time pattern defined");
return false;
}
int size = 0;
for (t_TimePatternPartList::iterator part = this->partList.begin(); part != this->partList.end(); ++part)
size += (*part).length;
return size;
}
}
}