/src/logging-log4cxx/src/main/cpp/simpledateformat.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Licensed to the Apache Software Foundation (ASF) under one or more |
3 | | * contributor license agreements. See the NOTICE file distributed with |
4 | | * this work for additional information regarding copyright ownership. |
5 | | * The ASF licenses this file to You under the Apache License, Version 2.0 |
6 | | * (the "License"); you may not use this file except in compliance with |
7 | | * the License. You may obtain a copy of the License at |
8 | | * |
9 | | * http://www.apache.org/licenses/LICENSE-2.0 |
10 | | * |
11 | | * Unless required by applicable law or agreed to in writing, software |
12 | | * distributed under the License is distributed on an "AS IS" BASIS, |
13 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
14 | | * See the License for the specific language governing permissions and |
15 | | * limitations under the License. |
16 | | */ |
17 | | #include <log4cxx/logstring.h> |
18 | | #include <log4cxx/helpers/simpledateformat.h> |
19 | | |
20 | | #include <apr_time.h> |
21 | | #include <apr_strings.h> |
22 | | #include <sstream> |
23 | | #include <log4cxx/helpers/transcoder.h> |
24 | | #include <log4cxx/helpers/stringhelper.h> |
25 | | #include <assert.h> |
26 | | #if !defined(LOG4CXX) |
27 | | #define LOG4CXX 1 |
28 | | #endif |
29 | | #include <log4cxx/private/log4cxx_private.h> |
30 | | #include <log4cxx/helpers/pool.h> |
31 | | |
32 | | using namespace LOG4CXX_NS; |
33 | | using namespace LOG4CXX_NS::helpers; |
34 | | |
35 | | using namespace std; |
36 | | |
37 | | #if LOG4CXX_HAS_STD_LOCALE |
38 | | #include <locale> |
39 | | #endif |
40 | | |
41 | | #if defined(_MSC_VER) && _MSC_VER < 1300 |
42 | | #define HAS_FACET(locale, type) _HAS(locale, type) |
43 | | #define USE_FACET(locale, type) _USE(locale, type) |
44 | | #define PUT_FACET(facet, os, time, spec) facet.put(os, os, time, spec) |
45 | | #else |
46 | | #if defined(_RWSTD_NO_TEMPLATE_ON_RETURN_TYPE) |
47 | | #define HAS_FACET(locale, type) std::has_facet(locale, (type*) 0) |
48 | | #define USE_FACET(locale, type) std::use_facet(locale, (type*) 0) |
49 | | #else |
50 | 6.86k | #define HAS_FACET(locale, type) std::has_facet < type >(locale) |
51 | 6.86k | #define USE_FACET(locale, type) std::use_facet < type >(locale) |
52 | | #endif |
53 | 47.3k | #define PUT_FACET(facet, os, time, spec) facet.put(os, os, os.fill(), time, spec) |
54 | | #endif |
55 | | |
56 | | namespace LOG4CXX_NS |
57 | | { |
58 | | namespace helpers |
59 | | { |
60 | | namespace SimpleDateFormatImpl |
61 | | { |
62 | | typedef void (*incrementFunction)(tm& time, apr_time_exp_t& apr_time); |
63 | | |
64 | | /** |
65 | | * Abstract inner class representing one format token |
66 | | * (one or more instances of a character). |
67 | | */ |
68 | | class PatternToken |
69 | | { |
70 | | public: |
71 | | PatternToken() |
72 | 81.6k | { |
73 | 81.6k | } |
74 | | |
75 | | virtual ~PatternToken() |
76 | 81.6k | { |
77 | 81.6k | } |
78 | | |
79 | | /** |
80 | | * Sets the time zone. |
81 | | * @param zone new time zone. |
82 | | */ |
83 | | virtual void setTimeZone(const TimeZonePtr& zone) |
84 | 77.2k | { |
85 | 77.2k | } |
86 | | |
87 | | /** |
88 | | * Appends the formatted content to the string. |
89 | | * @param s string to which format contribution is appended. |
90 | | * @param date exploded date/time. |
91 | | * @param p memory pool. |
92 | | */ |
93 | | virtual void format(LogString& s, |
94 | | const apr_time_exp_t& date, |
95 | | LOG4CXX_NS::helpers::Pool& p) const = 0; |
96 | | |
97 | | protected: |
98 | | |
99 | | static void incrementMonth(tm& time, apr_time_exp_t& aprtime) |
100 | 18.5k | { |
101 | 18.5k | time.tm_mon++; |
102 | 18.5k | aprtime.tm_mon++; |
103 | 18.5k | } |
104 | | |
105 | | static void incrementDay(tm& time, apr_time_exp_t& aprtime) |
106 | 25.3k | { |
107 | 25.3k | time.tm_wday++; |
108 | 25.3k | aprtime.tm_wday++; |
109 | 25.3k | } |
110 | | |
111 | | static void incrementHalfDay(tm& time, apr_time_exp_t& aprtime) |
112 | 3.40k | { |
113 | 3.40k | time.tm_hour += 12; |
114 | 3.40k | aprtime.tm_hour += 12; |
115 | 3.40k | } |
116 | | |
117 | | static void renderFacet(const std::locale* locale, |
118 | | incrementFunction inc, |
119 | | char spec, |
120 | | unsigned int wspec, |
121 | | const char* aprspec, |
122 | | std::vector<LogString>& values) |
123 | 6.86k | { |
124 | 6.86k | std::vector<LogString>::iterator valueIter = values.begin(); |
125 | 6.86k | tm time; |
126 | 6.86k | memset(&time, 0, sizeof(time)); |
127 | 6.86k | apr_time_exp_t aprtime; |
128 | 6.86k | memset(&aprtime, 0, sizeof(aprtime)); |
129 | 6.86k | #if LOG4CXX_HAS_STD_LOCALE |
130 | | |
131 | 6.86k | if (locale != NULL) |
132 | 6.86k | { |
133 | 6.86k | #if LOG4CXX_WCHAR_T_API |
134 | | |
135 | 6.86k | if (HAS_FACET(*locale, std::time_put<wchar_t>)) |
136 | 6.86k | { |
137 | 6.86k | const std::time_put<wchar_t>& facet = USE_FACET(*locale, std::time_put<wchar_t>); |
138 | 6.86k | size_t start = 0; |
139 | 6.86k | std::basic_ostringstream<wchar_t> os; |
140 | | |
141 | 54.1k | for (; valueIter != values.end(); valueIter++) |
142 | 47.3k | { |
143 | 47.3k | PUT_FACET(facet, os, &time, (char)wspec); |
144 | 47.3k | Transcoder::decode(os.str().substr(start), *valueIter); |
145 | 47.3k | start = os.str().length(); |
146 | 47.3k | (*inc)(time, aprtime); |
147 | 47.3k | } |
148 | 6.86k | } |
149 | 0 | else |
150 | 0 | #endif |
151 | 0 | if (HAS_FACET(*locale, std::time_put<char>)) |
152 | 0 | { |
153 | 0 | const std::time_put<char>& facet = USE_FACET(*locale, std::time_put<char> ); |
154 | 0 | size_t start = 0; |
155 | 0 | std::ostringstream os; |
156 | |
|
157 | 0 | for (; valueIter != values.end(); valueIter++) |
158 | 0 | { |
159 | 0 | PUT_FACET(facet, os, &time, spec); |
160 | 0 | Transcoder::decode(os.str().substr(start), *valueIter); |
161 | 0 | start = os.str().length(); |
162 | 0 | (*inc)(time, aprtime); |
163 | 0 | } |
164 | 0 | } |
165 | 6.86k | } |
166 | | |
167 | 6.86k | #endif |
168 | 6.86k | const size_t BUFSIZE = 256; |
169 | 6.86k | char buf[BUFSIZE]; |
170 | 6.86k | memset(buf, 0, BUFSIZE); |
171 | 6.86k | apr_size_t retsize = 0; |
172 | | |
173 | 6.86k | for (; valueIter != values.end(); valueIter++) |
174 | 0 | { |
175 | 0 | apr_status_t stat = apr_strftime(buf, &retsize, BUFSIZE, aprspec, &aprtime); |
176 | 0 | (*inc)(time, aprtime); |
177 | |
|
178 | 0 | if (stat == APR_SUCCESS) |
179 | 0 | { |
180 | 0 | Transcoder::decode(std::string(buf, retsize), *valueIter); |
181 | 0 | } |
182 | 0 | else |
183 | 0 | { |
184 | 0 | valueIter->append(1, (logchar) 0x3F); |
185 | 0 | } |
186 | 0 | } |
187 | 6.86k | } |
188 | | |
189 | | private: |
190 | | /** |
191 | | * Private copy constructor. |
192 | | */ |
193 | | PatternToken(const PatternToken&); |
194 | | |
195 | | /** |
196 | | * Private assignment operator. |
197 | | */ |
198 | | PatternToken& operator=(const PatternToken&); |
199 | | }; |
200 | | |
201 | | |
202 | | class LiteralToken : public PatternToken |
203 | | { |
204 | | public: |
205 | 15.2k | LiteralToken( logchar ch1, int count1 ) : ch( ch1 ), count( count1 ) |
206 | 15.2k | { |
207 | 15.2k | } |
208 | | |
209 | | void format( LogString& s, const apr_time_exp_t&, Pool& /* p */ ) const |
210 | 41.5k | { |
211 | 41.5k | s.append( count, ch ); |
212 | 41.5k | } |
213 | | |
214 | | private: |
215 | | logchar ch; |
216 | | int count; |
217 | | }; |
218 | | |
219 | | |
220 | | |
221 | | class EraToken : public PatternToken |
222 | | { |
223 | | public: |
224 | | EraToken( int /* count */, const std::locale* /* locale */ ) |
225 | 536 | { |
226 | 536 | } |
227 | | |
228 | | void format(LogString& s, const apr_time_exp_t& /* tm */, Pool& /* p */ ) const |
229 | 1.38k | { |
230 | 1.38k | s.append(1, (logchar) 0x41 /* 'A' */); |
231 | 1.38k | s.append(1, (logchar) 0x44 /* 'D' */); |
232 | 1.38k | } |
233 | | }; |
234 | | |
235 | | |
236 | | |
237 | | class NumericToken : public PatternToken |
238 | | { |
239 | | public: |
240 | 37.2k | NumericToken( size_t width1 ) : width( width1 ) |
241 | 37.2k | { |
242 | 37.2k | } |
243 | | |
244 | | virtual int getField( const apr_time_exp_t& tm ) const = 0; |
245 | | |
246 | | void format( LogString& s, const apr_time_exp_t& tm, Pool& p ) const |
247 | 106k | { |
248 | 106k | size_t initialLength = s.length(); |
249 | | |
250 | 106k | StringHelper::toString( getField( tm ), p, s ); |
251 | 106k | size_t finalLength = s.length(); |
252 | | |
253 | 106k | if ( initialLength + width > finalLength ) |
254 | 4.24k | { |
255 | 4.24k | s.insert( initialLength, ( initialLength + width ) - finalLength, (logchar) 0x30 /* '0' */); |
256 | 4.24k | } |
257 | 106k | } |
258 | | |
259 | | private: |
260 | | size_t width; |
261 | | }; |
262 | | |
263 | | |
264 | | |
265 | | class YearToken : public NumericToken |
266 | | { |
267 | | public: |
268 | 19.2k | YearToken( int width1 ) : NumericToken( width1 ) |
269 | 19.2k | { |
270 | 19.2k | } |
271 | | |
272 | | int getField( const apr_time_exp_t& tm ) const |
273 | 55.1k | { |
274 | 55.1k | return 1900 + tm.tm_year; |
275 | 55.1k | } |
276 | | }; |
277 | | |
278 | | |
279 | | |
280 | | class MonthToken : public NumericToken |
281 | | { |
282 | | public: |
283 | 1.69k | MonthToken( int width1 ) : NumericToken( width1 ) |
284 | 1.69k | { |
285 | 1.69k | } |
286 | | |
287 | | int getField( const apr_time_exp_t& tm ) const |
288 | 4.76k | { |
289 | 4.76k | return tm.tm_mon + 1; |
290 | 4.76k | } |
291 | | }; |
292 | | |
293 | | |
294 | | |
295 | | class AbbreviatedMonthNameToken : public PatternToken |
296 | | { |
297 | | public: |
298 | 693 | AbbreviatedMonthNameToken(int, const std::locale* locale) : names( 12 ) |
299 | 693 | { |
300 | 693 | renderFacet(locale, PatternToken::incrementMonth, 'b', 0x62, "%b", names); |
301 | 693 | } |
302 | | |
303 | | void format(LogString& s, const apr_time_exp_t& tm, Pool& /* p */ ) const |
304 | 1.69k | { |
305 | 1.69k | s.append( names[tm.tm_mon] ); |
306 | 1.69k | } |
307 | | |
308 | | private: |
309 | | std::vector < LogString > names; |
310 | | }; |
311 | | |
312 | | |
313 | | |
314 | | class FullMonthNameToken : public PatternToken |
315 | | { |
316 | | public: |
317 | 856 | FullMonthNameToken( int width, const std::locale* locale) : names( 12 ) |
318 | 856 | { |
319 | 856 | renderFacet(locale, PatternToken::incrementMonth, 'B', 0x42, "%B", names); |
320 | 856 | } |
321 | | |
322 | | void format( LogString& s, const apr_time_exp_t& tm, Pool& /* p */ ) const |
323 | 2.09k | { |
324 | 2.09k | s.append( names[tm.tm_mon] ); |
325 | 2.09k | } |
326 | | |
327 | | private: |
328 | | std::vector < LogString > names; |
329 | | }; |
330 | | |
331 | | |
332 | | |
333 | | class WeekInYearToken : public NumericToken |
334 | | { |
335 | | public: |
336 | 814 | WeekInYearToken( int width1 ) : NumericToken( width1 ) |
337 | 814 | { |
338 | 814 | } |
339 | | |
340 | | int getField( const apr_time_exp_t& tm ) const |
341 | 2.42k | { |
342 | 2.42k | return tm.tm_yday / 7; |
343 | 2.42k | } |
344 | | }; |
345 | | |
346 | | |
347 | | |
348 | | class WeekInMonthToken : public NumericToken |
349 | | { |
350 | | public: |
351 | 1.04k | WeekInMonthToken( int width1 ) : NumericToken( width1 ) |
352 | 1.04k | { |
353 | 1.04k | } |
354 | | |
355 | | int getField( const apr_time_exp_t& tm ) const |
356 | 2.72k | { |
357 | 2.72k | return tm.tm_mday / 7; |
358 | 2.72k | } |
359 | | }; |
360 | | |
361 | | |
362 | | |
363 | | class DayInMonthToken : public NumericToken |
364 | | { |
365 | | public: |
366 | 1.06k | DayInMonthToken( int width1 ) : NumericToken( width1 ) |
367 | 1.06k | { |
368 | 1.06k | } |
369 | | |
370 | | int getField( const apr_time_exp_t& tm ) const |
371 | 3.00k | { |
372 | 3.00k | return tm.tm_mday; |
373 | 3.00k | } |
374 | | }; |
375 | | |
376 | | |
377 | | |
378 | | class DayInYearToken : public NumericToken |
379 | | { |
380 | | public: |
381 | 1.61k | DayInYearToken( int width1 ) : NumericToken( width1 ) |
382 | 1.61k | { |
383 | 1.61k | } |
384 | | |
385 | | int getField( const apr_time_exp_t& tm ) const |
386 | 4.79k | { |
387 | 4.79k | return tm.tm_yday; |
388 | 4.79k | } |
389 | | }; |
390 | | |
391 | | |
392 | | |
393 | | class DayOfWeekInMonthToken : public NumericToken |
394 | | { |
395 | | public: |
396 | 633 | DayOfWeekInMonthToken( int width1 ) : NumericToken( width1 ) |
397 | 633 | { |
398 | 633 | } |
399 | | |
400 | | int getField( const apr_time_exp_t& /* tm */ ) const |
401 | 1.62k | { |
402 | 1.62k | return -1; |
403 | 1.62k | } |
404 | | }; |
405 | | |
406 | | |
407 | | |
408 | | class AbbreviatedDayNameToken : public PatternToken |
409 | | { |
410 | | public: |
411 | 3.05k | AbbreviatedDayNameToken( int width, const std::locale* locale) : names( 7 ) |
412 | 3.05k | { |
413 | 3.05k | renderFacet(locale, PatternToken::incrementDay, 'a', 0x61, "%a", names); |
414 | 3.05k | } |
415 | | |
416 | | void format( LogString& s, const apr_time_exp_t& tm, Pool& /* p */ ) const |
417 | 7.98k | { |
418 | 7.98k | s.append( names[tm.tm_wday] ); |
419 | 7.98k | } |
420 | | |
421 | | private: |
422 | | std::vector < LogString > names; |
423 | | |
424 | | }; |
425 | | |
426 | | |
427 | | |
428 | | class FullDayNameToken : public PatternToken |
429 | | { |
430 | | public: |
431 | 570 | FullDayNameToken( int width, const std::locale* locale) : names( 7 ) |
432 | 570 | { |
433 | 570 | renderFacet(locale, PatternToken::incrementDay, 'A', 0x41, "%A", names); |
434 | 570 | } |
435 | | |
436 | | void format( LogString& s, const apr_time_exp_t& tm, Pool& /* p */ ) const |
437 | 1.55k | { |
438 | 1.55k | s.append( names[tm.tm_wday] ); |
439 | 1.55k | } |
440 | | |
441 | | private: |
442 | | std::vector < LogString > names; |
443 | | |
444 | | }; |
445 | | |
446 | | |
447 | | |
448 | | class MilitaryHourToken : public NumericToken |
449 | | { |
450 | | public: |
451 | 1.90k | MilitaryHourToken( int width1, int offset1 ) : NumericToken( width1 ), offset( offset1 ) |
452 | 1.90k | { |
453 | 1.90k | } |
454 | | |
455 | | int getField( const apr_time_exp_t& tm ) const |
456 | 5.26k | { |
457 | 5.26k | return tm.tm_hour + offset; |
458 | 5.26k | } |
459 | | |
460 | | private: |
461 | | int offset; |
462 | | }; |
463 | | |
464 | | |
465 | | |
466 | | class HourToken : public NumericToken |
467 | | { |
468 | | public: |
469 | 1.38k | HourToken( int width1, int /* offset1 */ ) : NumericToken( width1 ), offset( 0 ) |
470 | 1.38k | { |
471 | 1.38k | } |
472 | | |
473 | | int getField( const apr_time_exp_t& tm ) const |
474 | 3.19k | { |
475 | 3.19k | return ( ( tm.tm_hour + 12 - offset ) % 12 ) + offset; |
476 | 3.19k | } |
477 | | |
478 | | private: |
479 | | int offset; |
480 | | }; |
481 | | |
482 | | |
483 | | |
484 | | class MinuteToken : public NumericToken |
485 | | { |
486 | | public: |
487 | 1.11k | MinuteToken( int width1 ) : NumericToken( width1 ) |
488 | 1.11k | { |
489 | 1.11k | } |
490 | | |
491 | | int getField( const apr_time_exp_t& tm ) const |
492 | 3.25k | { |
493 | 3.25k | return tm.tm_min; |
494 | 3.25k | } |
495 | | }; |
496 | | |
497 | | |
498 | | |
499 | | class SecondToken : public NumericToken |
500 | | { |
501 | | public: |
502 | 1.42k | SecondToken( int width1 ) : NumericToken( width1 ) |
503 | 1.42k | { |
504 | 1.42k | } |
505 | | |
506 | | int getField( const apr_time_exp_t& tm ) const |
507 | 3.98k | { |
508 | 3.98k | return tm.tm_sec; |
509 | 3.98k | } |
510 | | }; |
511 | | |
512 | | |
513 | | |
514 | | class MillisecondToken : public NumericToken |
515 | | { |
516 | | public: |
517 | 4.98k | MillisecondToken( int width1 ) : NumericToken( width1 ) |
518 | 4.98k | { |
519 | 4.98k | } |
520 | | |
521 | | int getField( const apr_time_exp_t& tm ) const |
522 | 14.9k | { |
523 | 14.9k | return tm.tm_usec / 1000; |
524 | 14.9k | } |
525 | | }; |
526 | | |
527 | | |
528 | | |
529 | | class MicrosecondToken : public NumericToken |
530 | | { |
531 | | public: |
532 | 343 | MicrosecondToken( int width1 ) : NumericToken( width1 ) |
533 | 343 | { |
534 | 343 | } |
535 | | |
536 | | int getField( const apr_time_exp_t& tm ) const |
537 | 1.02k | { |
538 | 1.02k | return tm.tm_usec; |
539 | 1.02k | } |
540 | | }; |
541 | | |
542 | | |
543 | | |
544 | | class AMPMToken : public PatternToken |
545 | | { |
546 | | public: |
547 | 1.70k | AMPMToken( int width, const std::locale* locale) : names( 2 ) |
548 | 1.70k | { |
549 | 1.70k | renderFacet(locale, PatternToken::incrementHalfDay, 'p', 0x70, "%p", names); |
550 | 1.70k | } |
551 | | |
552 | | void format( LogString& s, const apr_time_exp_t& tm, Pool& /* p */ ) const |
553 | 4.29k | { |
554 | 4.29k | s.append( names[tm.tm_hour / 12] ); |
555 | 4.29k | } |
556 | | |
557 | | private: |
558 | | std::vector < LogString > names; |
559 | | }; |
560 | | |
561 | | |
562 | | |
563 | | class GeneralTimeZoneToken : public PatternToken |
564 | | { |
565 | | public: |
566 | | GeneralTimeZoneToken( int /* width */ ) |
567 | 4.37k | { |
568 | 4.37k | } |
569 | | |
570 | | void format( LogString& s, const apr_time_exp_t&, Pool& /* p */ ) const |
571 | 12.6k | { |
572 | 12.6k | s.append(timeZone->getID()); |
573 | 12.6k | } |
574 | | |
575 | | void setTimeZone( const TimeZonePtr& zone ) |
576 | 4.37k | { |
577 | 4.37k | timeZone = zone; |
578 | 4.37k | } |
579 | | |
580 | | private: |
581 | | TimeZonePtr timeZone; |
582 | | }; |
583 | | |
584 | | |
585 | | |
586 | | class RFC822TimeZoneToken : public PatternToken |
587 | | { |
588 | | public: |
589 | | RFC822TimeZoneToken( int /* width */ ) |
590 | 17.3k | { |
591 | 17.3k | } |
592 | | |
593 | | void format( LogString& s, const apr_time_exp_t& tm, Pool& p ) const |
594 | 49.8k | { |
595 | 49.8k | if ( tm.tm_gmtoff == 0 ) |
596 | 4.16k | { |
597 | 4.16k | s.append( 1, (logchar) 0x5A /* 'Z' */ ); |
598 | 4.16k | } |
599 | 45.7k | else |
600 | 45.7k | { |
601 | 45.7k | apr_int32_t off = tm.tm_gmtoff; |
602 | 45.7k | s.reserve(s.length() + 5); |
603 | | |
604 | 45.7k | if ( off < 0 ) |
605 | 2.94k | { |
606 | 2.94k | s.push_back( '-' ); |
607 | 2.94k | off = -off; |
608 | 42.7k | }else{ |
609 | 42.7k | s.push_back( '+' ); |
610 | 42.7k | } |
611 | | |
612 | 45.7k | LogString hours; |
613 | 45.7k | StringHelper::toString( off / 3600, p, hours ); |
614 | 45.7k | if( hours.size() == 1 ){ |
615 | 44.9k | s.push_back( '0' ); |
616 | 44.9k | } |
617 | 45.7k | s.append(hours); |
618 | | |
619 | 45.7k | LogString min; |
620 | 45.7k | StringHelper::toString( ( off % 3600 ) / 60, p, min ); |
621 | 45.7k | if( min.size() == 1 ){ |
622 | 44.6k | s.push_back( '0' ); |
623 | 44.6k | } |
624 | 45.7k | s.append(min); |
625 | 45.7k | } |
626 | 49.8k | } |
627 | | }; |
628 | | |
629 | | |
630 | | |
631 | | |
632 | | } |
633 | | } |
634 | | } |
635 | | |
636 | | |
637 | | using namespace LOG4CXX_NS::helpers::SimpleDateFormatImpl; |
638 | | |
639 | | void SimpleDateFormat::addToken(const logchar spec, const int repeat, const std::locale* locale, |
640 | | std::vector < PatternToken* >& pattern ) |
641 | 81.6k | { |
642 | 81.6k | PatternToken* token = NULL; |
643 | | |
644 | 81.6k | switch ( spec ) |
645 | 81.6k | { |
646 | 536 | case 0x47: // 'G' |
647 | 536 | token = ( new EraToken( repeat, locale ) ); |
648 | 536 | break; |
649 | | |
650 | 19.2k | case 0x79: // 'y' |
651 | 19.2k | token = ( new YearToken( repeat ) ); |
652 | 19.2k | break; |
653 | | |
654 | 3.23k | case 0x4D: // 'M' |
655 | 3.23k | if ( repeat <= 2 ) |
656 | 1.69k | { |
657 | 1.69k | token = ( new MonthToken( repeat ) ); |
658 | 1.69k | } |
659 | 1.54k | else if ( repeat <= 3 ) |
660 | 693 | { |
661 | 693 | token = ( new AbbreviatedMonthNameToken( repeat, locale ) ); |
662 | 693 | } |
663 | 856 | else |
664 | 856 | { |
665 | 856 | token = ( new FullMonthNameToken( repeat, locale ) ); |
666 | 856 | } |
667 | | |
668 | 3.23k | break; |
669 | | |
670 | 814 | case 0x77: // 'w' |
671 | 814 | token = ( new WeekInYearToken( repeat ) ); |
672 | 814 | break; |
673 | | |
674 | 1.04k | case 0x57: // 'W' |
675 | 1.04k | token = ( new WeekInMonthToken( repeat ) ); |
676 | 1.04k | break; |
677 | | |
678 | 1.61k | case 0x44: // 'D' |
679 | 1.61k | token = ( new DayInYearToken( repeat ) ); |
680 | 1.61k | break; |
681 | | |
682 | 1.06k | case 0x64: // 'd' |
683 | 1.06k | token = ( new DayInMonthToken( repeat ) ); |
684 | 1.06k | break; |
685 | | |
686 | 633 | case 0x46: // 'F' |
687 | 633 | token = ( new DayOfWeekInMonthToken( repeat ) ); |
688 | 633 | break; |
689 | | |
690 | 3.62k | case 0x45: // 'E' |
691 | 3.62k | if ( repeat <= 3 ) |
692 | 3.05k | { |
693 | 3.05k | token = ( new AbbreviatedDayNameToken( repeat, locale ) ); |
694 | 3.05k | } |
695 | 570 | else |
696 | 570 | { |
697 | 570 | token = ( new FullDayNameToken( repeat, locale ) ); |
698 | 570 | } |
699 | | |
700 | 3.62k | break; |
701 | | |
702 | 1.70k | case 0x61: // 'a' |
703 | 1.70k | token = ( new AMPMToken( repeat, locale ) ); |
704 | 1.70k | break; |
705 | | |
706 | 1.11k | case 0x48: // 'H' |
707 | 1.11k | token = ( new MilitaryHourToken( repeat, 0 ) ); |
708 | 1.11k | break; |
709 | | |
710 | 791 | case 0x6B: // 'k' |
711 | 791 | token = ( new MilitaryHourToken( repeat, 1 ) ); |
712 | 791 | break; |
713 | | |
714 | 863 | case 0x4B: // 'K' |
715 | 863 | token = ( new HourToken( repeat, 0 ) ); |
716 | 863 | break; |
717 | | |
718 | 524 | case 0x68: // 'h' |
719 | 524 | token = ( new HourToken( repeat, 1 ) ); |
720 | 524 | break; |
721 | | |
722 | 1.11k | case 0x6D: // 'm' |
723 | 1.11k | token = ( new MinuteToken( repeat ) ); |
724 | 1.11k | break; |
725 | | |
726 | 1.42k | case 0x73: // 's' |
727 | 1.42k | token = ( new SecondToken( repeat ) ); |
728 | 1.42k | break; |
729 | | |
730 | 5.32k | case 0x53: // 'S' |
731 | 5.32k | if ( repeat == 6 ) |
732 | 343 | { |
733 | 343 | token = ( new MicrosecondToken( repeat ) ); |
734 | 343 | } |
735 | 4.98k | else |
736 | 4.98k | { |
737 | | // It would be nice to support patterns with arbitrary |
738 | | // subsecond precision (like "s.S" or "s.SSSS"), but we |
739 | | // don't; so this is a back-compatible default. |
740 | 4.98k | token = ( new MillisecondToken( repeat ) ); |
741 | 4.98k | } |
742 | | |
743 | 5.32k | break; |
744 | | |
745 | 4.37k | case 0x7A: // 'z' |
746 | 4.37k | token = ( new GeneralTimeZoneToken( repeat ) ); |
747 | 4.37k | break; |
748 | | |
749 | 17.3k | case 0x5A: // 'Z' |
750 | 17.3k | token = ( new RFC822TimeZoneToken( repeat ) ); |
751 | 17.3k | break; |
752 | | |
753 | 15.2k | default: |
754 | 15.2k | token = ( new LiteralToken( spec, repeat ) ); |
755 | 81.6k | } |
756 | | |
757 | 81.6k | assert( token != NULL ); |
758 | 81.6k | pattern.push_back( token ); |
759 | 81.6k | } |
760 | | |
761 | | |
762 | | void SimpleDateFormat::parsePattern( const LogString& fmt, const std::locale* locale, |
763 | | std::vector < PatternToken* >& pattern ) |
764 | 1.18k | { |
765 | 1.18k | if ( !fmt.empty() ) |
766 | 1.18k | { |
767 | 1.18k | LogString::const_iterator iter = fmt.begin(); |
768 | 1.18k | int repeat = 1; |
769 | 1.18k | logchar prevChar = * iter; |
770 | | |
771 | 107k | for ( iter++; iter != fmt.end(); iter++ ) |
772 | 106k | { |
773 | 106k | if ( * iter == prevChar ) |
774 | 26.0k | { |
775 | 26.0k | repeat++; |
776 | 26.0k | } |
777 | 80.4k | else |
778 | 80.4k | { |
779 | 80.4k | addToken( prevChar, repeat, locale, pattern ); |
780 | 80.4k | prevChar = * iter; |
781 | 80.4k | repeat = 1; |
782 | 80.4k | } |
783 | 106k | } |
784 | | |
785 | 1.18k | addToken( prevChar, repeat, locale, pattern ); |
786 | 1.18k | } |
787 | 1.18k | } |
788 | | |
789 | | |
790 | | struct SimpleDateFormat::SimpleDateFormatPrivate{ |
791 | | SimpleDateFormatPrivate() : |
792 | 1.18k | timeZone(TimeZone::getDefault()) |
793 | 1.18k | {} |
794 | | |
795 | | /** |
796 | | * Time zone. |
797 | | */ |
798 | | TimeZonePtr timeZone; |
799 | | |
800 | | /** |
801 | | * List of tokens. |
802 | | */ |
803 | | PatternTokenList pattern; |
804 | | }; |
805 | | |
806 | 1.18k | SimpleDateFormat::SimpleDateFormat( const LogString& fmt ) : m_priv(std::make_unique<SimpleDateFormatPrivate>()) |
807 | 1.18k | { |
808 | 1.18k | #if LOG4CXX_HAS_STD_LOCALE |
809 | 1.18k | std::locale defaultLocale; |
810 | 1.18k | parsePattern( fmt, & defaultLocale, m_priv->pattern ); |
811 | | #else |
812 | | parsePattern( fmt, NULL, m_priv->pattern ); |
813 | | #endif |
814 | | |
815 | 1.18k | for (auto const& item : m_priv->pattern) |
816 | 81.6k | { |
817 | 81.6k | item->setTimeZone( m_priv->timeZone ); |
818 | 81.6k | } |
819 | 1.18k | } |
820 | | |
821 | 0 | SimpleDateFormat::SimpleDateFormat( const LogString& fmt, const std::locale* locale ) : m_priv(std::make_unique<SimpleDateFormatPrivate>()) |
822 | 0 | { |
823 | 0 | parsePattern( fmt, locale, m_priv->pattern ); |
824 | |
|
825 | 0 | for (auto const& item : m_priv->pattern) |
826 | 0 | { |
827 | 0 | item->setTimeZone( m_priv->timeZone ); |
828 | 0 | } |
829 | 0 | } |
830 | | |
831 | | |
832 | | SimpleDateFormat::~SimpleDateFormat() |
833 | 1.18k | { |
834 | 1.18k | for (auto item : m_priv->pattern) |
835 | 81.6k | { |
836 | 81.6k | delete item; |
837 | 81.6k | } |
838 | 1.18k | } |
839 | | |
840 | | |
841 | | void SimpleDateFormat::format( LogString& s, log4cxx_time_t time, Pool& p ) const |
842 | 3.25k | { |
843 | 3.25k | apr_time_exp_t exploded; |
844 | 3.25k | apr_status_t stat = m_priv->timeZone->explode( & exploded, time ); |
845 | | |
846 | 3.25k | if ( stat == APR_SUCCESS ) |
847 | 3.25k | { |
848 | 232k | for ( PatternTokenList::const_iterator iter = m_priv->pattern.begin(); iter != m_priv->pattern.end(); iter++ ) |
849 | 229k | { |
850 | 229k | ( * iter )->format( s, exploded, p ); |
851 | 229k | } |
852 | 3.25k | } |
853 | 3.25k | } |
854 | | |
855 | | void SimpleDateFormat::setTimeZone( const TimeZonePtr& zone ) |
856 | 959 | { |
857 | 959 | m_priv->timeZone = zone; |
858 | 959 | } |