1 | // file : xsd/cxx/tree/date-time-ostream.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 <ostream> |
---|
7 | |
---|
8 | namespace xsd |
---|
9 | { |
---|
10 | namespace cxx |
---|
11 | { |
---|
12 | namespace tree |
---|
13 | { |
---|
14 | // time_zone |
---|
15 | // |
---|
16 | template <typename C> |
---|
17 | std::basic_ostream<C>& |
---|
18 | operator<< (std::basic_ostream<C>& os, const time_zone& z) |
---|
19 | { |
---|
20 | short h = z.zone_hours (); |
---|
21 | short m = z.zone_minutes (); |
---|
22 | |
---|
23 | if (h == 0 && m == 0) |
---|
24 | { |
---|
25 | os << C ('Z'); |
---|
26 | } |
---|
27 | else |
---|
28 | { |
---|
29 | if (h < 0 || m < 0) |
---|
30 | { |
---|
31 | h = -h; |
---|
32 | m = -m; |
---|
33 | os << C ('-'); |
---|
34 | } |
---|
35 | else |
---|
36 | os << C ('+'); |
---|
37 | |
---|
38 | C f (os.fill (C ('0'))); |
---|
39 | |
---|
40 | os.width (2); |
---|
41 | os << h << C (':'); |
---|
42 | os.width (2); |
---|
43 | os << m; |
---|
44 | |
---|
45 | os.fill (f); |
---|
46 | } |
---|
47 | |
---|
48 | return os; |
---|
49 | } |
---|
50 | |
---|
51 | // gday |
---|
52 | // |
---|
53 | template <typename C, typename B> |
---|
54 | std::basic_ostream<C>& |
---|
55 | operator<< (std::basic_ostream<C>& os, const gday<C, B>& x) |
---|
56 | { |
---|
57 | C f (os.fill (C ('0'))); |
---|
58 | os.width (2); |
---|
59 | os << x.day (); |
---|
60 | os.fill (f); |
---|
61 | |
---|
62 | if (x.zone_present ()) |
---|
63 | { |
---|
64 | const time_zone& z (x); |
---|
65 | os << z; |
---|
66 | } |
---|
67 | |
---|
68 | return os; |
---|
69 | } |
---|
70 | |
---|
71 | // gmonth |
---|
72 | // |
---|
73 | template <typename C, typename B> |
---|
74 | std::basic_ostream<C>& |
---|
75 | operator<< (std::basic_ostream<C>& os, const gmonth<C, B>& x) |
---|
76 | { |
---|
77 | C f (os.fill (C ('0'))); |
---|
78 | os.width (2); |
---|
79 | os << x.month (); |
---|
80 | os.fill (f); |
---|
81 | |
---|
82 | if (x.zone_present ()) |
---|
83 | { |
---|
84 | const time_zone& z (x); |
---|
85 | os << z; |
---|
86 | } |
---|
87 | |
---|
88 | return os; |
---|
89 | } |
---|
90 | |
---|
91 | // gyear |
---|
92 | // |
---|
93 | template <typename C, typename B> |
---|
94 | std::basic_ostream<C>& |
---|
95 | operator<< (std::basic_ostream<C>& os, const gyear<C, B>& x) |
---|
96 | { |
---|
97 | C f (os.fill (C ('0'))); |
---|
98 | os.width (4); |
---|
99 | os << x.year (); |
---|
100 | os.fill (f); |
---|
101 | |
---|
102 | if (x.zone_present ()) |
---|
103 | { |
---|
104 | const time_zone& z (x); |
---|
105 | os << z; |
---|
106 | } |
---|
107 | |
---|
108 | return os; |
---|
109 | } |
---|
110 | |
---|
111 | // gmonth_day |
---|
112 | // |
---|
113 | template <typename C, typename B> |
---|
114 | std::basic_ostream<C>& |
---|
115 | operator<< (std::basic_ostream<C>& os, const gmonth_day<C, B>& x) |
---|
116 | { |
---|
117 | C f (os.fill (C ('0'))); |
---|
118 | |
---|
119 | os.width (2); |
---|
120 | os << x.month () << C ('-'); |
---|
121 | |
---|
122 | os.width (2); |
---|
123 | os << x.day (); |
---|
124 | |
---|
125 | os.fill (f); |
---|
126 | |
---|
127 | if (x.zone_present ()) |
---|
128 | { |
---|
129 | const time_zone& z (x); |
---|
130 | os << z; |
---|
131 | } |
---|
132 | |
---|
133 | return os; |
---|
134 | } |
---|
135 | |
---|
136 | |
---|
137 | // gyear_month |
---|
138 | // |
---|
139 | template <typename C, typename B> |
---|
140 | std::basic_ostream<C>& |
---|
141 | operator<< (std::basic_ostream<C>& os, const gyear_month<C, B>& x) |
---|
142 | { |
---|
143 | C f (os.fill (C ('0'))); |
---|
144 | |
---|
145 | os.width (4); |
---|
146 | os << x.year () << C ('-'); |
---|
147 | |
---|
148 | os.width (2); |
---|
149 | os << x.month (); |
---|
150 | |
---|
151 | os.fill (f); |
---|
152 | |
---|
153 | if (x.zone_present ()) |
---|
154 | { |
---|
155 | const time_zone& z (x); |
---|
156 | os << z; |
---|
157 | } |
---|
158 | |
---|
159 | return os; |
---|
160 | } |
---|
161 | |
---|
162 | // date |
---|
163 | // |
---|
164 | template <typename C, typename B> |
---|
165 | std::basic_ostream<C>& |
---|
166 | operator<< (std::basic_ostream<C>& os, const date<C, B>& x) |
---|
167 | { |
---|
168 | C f (os.fill (C ('0'))); |
---|
169 | |
---|
170 | os.width (4); |
---|
171 | os << x.year () << C ('-'); |
---|
172 | |
---|
173 | os.width (2); |
---|
174 | os << x.month () << C ('-'); |
---|
175 | |
---|
176 | os.width (2); |
---|
177 | os << x.day (); |
---|
178 | |
---|
179 | os.fill (f); |
---|
180 | |
---|
181 | if (x.zone_present ()) |
---|
182 | { |
---|
183 | const time_zone& z (x); |
---|
184 | os << z; |
---|
185 | } |
---|
186 | |
---|
187 | return os; |
---|
188 | } |
---|
189 | |
---|
190 | // time |
---|
191 | // |
---|
192 | template <typename C, typename B> |
---|
193 | std::basic_ostream<C>& |
---|
194 | operator<< (std::basic_ostream<C>& os, const time<C, B>& x) |
---|
195 | { |
---|
196 | C f (os.fill (C ('0'))); |
---|
197 | |
---|
198 | os.width (2); |
---|
199 | os << x.hours () << C (':'); |
---|
200 | |
---|
201 | os.width (2); |
---|
202 | os << x.minutes () << C (':'); |
---|
203 | |
---|
204 | os.width (9); |
---|
205 | os << std::fixed << x.seconds (); |
---|
206 | |
---|
207 | os.fill (f); |
---|
208 | |
---|
209 | if (x.zone_present ()) |
---|
210 | { |
---|
211 | const time_zone& z (x); |
---|
212 | os << z; |
---|
213 | } |
---|
214 | |
---|
215 | return os; |
---|
216 | } |
---|
217 | |
---|
218 | // date_time |
---|
219 | // |
---|
220 | template <typename C, typename B> |
---|
221 | std::basic_ostream<C>& |
---|
222 | operator<< (std::basic_ostream<C>& os, const date_time<C, B>& x) |
---|
223 | { |
---|
224 | C f (os.fill (C ('0'))); |
---|
225 | |
---|
226 | os.width (4); |
---|
227 | os << x.year () << C ('-'); |
---|
228 | |
---|
229 | os.width (2); |
---|
230 | os << x.month () << C ('-'); |
---|
231 | |
---|
232 | os.width (2); |
---|
233 | os << x.day () << C ('T'); |
---|
234 | |
---|
235 | os.width (2); |
---|
236 | os << x.hours () << C (':'); |
---|
237 | |
---|
238 | os.width (2); |
---|
239 | os << x.minutes () << C (':'); |
---|
240 | |
---|
241 | os.width (9); |
---|
242 | os << std::fixed << x.seconds (); |
---|
243 | |
---|
244 | os.fill (f); |
---|
245 | |
---|
246 | if (x.zone_present ()) |
---|
247 | { |
---|
248 | const time_zone& z (x); |
---|
249 | os << z; |
---|
250 | } |
---|
251 | |
---|
252 | return os; |
---|
253 | } |
---|
254 | |
---|
255 | // duration |
---|
256 | // |
---|
257 | template <typename C, typename B> |
---|
258 | std::basic_ostream<C>& |
---|
259 | operator<< (std::basic_ostream<C>& os, const duration<C, B>& x) |
---|
260 | { |
---|
261 | if (x.negative ()) |
---|
262 | os << C ('-'); |
---|
263 | |
---|
264 | os << C ('P'); |
---|
265 | |
---|
266 | // In case it is 0-duration, use the years field to handle |
---|
267 | // this case. |
---|
268 | // |
---|
269 | if (x.years () != 0 || |
---|
270 | (x.months () == 0 && |
---|
271 | x.days () == 0 && |
---|
272 | x.hours () == 0 && |
---|
273 | x.minutes () == 0 && |
---|
274 | x.seconds () == 0.0)) |
---|
275 | { |
---|
276 | os << x.years () << C ('Y'); |
---|
277 | } |
---|
278 | |
---|
279 | if (x.months () != 0) |
---|
280 | { |
---|
281 | os << x.months () << C ('M'); |
---|
282 | } |
---|
283 | |
---|
284 | if (x.days () != 0) |
---|
285 | { |
---|
286 | os << x.days () << C ('D'); |
---|
287 | } |
---|
288 | |
---|
289 | // Figure out if we need the 'T' delimiter. |
---|
290 | // |
---|
291 | if (x.hours () != 0 || |
---|
292 | x.minutes () != 0 || |
---|
293 | x.seconds () != 0.0) |
---|
294 | os << C ('T'); |
---|
295 | |
---|
296 | if (x.hours () != 0) |
---|
297 | { |
---|
298 | os << x.hours () << C ('H'); |
---|
299 | } |
---|
300 | |
---|
301 | if (x.minutes () != 0) |
---|
302 | { |
---|
303 | os << x.minutes () << C ('M'); |
---|
304 | } |
---|
305 | |
---|
306 | if (x.seconds () > 0.0) |
---|
307 | { |
---|
308 | os << std::fixed << x.seconds () << C ('S'); |
---|
309 | } |
---|
310 | |
---|
311 | return os; |
---|
312 | } |
---|
313 | } |
---|
314 | } |
---|
315 | } |
---|