1 | // file : xsd/cxx/tree/serialization/date-time.txx |
---|
2 | // author : Boris Kolpackov <boris@codesynthesis.com> |
---|
3 | // copyright : Copyright (c) 2005-2008 Code Synthesis Tools CC |
---|
4 | // license : GNU GPL v2 + exceptions; see accompanying LICENSE file |
---|
5 | |
---|
6 | #include <locale> |
---|
7 | #include <string> |
---|
8 | #include <ostream> |
---|
9 | #include <sstream> |
---|
10 | |
---|
11 | #include <xsd/cxx/tree/bits/literals.hxx> // bits::{gday_prefix,gmonth_prefix} |
---|
12 | |
---|
13 | namespace xsd |
---|
14 | { |
---|
15 | namespace cxx |
---|
16 | { |
---|
17 | namespace tree |
---|
18 | { |
---|
19 | // time_zone |
---|
20 | // |
---|
21 | namespace bits |
---|
22 | { |
---|
23 | // Assumes the fill character is set to '0'. |
---|
24 | // |
---|
25 | template <typename C> |
---|
26 | void |
---|
27 | zone_insert (std::basic_ostream<C>& os, const time_zone& z) |
---|
28 | { |
---|
29 | // time-zone := Z|(+|-)HH:MM |
---|
30 | // |
---|
31 | short h = z.zone_hours (); |
---|
32 | short m = z.zone_minutes (); |
---|
33 | |
---|
34 | if (h == 0 && m == 0) |
---|
35 | { |
---|
36 | os << C ('Z'); |
---|
37 | } |
---|
38 | else |
---|
39 | { |
---|
40 | if (h < 0 || m < 0) |
---|
41 | { |
---|
42 | h = -h; |
---|
43 | m = -m; |
---|
44 | os << C ('-'); |
---|
45 | } |
---|
46 | else |
---|
47 | os << C ('+'); |
---|
48 | |
---|
49 | if (h >= 0 && h <= 14 && m >= 0 && m <= 59) |
---|
50 | { |
---|
51 | os.width (2); |
---|
52 | os << h << C (':'); |
---|
53 | os.width (2); |
---|
54 | os << m; |
---|
55 | } |
---|
56 | } |
---|
57 | } |
---|
58 | } |
---|
59 | |
---|
60 | // gday |
---|
61 | // |
---|
62 | namespace bits |
---|
63 | { |
---|
64 | template <typename C, typename B> |
---|
65 | void |
---|
66 | insert (std::basic_ostream<C>& os, const tree::gday<C, B>& x) |
---|
67 | { |
---|
68 | if (x.day () < 32) |
---|
69 | { |
---|
70 | // Save some time and space by not restoring the fill character |
---|
71 | // since it is the same in case of a list. |
---|
72 | // |
---|
73 | os.fill (C ('0')); |
---|
74 | os << bits::gday_prefix<C> (); |
---|
75 | os.width (2); |
---|
76 | os << x.day (); |
---|
77 | |
---|
78 | if (x.zone_present ()) |
---|
79 | zone_insert (os, x); |
---|
80 | } |
---|
81 | } |
---|
82 | } |
---|
83 | |
---|
84 | template <typename C, typename B> |
---|
85 | inline void |
---|
86 | operator<< (xercesc::DOMElement& e, const gday<C, B>& x) |
---|
87 | { |
---|
88 | std::basic_ostringstream<C> os; |
---|
89 | bits::insert (os, x); |
---|
90 | e << os.str (); |
---|
91 | } |
---|
92 | |
---|
93 | template <typename C, typename B> |
---|
94 | inline void |
---|
95 | operator<< (xercesc::DOMAttr& a, const gday<C, B>& x) |
---|
96 | { |
---|
97 | std::basic_ostringstream<C> os; |
---|
98 | bits::insert (os, x); |
---|
99 | a << os.str (); |
---|
100 | } |
---|
101 | |
---|
102 | template <typename C, typename B> |
---|
103 | inline void |
---|
104 | operator<< (list_stream<C>& ls, const gday<C, B>& x) |
---|
105 | { |
---|
106 | bits::insert (ls.os_, x); |
---|
107 | } |
---|
108 | |
---|
109 | // gmonth |
---|
110 | // |
---|
111 | namespace bits |
---|
112 | { |
---|
113 | template <typename C, typename B> |
---|
114 | void |
---|
115 | insert (std::basic_ostream<C>& os, const tree::gmonth<C, B>& x) |
---|
116 | { |
---|
117 | if (x.month () < 13) |
---|
118 | { |
---|
119 | os.fill (C ('0')); |
---|
120 | os << bits::gmonth_prefix<C> (); |
---|
121 | os.width (2); |
---|
122 | os << x.month (); |
---|
123 | |
---|
124 | if (x.zone_present ()) |
---|
125 | zone_insert (os, x); |
---|
126 | } |
---|
127 | } |
---|
128 | } |
---|
129 | |
---|
130 | template <typename C, typename B> |
---|
131 | inline void |
---|
132 | operator<< (xercesc::DOMElement& e, const gmonth<C, B>& x) |
---|
133 | { |
---|
134 | std::basic_ostringstream<C> os; |
---|
135 | bits::insert (os, x); |
---|
136 | e << os.str (); |
---|
137 | } |
---|
138 | |
---|
139 | template <typename C, typename B> |
---|
140 | inline void |
---|
141 | operator<< (xercesc::DOMAttr& a, const gmonth<C, B>& x) |
---|
142 | { |
---|
143 | std::basic_ostringstream<C> os; |
---|
144 | bits::insert (os, x); |
---|
145 | a << os.str (); |
---|
146 | } |
---|
147 | |
---|
148 | template <typename C, typename B> |
---|
149 | inline void |
---|
150 | operator<< (list_stream<C>& ls, const gmonth<C, B>& x) |
---|
151 | { |
---|
152 | bits::insert (ls.os_, x); |
---|
153 | } |
---|
154 | |
---|
155 | // gyear |
---|
156 | // |
---|
157 | namespace bits |
---|
158 | { |
---|
159 | template <typename C, typename B> |
---|
160 | void |
---|
161 | insert (std::basic_ostream<C>& os, const tree::gyear<C, B>& x) |
---|
162 | { |
---|
163 | os.fill (C ('0')); |
---|
164 | os.width (4); |
---|
165 | os << x.year (); |
---|
166 | |
---|
167 | if (x.zone_present ()) |
---|
168 | zone_insert (os, x); |
---|
169 | } |
---|
170 | } |
---|
171 | |
---|
172 | template <typename C, typename B> |
---|
173 | inline void |
---|
174 | operator<< (xercesc::DOMElement& e, const gyear<C, B>& x) |
---|
175 | { |
---|
176 | std::basic_ostringstream<C> os; |
---|
177 | bits::insert (os, x); |
---|
178 | e << os.str (); |
---|
179 | } |
---|
180 | |
---|
181 | template <typename C, typename B> |
---|
182 | inline void |
---|
183 | operator<< (xercesc::DOMAttr& a, const gyear<C, B>& x) |
---|
184 | { |
---|
185 | std::basic_ostringstream<C> os; |
---|
186 | bits::insert (os, x); |
---|
187 | a << os.str (); |
---|
188 | } |
---|
189 | |
---|
190 | template <typename C, typename B> |
---|
191 | inline void |
---|
192 | operator<< (list_stream<C>& ls, const gyear<C, B>& x) |
---|
193 | { |
---|
194 | bits::insert (ls.os_, x); |
---|
195 | } |
---|
196 | |
---|
197 | // gmonth_day |
---|
198 | // |
---|
199 | namespace bits |
---|
200 | { |
---|
201 | template <typename C, typename B> |
---|
202 | void |
---|
203 | insert (std::basic_ostream<C>& os, const tree::gmonth_day<C, B>& x) |
---|
204 | { |
---|
205 | if (x.month () < 13 && x.day () < 32) |
---|
206 | { |
---|
207 | os.fill (C ('0')); |
---|
208 | os << bits::gmonth_prefix<C> (); |
---|
209 | os.width (2); |
---|
210 | os << x.month () << C ('-'); |
---|
211 | os.width (2); |
---|
212 | os << x.day (); |
---|
213 | |
---|
214 | if (x.zone_present ()) |
---|
215 | zone_insert (os, x); |
---|
216 | } |
---|
217 | } |
---|
218 | } |
---|
219 | |
---|
220 | template <typename C, typename B> |
---|
221 | inline void |
---|
222 | operator<< (xercesc::DOMElement& e, const gmonth_day<C, B>& x) |
---|
223 | { |
---|
224 | std::basic_ostringstream<C> os; |
---|
225 | bits::insert (os, x); |
---|
226 | e << os.str (); |
---|
227 | } |
---|
228 | |
---|
229 | template <typename C, typename B> |
---|
230 | inline void |
---|
231 | operator<< (xercesc::DOMAttr& a, const gmonth_day<C, B>& x) |
---|
232 | { |
---|
233 | std::basic_ostringstream<C> os; |
---|
234 | bits::insert (os, x); |
---|
235 | a << os.str (); |
---|
236 | } |
---|
237 | |
---|
238 | template <typename C, typename B> |
---|
239 | inline void |
---|
240 | operator<< (list_stream<C>& ls, const gmonth_day<C, B>& x) |
---|
241 | { |
---|
242 | bits::insert (ls.os_, x); |
---|
243 | } |
---|
244 | |
---|
245 | // gyear_month |
---|
246 | // |
---|
247 | namespace bits |
---|
248 | { |
---|
249 | template <typename C, typename B> |
---|
250 | void |
---|
251 | insert (std::basic_ostream<C>& os, const tree::gyear_month<C, B>& x) |
---|
252 | { |
---|
253 | if (x.month () < 13) |
---|
254 | { |
---|
255 | os.fill (C ('0')); |
---|
256 | os.width (4); |
---|
257 | os << x.year () << C ('-'); |
---|
258 | os.width (2); |
---|
259 | os << x.month (); |
---|
260 | |
---|
261 | if (x.zone_present ()) |
---|
262 | zone_insert (os, x); |
---|
263 | } |
---|
264 | } |
---|
265 | } |
---|
266 | |
---|
267 | template <typename C, typename B> |
---|
268 | inline void |
---|
269 | operator<< (xercesc::DOMElement& e, const gyear_month<C, B>& x) |
---|
270 | { |
---|
271 | std::basic_ostringstream<C> os; |
---|
272 | bits::insert (os, x); |
---|
273 | e << os.str (); |
---|
274 | } |
---|
275 | |
---|
276 | template <typename C, typename B> |
---|
277 | inline void |
---|
278 | operator<< (xercesc::DOMAttr& a, const gyear_month<C, B>& x) |
---|
279 | { |
---|
280 | std::basic_ostringstream<C> os; |
---|
281 | bits::insert (os, x); |
---|
282 | a << os.str (); |
---|
283 | } |
---|
284 | |
---|
285 | template <typename C, typename B> |
---|
286 | inline void |
---|
287 | operator<< (list_stream<C>& ls, const gyear_month<C, B>& x) |
---|
288 | { |
---|
289 | bits::insert (ls.os_, x); |
---|
290 | } |
---|
291 | |
---|
292 | // date |
---|
293 | // |
---|
294 | namespace bits |
---|
295 | { |
---|
296 | template <typename C, typename B> |
---|
297 | void |
---|
298 | insert (std::basic_ostream<C>& os, const tree::date<C, B>& x) |
---|
299 | { |
---|
300 | if (x.month () < 13 && x.day () < 32) |
---|
301 | { |
---|
302 | os.fill (C ('0')); |
---|
303 | os.width (4); |
---|
304 | os << x.year () << C ('-'); |
---|
305 | os.width (2); |
---|
306 | os << x.month () << C ('-'); |
---|
307 | os.width (2); |
---|
308 | os << x.day (); |
---|
309 | |
---|
310 | if (x.zone_present ()) |
---|
311 | zone_insert (os, x); |
---|
312 | } |
---|
313 | } |
---|
314 | } |
---|
315 | |
---|
316 | template <typename C, typename B> |
---|
317 | inline void |
---|
318 | operator<< (xercesc::DOMElement& e, const date<C, B>& x) |
---|
319 | { |
---|
320 | std::basic_ostringstream<C> os; |
---|
321 | bits::insert (os, x); |
---|
322 | e << os.str (); |
---|
323 | } |
---|
324 | |
---|
325 | template <typename C, typename B> |
---|
326 | inline void |
---|
327 | operator<< (xercesc::DOMAttr& a, const date<C, B>& x) |
---|
328 | { |
---|
329 | std::basic_ostringstream<C> os; |
---|
330 | bits::insert (os, x); |
---|
331 | a << os.str (); |
---|
332 | } |
---|
333 | |
---|
334 | template <typename C, typename B> |
---|
335 | inline void |
---|
336 | operator<< (list_stream<C>& ls, const date<C, B>& x) |
---|
337 | { |
---|
338 | bits::insert (ls.os_, x); |
---|
339 | } |
---|
340 | |
---|
341 | // time |
---|
342 | // |
---|
343 | namespace bits |
---|
344 | { |
---|
345 | template <typename C, typename B> |
---|
346 | void |
---|
347 | insert (std::basic_ostream<C>& os, const tree::time<C, B>& x) |
---|
348 | { |
---|
349 | if (x.hours () <= 24 && |
---|
350 | x.minutes () <= 59 && |
---|
351 | x.seconds () >= 0.0 && |
---|
352 | x.seconds () < 60.0) |
---|
353 | { |
---|
354 | os.fill (C ('0')); |
---|
355 | os.width (2); |
---|
356 | os << x.hours () << C (':'); |
---|
357 | |
---|
358 | os.width (2); |
---|
359 | os << x.minutes () << C (':'); |
---|
360 | |
---|
361 | std::basic_ostringstream<C> ostr; |
---|
362 | ostr.imbue (std::locale::classic ()); |
---|
363 | ostr.width (9); |
---|
364 | ostr.fill (C ('0')); |
---|
365 | ostr << std::fixed << x.seconds (); |
---|
366 | |
---|
367 | std::basic_string<C> s (ostr.str ()); |
---|
368 | |
---|
369 | // Remove the trailing zeros and the decimal point if necessary. |
---|
370 | // |
---|
371 | typedef typename std::basic_string<C>::size_type size_type; |
---|
372 | |
---|
373 | size_type size (s.size ()), n (size); |
---|
374 | |
---|
375 | for (; n > 0 && s[n - 1] == C ('0'); --n); |
---|
376 | |
---|
377 | if (n > 0 && s[n - 1] == C ('.')) |
---|
378 | --n; |
---|
379 | |
---|
380 | if (n != size) |
---|
381 | s.resize (n); |
---|
382 | |
---|
383 | os << s; |
---|
384 | |
---|
385 | if (x.zone_present ()) |
---|
386 | zone_insert (os, x); |
---|
387 | } |
---|
388 | } |
---|
389 | } |
---|
390 | |
---|
391 | template <typename C, typename B> |
---|
392 | inline void |
---|
393 | operator<< (xercesc::DOMElement& e, const time<C, B>& x) |
---|
394 | { |
---|
395 | std::basic_ostringstream<C> os; |
---|
396 | bits::insert (os, x); |
---|
397 | e << os.str (); |
---|
398 | } |
---|
399 | |
---|
400 | template <typename C, typename B> |
---|
401 | inline void |
---|
402 | operator<< (xercesc::DOMAttr& a, const time<C, B>& x) |
---|
403 | { |
---|
404 | std::basic_ostringstream<C> os; |
---|
405 | bits::insert (os, x); |
---|
406 | a << os.str (); |
---|
407 | } |
---|
408 | |
---|
409 | template <typename C, typename B> |
---|
410 | inline void |
---|
411 | operator<< (list_stream<C>& ls, const time<C, B>& x) |
---|
412 | { |
---|
413 | bits::insert (ls.os_, x); |
---|
414 | } |
---|
415 | |
---|
416 | // date_time |
---|
417 | // |
---|
418 | namespace bits |
---|
419 | { |
---|
420 | template <typename C, typename B> |
---|
421 | void |
---|
422 | insert (std::basic_ostream<C>& os, const tree::date_time<C, B>& x) |
---|
423 | { |
---|
424 | if (x.month () <= 12 && |
---|
425 | x.day () <= 31 && |
---|
426 | x.hours () <= 24 && |
---|
427 | x.minutes () <= 59 && |
---|
428 | x.seconds () >= 0.0 && |
---|
429 | x.seconds () < 60.0) |
---|
430 | { |
---|
431 | os.fill (C ('0')); |
---|
432 | os.width (4); |
---|
433 | os << x.year () << C ('-'); |
---|
434 | |
---|
435 | os.width (2); |
---|
436 | os << x.month () << C ('-'); |
---|
437 | |
---|
438 | os.width (2); |
---|
439 | os << x.day () << C ('T'); |
---|
440 | |
---|
441 | os.width (2); |
---|
442 | os << x.hours () << C (':'); |
---|
443 | |
---|
444 | os.width (2); |
---|
445 | os << x.minutes () << C (':'); |
---|
446 | |
---|
447 | std::basic_ostringstream<C> ostr; |
---|
448 | ostr.imbue (std::locale::classic ()); |
---|
449 | ostr.width (9); |
---|
450 | ostr.fill (C ('0')); |
---|
451 | ostr << std::fixed << x.seconds (); |
---|
452 | |
---|
453 | std::basic_string<C> s (ostr.str ()); |
---|
454 | |
---|
455 | // Remove the trailing zeros and the decimal point if necessary. |
---|
456 | // |
---|
457 | typedef typename std::basic_string<C>::size_type size_type; |
---|
458 | |
---|
459 | size_type size (s.size ()), n (size); |
---|
460 | |
---|
461 | for (; n > 0 && s[n - 1] == C ('0'); --n); |
---|
462 | |
---|
463 | if (n > 0 && s[n - 1] == C ('.')) |
---|
464 | --n; |
---|
465 | |
---|
466 | if (n != size) |
---|
467 | s.resize (n); |
---|
468 | |
---|
469 | os << s; |
---|
470 | |
---|
471 | if (x.zone_present ()) |
---|
472 | zone_insert (os, x); |
---|
473 | } |
---|
474 | } |
---|
475 | } |
---|
476 | |
---|
477 | template <typename C, typename B> |
---|
478 | inline void |
---|
479 | operator<< (xercesc::DOMElement& e, const date_time<C, B>& x) |
---|
480 | { |
---|
481 | std::basic_ostringstream<C> os; |
---|
482 | bits::insert (os, x); |
---|
483 | e << os.str (); |
---|
484 | } |
---|
485 | |
---|
486 | template <typename C, typename B> |
---|
487 | inline void |
---|
488 | operator<< (xercesc::DOMAttr& a, const date_time<C, B>& x) |
---|
489 | { |
---|
490 | std::basic_ostringstream<C> os; |
---|
491 | bits::insert (os, x); |
---|
492 | a << os.str (); |
---|
493 | } |
---|
494 | |
---|
495 | template <typename C, typename B> |
---|
496 | inline void |
---|
497 | operator<< (list_stream<C>& ls, const date_time<C, B>& x) |
---|
498 | { |
---|
499 | bits::insert (ls.os_, x); |
---|
500 | } |
---|
501 | |
---|
502 | // duration |
---|
503 | // |
---|
504 | namespace bits |
---|
505 | { |
---|
506 | template <typename C, typename B> |
---|
507 | void |
---|
508 | insert (std::basic_ostream<C>& os, const tree::duration<C, B>& x) |
---|
509 | { |
---|
510 | if (x.negative ()) |
---|
511 | os << C ('-'); |
---|
512 | |
---|
513 | os << C ('P'); |
---|
514 | |
---|
515 | // years |
---|
516 | // |
---|
517 | // In case it is 0-duration, use the years field to handle |
---|
518 | // this case. |
---|
519 | // |
---|
520 | if (x.years () != 0 || |
---|
521 | (x.months () == 0 && |
---|
522 | x.days () == 0 && |
---|
523 | x.hours () == 0 && |
---|
524 | x.minutes () == 0 && |
---|
525 | x.seconds () == 0.0)) |
---|
526 | { |
---|
527 | os << x.years () << C ('Y'); |
---|
528 | } |
---|
529 | |
---|
530 | // months |
---|
531 | // |
---|
532 | if (x.months () != 0) |
---|
533 | { |
---|
534 | os << x.months () << C ('M'); |
---|
535 | } |
---|
536 | |
---|
537 | // days |
---|
538 | // |
---|
539 | if (x.days () != 0) |
---|
540 | { |
---|
541 | os << x.days () << C ('D'); |
---|
542 | } |
---|
543 | |
---|
544 | // Figure out if we need the 'T' delimiter. |
---|
545 | // |
---|
546 | if (x.hours () != 0 || |
---|
547 | x.minutes () != 0 || |
---|
548 | x.seconds () != 0.0) |
---|
549 | os << C ('T'); |
---|
550 | |
---|
551 | // hours |
---|
552 | // |
---|
553 | if (x.hours () != 0) |
---|
554 | { |
---|
555 | os << x.hours () << C ('H'); |
---|
556 | } |
---|
557 | |
---|
558 | // minutes |
---|
559 | // |
---|
560 | if (x.minutes () != 0) |
---|
561 | { |
---|
562 | os << x.minutes () << C ('M'); |
---|
563 | } |
---|
564 | |
---|
565 | // seconds |
---|
566 | // |
---|
567 | if (x.seconds () > 0.0) |
---|
568 | { |
---|
569 | std::basic_ostringstream<C> ostr; |
---|
570 | ostr.imbue (std::locale::classic ()); |
---|
571 | ostr << std::fixed << x.seconds (); |
---|
572 | |
---|
573 | std::basic_string<C> s (ostr.str ()); |
---|
574 | |
---|
575 | // Remove the trailing zeros and the decimal point if necessary. |
---|
576 | // |
---|
577 | typedef typename std::basic_string<C>::size_type size_type; |
---|
578 | |
---|
579 | size_type size (s.size ()), n (size); |
---|
580 | |
---|
581 | for (; n > 0 && s[n - 1] == C ('0'); --n); |
---|
582 | |
---|
583 | if (n > 0 && s[n - 1] == C ('.')) |
---|
584 | --n; |
---|
585 | |
---|
586 | if (n != size) |
---|
587 | s.resize (n); |
---|
588 | |
---|
589 | os << s << C ('S'); |
---|
590 | } |
---|
591 | } |
---|
592 | } |
---|
593 | |
---|
594 | template <typename C, typename B> |
---|
595 | inline void |
---|
596 | operator<< (xercesc::DOMElement& e, const duration<C, B>& x) |
---|
597 | { |
---|
598 | std::basic_ostringstream<C> os; |
---|
599 | bits::insert (os, x); |
---|
600 | e << os.str (); |
---|
601 | } |
---|
602 | |
---|
603 | template <typename C, typename B> |
---|
604 | inline void |
---|
605 | operator<< (xercesc::DOMAttr& a, const duration<C, B>& x) |
---|
606 | { |
---|
607 | std::basic_ostringstream<C> os; |
---|
608 | bits::insert (os, x); |
---|
609 | a << os.str (); |
---|
610 | } |
---|
611 | |
---|
612 | template <typename C, typename B> |
---|
613 | inline void |
---|
614 | operator<< (list_stream<C>& ls, const duration<C, B>& x) |
---|
615 | { |
---|
616 | bits::insert (ls.os_, x); |
---|
617 | } |
---|
618 | } |
---|
619 | } |
---|
620 | } |
---|