Boost.Locale
formatting.hpp
1//
2// Copyright (c) 2009-2011 Artyom Beilis (Tonkikh)
3// Copyright (c) 2022-2023 Alexander Grund
4//
5// Distributed under the Boost Software License, Version 1.0.
6// https://www.boost.org/LICENSE_1_0.txt
7
8#ifndef BOOST_LOCALE_FORMATTING_HPP_INCLUDED
9#define BOOST_LOCALE_FORMATTING_HPP_INCLUDED
10
11#include <boost/locale/detail/any_string.hpp>
12#include <boost/locale/time_zone.hpp>
13#include <cstdint>
14#include <cstring>
15#include <istream>
16#include <ostream>
17#include <string>
18
19#ifdef BOOST_MSVC
20# pragma warning(push)
21# pragma warning(disable : 4275 4251 4231 4660)
22#endif
23
24namespace boost { namespace locale {
25
28 namespace flags {
29
33 posix = 0,
34 number = 1,
35 currency = 2,
36 percent = 3,
37 date = 4,
38 time = 5,
39 datetime = 6,
40 strftime = 7,
41 spellout = 8,
42 ordinal = 9,
43
44 display_flags_mask = 31,
45
46 currency_default = 0 << 5,
47 currency_iso = 1 << 5,
48 currency_national = 2 << 5,
49
50 currency_flags_mask = 3 << 5,
51
52 time_default = 0 << 7,
53 time_short = 1 << 7,
54 time_medium = 2 << 7,
55 time_long = 3 << 7,
56 time_full = 4 << 7,
57 time_flags_mask = 7 << 7,
58
59 date_default = 0 << 10,
60 date_short = 1 << 10,
61 date_medium = 2 << 10,
62 date_long = 3 << 10,
63 date_full = 4 << 10,
64 date_flags_mask = 7 << 10,
65 };
66
71 };
72
76 };
77
78 } // namespace flags
79
85 class BOOST_LOCALE_DECL ios_info {
86 public:
88 ios_info();
89 ios_info(const ios_info&);
90 ios_info& operator=(const ios_info&);
91 ~ios_info();
93
95 static ios_info& get(std::ios_base& ios);
96
98 void display_flags(uint64_t flags);
100 uint64_t display_flags() const;
101
103 void currency_flags(uint64_t flags);
105 uint64_t currency_flags() const;
106
108 void date_flags(uint64_t flags);
110 uint64_t date_flags() const;
111
113 void time_flags(uint64_t flags);
115 uint64_t time_flags() const;
116
118 void domain_id(int);
120 int domain_id() const;
121
123 void time_zone(const std::string&);
125 std::string time_zone() const;
126
128 template<typename CharType>
129 void date_time_pattern(const std::basic_string<CharType>& str)
130 {
131 datetime_.set<CharType>(str);
132 }
134 template<typename CharType>
135 std::basic_string<CharType> date_time_pattern() const
136 {
137 return datetime_.get<CharType>();
138 }
139
141 void on_imbue();
143
144 private:
145 uint64_t flags_;
146 int domain_id_;
147 std::string time_zone_;
148 detail::any_string datetime_;
149 };
150
152 namespace as {
156
159 inline std::ios_base& posix(std::ios_base& ios)
160 {
161 ios_info::get(ios).display_flags(flags::posix);
162 return ios;
163 }
164
167 inline std::ios_base& number(std::ios_base& ios)
168 {
169 ios_info::get(ios).display_flags(flags::number);
170 return ios;
171 }
172
174 inline std::ios_base& currency(std::ios_base& ios)
175 {
176 ios_info::get(ios).display_flags(flags::currency);
177 return ios;
178 }
179
181 inline std::ios_base& percent(std::ios_base& ios)
182 {
183 ios_info::get(ios).display_flags(flags::percent);
184 return ios;
185 }
186
188 inline std::ios_base& date(std::ios_base& ios)
189 {
190 ios_info::get(ios).display_flags(flags::date);
191 return ios;
192 }
193
195 inline std::ios_base& time(std::ios_base& ios)
196 {
197 ios_info::get(ios).display_flags(flags::time);
198 return ios;
199 }
200
202 inline std::ios_base& datetime(std::ios_base& ios)
203 {
204 ios_info::get(ios).display_flags(flags::datetime);
205 return ios;
206 }
207
210 inline std::ios_base& strftime(std::ios_base& ios)
211 {
212 ios_info::get(ios).display_flags(flags::strftime);
213 return ios;
214 }
215
217 inline std::ios_base& spellout(std::ios_base& ios)
218 {
219 ios_info::get(ios).display_flags(flags::spellout);
220 return ios;
221 }
222
224 inline std::ios_base& ordinal(std::ios_base& ios)
225 {
226 ios_info::get(ios).display_flags(flags::ordinal);
227 return ios;
228 }
229
231 inline std::ios_base& currency_default(std::ios_base& ios)
232 {
233 ios_info::get(ios).currency_flags(flags::currency_default);
234 return ios;
235 }
236
238 inline std::ios_base& currency_iso(std::ios_base& ios)
239 {
240 ios_info::get(ios).currency_flags(flags::currency_iso);
241 return ios;
242 }
243
245 inline std::ios_base& currency_national(std::ios_base& ios)
246 {
247 ios_info::get(ios).currency_flags(flags::currency_national);
248 return ios;
249 }
250
252 inline std::ios_base& time_default(std::ios_base& ios)
253 {
254 ios_info::get(ios).time_flags(flags::time_default);
255 return ios;
256 }
257
259 inline std::ios_base& time_short(std::ios_base& ios)
260 {
261 ios_info::get(ios).time_flags(flags::time_short);
262 return ios;
263 }
264
266 inline std::ios_base& time_medium(std::ios_base& ios)
267 {
268 ios_info::get(ios).time_flags(flags::time_medium);
269 return ios;
270 }
271
273 inline std::ios_base& time_long(std::ios_base& ios)
274 {
275 ios_info::get(ios).time_flags(flags::time_long);
276 return ios;
277 }
278
280 inline std::ios_base& time_full(std::ios_base& ios)
281 {
282 ios_info::get(ios).time_flags(flags::time_full);
283 return ios;
284 }
285
287 inline std::ios_base& date_default(std::ios_base& ios)
288 {
289 ios_info::get(ios).date_flags(flags::date_default);
290 return ios;
291 }
292
294 inline std::ios_base& date_short(std::ios_base& ios)
295 {
296 ios_info::get(ios).date_flags(flags::date_short);
297 return ios;
298 }
299
301 inline std::ios_base& date_medium(std::ios_base& ios)
302 {
303 ios_info::get(ios).date_flags(flags::date_medium);
304 return ios;
305 }
306
308 inline std::ios_base& date_long(std::ios_base& ios)
309 {
310 ios_info::get(ios).date_flags(flags::date_long);
311 return ios;
312 }
313
315 inline std::ios_base& date_full(std::ios_base& ios)
316 {
317 ios_info::get(ios).date_flags(flags::date_full);
318 return ios;
319 }
320
322 namespace detail {
323 inline bool is_datetime_display_flags(const uint64_t display_flags)
324 {
325 return (display_flags == flags::date || display_flags == flags::time || display_flags == flags::datetime
326 || display_flags == flags::strftime);
327 }
328
329 template<typename CharType>
330 struct add_ftime {
331 std::basic_string<CharType> ftime;
332
333 void apply(std::basic_ios<CharType>& ios) const
334 {
335 ios_info::get(ios).date_time_pattern(ftime);
336 as::strftime(ios);
337 }
338 };
339
340 template<typename CharType>
341 std::basic_ostream<CharType>& operator<<(std::basic_ostream<CharType>& out, const add_ftime<CharType>& fmt)
342 {
343 fmt.apply(out);
344 return out;
345 }
346
347 template<typename CharType>
348 std::basic_istream<CharType>& operator>>(std::basic_istream<CharType>& in, const add_ftime<CharType>& fmt)
349 {
350 fmt.apply(in);
351 return in;
352 }
353
354 } // namespace detail
356
390
391 template<typename CharType>
392#ifdef BOOST_LOCALE_DOXYGEN
393 unspecified_type
394#else
395 detail::add_ftime<CharType>
396#endif
397 ftime(const std::basic_string<CharType>& format)
398 {
399 detail::add_ftime<CharType> fmt;
400 fmt.ftime = format;
401 return fmt;
402 }
403
405 template<typename CharType>
406#ifdef BOOST_LOCALE_DOXYGEN
407 unspecified_type
408#else
409 detail::add_ftime<CharType>
410#endif
411 ftime(const CharType* format)
412 {
413 detail::add_ftime<CharType> fmt;
414 fmt.ftime = format;
415 return fmt;
416 }
417
419 namespace detail {
420 struct set_timezone {
421 std::string id;
422 };
423 template<typename CharType>
424 std::basic_ostream<CharType>& operator<<(std::basic_ostream<CharType>& out, const set_timezone& fmt)
425 {
426 ios_info::get(out).time_zone(fmt.id);
427 return out;
428 }
429
430 template<typename CharType>
431 std::basic_istream<CharType>& operator>>(std::basic_istream<CharType>& in, const set_timezone& fmt)
432 {
433 ios_info::get(in).time_zone(fmt.id);
434 return in;
435 }
436 } // namespace detail
438
440 inline std::ios_base& gmt(std::ios_base& ios)
441 {
442 ios_info::get(ios).time_zone("GMT");
443 return ios;
444 }
445
447 inline std::ios_base& local_time(std::ios_base& ios)
448 {
450 return ios;
451 }
452
454 inline
455#ifdef BOOST_LOCALE_DOXYGEN
456 unspecified_type
457#else
458 detail::set_timezone
459#endif
460 time_zone(const char* id)
461 {
462 detail::set_timezone tz;
463 tz.id = id;
464 return tz;
465 }
466
468 inline
469#ifdef BOOST_LOCALE_DOXYGEN
470 unspecified_type
471#else
472 detail::set_timezone
473#endif
474 time_zone(const std::string& id)
475 {
476 detail::set_timezone tz;
477 tz.id = id;
478 return tz;
479 }
480
482
483 } // namespace as
484
485}} // namespace boost::locale
486
487#ifdef BOOST_MSVC
488# pragma warning(pop)
489#endif
490
491#endif
a printf like class that allows type-safe and locale aware message formatting
Definition: format.hpp:181
This class holds external data beyond existing fmtflags that std::ios_base holds.
Definition: formatting.hpp:85
void time_flags(uint64_t flags)
Set flags that define how to format time.
void domain_id(int)
Set special message domain identification.
void display_flags(uint64_t flags)
Set flags that define how to format data, e.g. number, spell, currency etc.
static ios_info & get(std::ios_base &ios)
Get ios_info instance for specific stream object.
void time_zone(const std::string &)
Set time zone for formatting dates and time.
uint64_t display_flags() const
Get flags that define how to format data, e.g. number, spell, currency etc.
int domain_id() const
Get special message domain identification.
uint64_t time_flags() const
Get flags that define how to format time.
uint64_t currency_flags() const
Get flags that define how to format currency.
std::string time_zone() const
Get time zone for formatting dates and time.
void date_time_pattern(const std::basic_string< CharType > &str)
Set date/time pattern (strftime like)
Definition: formatting.hpp:129
std::basic_string< CharType > date_time_pattern() const
Get date/time pattern (strftime like)
Definition: formatting.hpp:135
uint64_t date_flags() const
Get flags that define how to format date.
void currency_flags(uint64_t flags)
Set flags that define how to format currency.
void date_flags(uint64_t flags)
Set flags that define how to format date.
std::basic_ostream< CharType, TraitsType > & operator<<(std::basic_ostream< CharType, TraitsType > &out, const segment< Iterator > &seg)
Write the segment to the stream character by character.
Definition: segment.hpp:367
std::basic_istream< CharType > & operator>>(std::basic_istream< CharType > &in, date_time &t)
Definition: date_time.hpp:743
basic_format< char > format
Definition of char based format.
Definition: format.hpp:418
std::ios_base & posix(std::ios_base &ios)
Definition: formatting.hpp:159
std::ios_base & date_full(std::ios_base &ios)
set full date formatting style
Definition: formatting.hpp:315
unspecified_type ftime(const std::basic_string< CharType > &format)
Definition: formatting.hpp:397
std::ios_base & time_short(std::ios_base &ios)
set short time formatting style
Definition: formatting.hpp:259
std::ios_base & date_default(std::ios_base &ios)
set default (medium) date formatting style
Definition: formatting.hpp:287
std::ios_base & date_medium(std::ios_base &ios)
set medium date formatting style
Definition: formatting.hpp:301
std::ios_base & strftime(std::ios_base &ios)
Definition: formatting.hpp:210
std::ios_base & currency_national(std::ios_base &ios)
Set national currency formatting style, like "$".
Definition: formatting.hpp:245
std::ios_base & currency_iso(std::ios_base &ios)
Set ISO currency formatting style, like "USD", (requires ICU >= 4.2)
Definition: formatting.hpp:238
std::ios_base & currency_default(std::ios_base &ios)
Set default currency formatting style – national, like "$".
Definition: formatting.hpp:231
unspecified_type time_zone(const char *id)
Set time zone using id.
Definition: formatting.hpp:460
std::ios_base & date_long(std::ios_base &ios)
set long date formatting style
Definition: formatting.hpp:308
std::ios_base & datetime(std::ios_base &ios)
Format a date and time, number is treated as POSIX time.
Definition: formatting.hpp:202
std::ios_base & time_default(std::ios_base &ios)
set default (medium) time formatting style
Definition: formatting.hpp:252
std::ios_base & currency(std::ios_base &ios)
Format currency, number is treated like amount of money.
Definition: formatting.hpp:174
std::ios_base & time_long(std::ios_base &ios)
set long time formatting style
Definition: formatting.hpp:273
std::ios_base & number(std::ios_base &ios)
Definition: formatting.hpp:167
std::ios_base & local_time(std::ios_base &ios)
Set local time zone to stream.
Definition: formatting.hpp:447
std::ios_base & percent(std::ios_base &ios)
Format percent, value 0.3 is treated as 30%.
Definition: formatting.hpp:181
std::ios_base & spellout(std::ios_base &ios)
Spell the number, like "one hundred and ten".
Definition: formatting.hpp:217
std::ios_base & time(std::ios_base &ios)
Format a time, number is treated as POSIX time.
Definition: formatting.hpp:195
std::ios_base & date_short(std::ios_base &ios)
set short date formatting style
Definition: formatting.hpp:294
std::ios_base & ordinal(std::ios_base &ios)
Write an order of the number like 4th.
Definition: formatting.hpp:224
std::ios_base & time_full(std::ios_base &ios)
set full time formatting style
Definition: formatting.hpp:280
std::ios_base & time_medium(std::ios_base &ios)
set medium time formatting style
Definition: formatting.hpp:266
std::ios_base & date(std::ios_base &ios)
Format a date, number is treated as POSIX time.
Definition: formatting.hpp:188
std::ios_base & gmt(std::ios_base &ios)
Set GMT time zone to stream.
Definition: formatting.hpp:440
pattern_type
Special string patterns that can be used for text formatting.
Definition: formatting.hpp:68
@ datetime_pattern
strftime like formatting
Definition: formatting.hpp:69
@ time_zone_id
time zone name
Definition: formatting.hpp:70
value_type
Special integer values that can be used for formatting.
Definition: formatting.hpp:74
@ domain_id
Domain code - for message formatting.
Definition: formatting.hpp:75
display_flags_type
Definition: formatting.hpp:32
std::string global()
Get global time zone identifier. If empty, system time zone is used.