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/time_zone.hpp>
12#include <boost/assert.hpp>
13#include <boost/utility/string_view.hpp>
14#include <cstdint>
15#include <cstring>
16#include <istream>
17#include <ostream>
18#include <string>
19#include <typeinfo>
20
21#ifdef BOOST_MSVC
22# pragma warning(push)
23# pragma warning(disable : 4275 4251 4231 4660)
24#endif
25
26namespace boost { namespace locale {
27
30 namespace flags {
31
35 posix = 0,
36 number = 1,
37 currency = 2,
38 percent = 3,
39 date = 4,
40 time = 5,
41 datetime = 6,
42 strftime = 7,
43 spellout = 8,
44 ordinal = 9,
45
46 display_flags_mask = 31,
47
48 currency_default = 0 << 5,
49 currency_iso = 1 << 5,
50 currency_national = 2 << 5,
51
52 currency_flags_mask = 3 << 5,
53
54 time_default = 0 << 7,
55 time_short = 1 << 7,
56 time_medium = 2 << 7,
57 time_long = 3 << 7,
58 time_full = 4 << 7,
59 time_flags_mask = 7 << 7,
60
61 date_default = 0 << 10,
62 date_short = 1 << 10,
63 date_medium = 2 << 10,
64 date_long = 3 << 10,
65 date_full = 4 << 10,
66 date_flags_mask = 7 << 10,
67 };
68
73 };
74
78 };
79
80 } // namespace flags
81
87 class BOOST_LOCALE_DECL ios_info {
88 public:
90 ios_info();
91 ios_info(const ios_info&);
92 ios_info& operator=(const ios_info&);
93 ~ios_info();
95
97 static ios_info& get(std::ios_base& ios);
98
100 void display_flags(uint64_t flags);
102 uint64_t display_flags() const;
103
105 void currency_flags(uint64_t flags);
107 uint64_t currency_flags() const;
108
110 void date_flags(uint64_t flags);
112 uint64_t date_flags() const;
113
115 void time_flags(uint64_t flags);
117 uint64_t time_flags() const;
118
120 void domain_id(int);
122 int domain_id() const;
123
125 void time_zone(const std::string&);
127 std::string time_zone() const;
128
130 template<typename CharType>
131 void date_time_pattern(const std::basic_string<CharType>& str)
132 {
133 date_time_pattern_set().set<CharType>(str);
134 }
136 template<typename CharType>
137 std::basic_string<CharType> date_time_pattern() const
138 {
139 return date_time_pattern_set().get<CharType>();
140 }
141
143 void on_imbue();
145
146 private:
147 class string_set;
148
149 const string_set& date_time_pattern_set() const;
150 string_set& date_time_pattern_set();
151
152 class BOOST_LOCALE_DECL string_set {
153 public:
154 string_set();
155 ~string_set();
156 string_set(const string_set& other);
157 string_set& operator=(string_set other);
158 void swap(string_set& other);
159
160 template<typename Char>
161 void set(const boost::basic_string_view<Char> s)
162 {
163 BOOST_ASSERT(!s.empty());
164 delete[] ptr;
165 ptr = nullptr;
166 type = &typeid(Char);
167 size = sizeof(Char) * s.size();
168 ptr = size ? new char[size] : nullptr;
169 memcpy(ptr, s.data(), size);
170 }
171
172 template<typename Char>
173 std::basic_string<Char> get() const
174 {
175 if(type == nullptr || *type != typeid(Char))
176 throw std::bad_cast();
177 std::basic_string<Char> result(size / sizeof(Char), Char(0));
178 memcpy(&result.front(), ptr, size);
179 return result;
180 }
181
182 private:
183 const std::type_info* type;
184 size_t size;
185 char* ptr;
186 };
187
188 uint64_t flags_;
189 int domain_id_;
190 std::string time_zone_;
191 string_set datetime_;
192 };
193
195 namespace as {
199
202 inline std::ios_base& posix(std::ios_base& ios)
203 {
204 ios_info::get(ios).display_flags(flags::posix);
205 return ios;
206 }
207
210 inline std::ios_base& number(std::ios_base& ios)
211 {
212 ios_info::get(ios).display_flags(flags::number);
213 return ios;
214 }
215
217 inline std::ios_base& currency(std::ios_base& ios)
218 {
219 ios_info::get(ios).display_flags(flags::currency);
220 return ios;
221 }
222
224 inline std::ios_base& percent(std::ios_base& ios)
225 {
226 ios_info::get(ios).display_flags(flags::percent);
227 return ios;
228 }
229
231 inline std::ios_base& date(std::ios_base& ios)
232 {
233 ios_info::get(ios).display_flags(flags::date);
234 return ios;
235 }
236
238 inline std::ios_base& time(std::ios_base& ios)
239 {
240 ios_info::get(ios).display_flags(flags::time);
241 return ios;
242 }
243
245 inline std::ios_base& datetime(std::ios_base& ios)
246 {
247 ios_info::get(ios).display_flags(flags::datetime);
248 return ios;
249 }
250
253 inline std::ios_base& strftime(std::ios_base& ios)
254 {
255 ios_info::get(ios).display_flags(flags::strftime);
256 return ios;
257 }
258
260 inline std::ios_base& spellout(std::ios_base& ios)
261 {
262 ios_info::get(ios).display_flags(flags::spellout);
263 return ios;
264 }
265
267 inline std::ios_base& ordinal(std::ios_base& ios)
268 {
269 ios_info::get(ios).display_flags(flags::ordinal);
270 return ios;
271 }
272
274 inline std::ios_base& currency_default(std::ios_base& ios)
275 {
276 ios_info::get(ios).currency_flags(flags::currency_default);
277 return ios;
278 }
279
281 inline std::ios_base& currency_iso(std::ios_base& ios)
282 {
283 ios_info::get(ios).currency_flags(flags::currency_iso);
284 return ios;
285 }
286
288 inline std::ios_base& currency_national(std::ios_base& ios)
289 {
290 ios_info::get(ios).currency_flags(flags::currency_national);
291 return ios;
292 }
293
295 inline std::ios_base& time_default(std::ios_base& ios)
296 {
297 ios_info::get(ios).time_flags(flags::time_default);
298 return ios;
299 }
300
302 inline std::ios_base& time_short(std::ios_base& ios)
303 {
304 ios_info::get(ios).time_flags(flags::time_short);
305 return ios;
306 }
307
309 inline std::ios_base& time_medium(std::ios_base& ios)
310 {
311 ios_info::get(ios).time_flags(flags::time_medium);
312 return ios;
313 }
314
316 inline std::ios_base& time_long(std::ios_base& ios)
317 {
318 ios_info::get(ios).time_flags(flags::time_long);
319 return ios;
320 }
321
323 inline std::ios_base& time_full(std::ios_base& ios)
324 {
325 ios_info::get(ios).time_flags(flags::time_full);
326 return ios;
327 }
328
330 inline std::ios_base& date_default(std::ios_base& ios)
331 {
332 ios_info::get(ios).date_flags(flags::date_default);
333 return ios;
334 }
335
337 inline std::ios_base& date_short(std::ios_base& ios)
338 {
339 ios_info::get(ios).date_flags(flags::date_short);
340 return ios;
341 }
342
344 inline std::ios_base& date_medium(std::ios_base& ios)
345 {
346 ios_info::get(ios).date_flags(flags::date_medium);
347 return ios;
348 }
349
351 inline std::ios_base& date_long(std::ios_base& ios)
352 {
353 ios_info::get(ios).date_flags(flags::date_long);
354 return ios;
355 }
356
358 inline std::ios_base& date_full(std::ios_base& ios)
359 {
360 ios_info::get(ios).date_flags(flags::date_full);
361 return ios;
362 }
363
365 namespace detail {
366 template<typename CharType>
367 struct add_ftime {
368 std::basic_string<CharType> ftime;
369
370 void apply(std::basic_ios<CharType>& ios) const
371 {
372 ios_info::get(ios).date_time_pattern(ftime);
373 as::strftime(ios);
374 }
375 };
376
377 template<typename CharType>
378 std::basic_ostream<CharType>& operator<<(std::basic_ostream<CharType>& out, const add_ftime<CharType>& fmt)
379 {
380 fmt.apply(out);
381 return out;
382 }
383
384 template<typename CharType>
385 std::basic_istream<CharType>& operator>>(std::basic_istream<CharType>& in, const add_ftime<CharType>& fmt)
386 {
387 fmt.apply(in);
388 return in;
389 }
390
391 } // namespace detail
393
427
428 template<typename CharType>
429#ifdef BOOST_LOCALE_DOXYGEN
430 unspecified_type
431#else
432 detail::add_ftime<CharType>
433#endif
434 ftime(const std::basic_string<CharType>& format)
435 {
436 detail::add_ftime<CharType> fmt;
437 fmt.ftime = format;
438 return fmt;
439 }
440
442 template<typename CharType>
443#ifdef BOOST_LOCALE_DOXYGEN
444 unspecified_type
445#else
446 detail::add_ftime<CharType>
447#endif
448 ftime(const CharType* format)
449 {
450 detail::add_ftime<CharType> fmt;
451 fmt.ftime = format;
452 return fmt;
453 }
454
456 namespace detail {
457 struct set_timezone {
458 std::string id;
459 };
460 template<typename CharType>
461 std::basic_ostream<CharType>& operator<<(std::basic_ostream<CharType>& out, const set_timezone& fmt)
462 {
463 ios_info::get(out).time_zone(fmt.id);
464 return out;
465 }
466
467 template<typename CharType>
468 std::basic_istream<CharType>& operator>>(std::basic_istream<CharType>& in, const set_timezone& fmt)
469 {
470 ios_info::get(in).time_zone(fmt.id);
471 return in;
472 }
473 } // namespace detail
475
477 inline std::ios_base& gmt(std::ios_base& ios)
478 {
479 ios_info::get(ios).time_zone("GMT");
480 return ios;
481 }
482
484 inline std::ios_base& local_time(std::ios_base& ios)
485 {
487 return ios;
488 }
489
491 inline
492#ifdef BOOST_LOCALE_DOXYGEN
493 unspecified_type
494#else
495 detail::set_timezone
496#endif
497 time_zone(const char* id)
498 {
499 detail::set_timezone tz;
500 tz.id = id;
501 return tz;
502 }
503
505 inline
506#ifdef BOOST_LOCALE_DOXYGEN
507 unspecified_type
508#else
509 detail::set_timezone
510#endif
511 time_zone(const std::string& id)
512 {
513 detail::set_timezone tz;
514 tz.id = id;
515 return tz;
516 }
517
519
520 } // namespace as
521
522}} // namespace boost::locale
523
524#ifdef BOOST_MSVC
525# pragma warning(pop)
526#endif
527
528#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:87
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:131
std::basic_string< CharType > date_time_pattern() const
Get date/time pattern (strftime like)
Definition: formatting.hpp:137
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_istream< CharType > & operator>>(std::basic_istream< CharType > &in, date_time &t)
Definition: date_time.hpp:742
basic_format< char > format
Definition of char based format.
Definition: format.hpp:415
std::ios_base & posix(std::ios_base &ios)
Definition: formatting.hpp:202
std::ios_base & date_full(std::ios_base &ios)
set full date formatting style
Definition: formatting.hpp:358
unspecified_type ftime(const std::basic_string< CharType > &format)
Definition: formatting.hpp:434
std::ios_base & time_short(std::ios_base &ios)
set short time formatting style
Definition: formatting.hpp:302
std::ios_base & date_default(std::ios_base &ios)
set default (medium) date formatting style
Definition: formatting.hpp:330
std::ios_base & date_medium(std::ios_base &ios)
set medium date formatting style
Definition: formatting.hpp:344
std::ios_base & strftime(std::ios_base &ios)
Definition: formatting.hpp:253
std::ios_base & currency_national(std::ios_base &ios)
Set national currency formatting style, like "$".
Definition: formatting.hpp:288
std::ios_base & currency_iso(std::ios_base &ios)
Set ISO currency formatting style, like "USD", (requires ICU >= 4.2)
Definition: formatting.hpp:281
std::ios_base & currency_default(std::ios_base &ios)
Set default currency formatting style – national, like "$".
Definition: formatting.hpp:274
unspecified_type time_zone(const char *id)
Set time zone using id.
Definition: formatting.hpp:497
std::ios_base & date_long(std::ios_base &ios)
set long date formatting style
Definition: formatting.hpp:351
std::ios_base & datetime(std::ios_base &ios)
Format a date and time, number is treated as POSIX time.
Definition: formatting.hpp:245
std::ios_base & time_default(std::ios_base &ios)
set default (medium) time formatting style
Definition: formatting.hpp:295
std::ios_base & currency(std::ios_base &ios)
Format currency, number is treated like amount of money.
Definition: formatting.hpp:217
std::ios_base & time_long(std::ios_base &ios)
set long time formatting style
Definition: formatting.hpp:316
std::ios_base & number(std::ios_base &ios)
Definition: formatting.hpp:210
std::ios_base & local_time(std::ios_base &ios)
Set local time zone to stream.
Definition: formatting.hpp:484
std::ios_base & percent(std::ios_base &ios)
Format percent, value 0.3 is treated as 30%.
Definition: formatting.hpp:224
std::ios_base & spellout(std::ios_base &ios)
Spell the number, like "one hundred and ten".
Definition: formatting.hpp:260
std::ios_base & time(std::ios_base &ios)
Format a time, number is treated as POSIX time.
Definition: formatting.hpp:238
std::ios_base & date_short(std::ios_base &ios)
set short date formatting style
Definition: formatting.hpp:337
std::ios_base & ordinal(std::ios_base &ios)
Write an order of the number like 4th.
Definition: formatting.hpp:267
std::ios_base & time_full(std::ios_base &ios)
set full time formatting style
Definition: formatting.hpp:323
std::ios_base & time_medium(std::ios_base &ios)
set medium time formatting style
Definition: formatting.hpp:309
std::ios_base & date(std::ios_base &ios)
Format a date, number is treated as POSIX time.
Definition: formatting.hpp:231
std::ios_base & gmt(std::ios_base &ios)
Set GMT time zone to stream.
Definition: formatting.hpp:477
pattern_type
Special string patterns that can be used for text formatting.
Definition: formatting.hpp:70
@ datetime_pattern
strftime like formatting
Definition: formatting.hpp:71
@ time_zone_id
time zone name
Definition: formatting.hpp:72
value_type
Special integer values that can be used for formatting.
Definition: formatting.hpp:76
@ domain_id
Domain code - for message formatting.
Definition: formatting.hpp:77
display_flags_type
Definition: formatting.hpp:34
std::string global()
Get global time zone identifier. If empty, system time zone is used.