2 // Copyright (c) 2009-2011 Artyom Beilis (Tonkikh)
4 // Distributed under the Boost Software License, Version 1.0. (See
5 // accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt)
8 #ifndef BOOST_LOCALE_FORMATTING_HPP_INCLUDED
9 #define BOOST_LOCALE_FORMATTING_HPP_INCLUDED
11 #include <boost/locale/config.hpp>
13 # pragma warning(push)
14 # pragma warning(disable : 4275 4251 4231 4660)
16 #include <boost/cstdint.hpp>
17 #include <boost/locale/time_zone.hpp>
27 /// \brief This namespace holds additional formatting
28 /// flags that can be set using ios_info.
32 /// Formatting flags, each one of them has corresponding manipulation
33 /// in namespace \a as
47 display_flags_mask = 31,
49 currency_default = 0 << 5,
50 currency_iso = 1 << 5,
51 currency_national = 2 << 5,
53 currency_flags_mask = 3 << 5,
55 time_default = 0 << 7,
60 time_flags_mask = 7 << 7,
62 date_default = 0 << 10,
64 date_medium = 2 << 10,
67 date_flags_mask = 7 << 10,
69 datetime_flags_mask = date_flags_mask | time_flags_mask
74 /// Special string patters that can be used
75 /// for text formatting
78 datetime_pattern, ///< strftime like formatting
79 time_zone_id ///< time zone name
83 /// Special integer values that can be used for formatting
86 domain_id ///< Domain code - for message formatting
93 /// \brief This class holds an external data - beyond existing fmtflags that std::ios_base holds
95 /// You should almost never create this object directly. Instead, you should access it via ios_info::get(stream_object)
96 /// static member function. It automatically creates default formatting data for that stream
98 class BOOST_LOCALE_DECL ios_info {
104 ios_info(ios_info const &);
105 ios_info const &operator=(ios_info const &);
111 /// Get ios_info instance for specific stream object
113 static ios_info &get(std::ios_base &ios);
116 /// Set a flags that define a way for format data like number, spell, currency etc.
118 void display_flags(uint64_t flags);
121 /// Set a flags that define how to format currency
123 void currency_flags(uint64_t flags);
126 /// Set a flags that define how to format date
128 void date_flags(uint64_t flags);
131 /// Set a flags that define how to format time
133 void time_flags(uint64_t flags);
136 /// Set a flags that define how to format both date and time
138 void datetime_flags(uint64_t flags);
141 /// Set special message domain identification
146 /// Set time zone for formatting dates and time
148 void time_zone(std::string const &);
152 /// Set date/time pattern (strftime like)
154 template<typename CharType>
155 void date_time_pattern(std::basic_string<CharType> const &str)
157 string_set &s = date_time_pattern_set();
158 s.set<CharType>(str.c_str());
163 /// Get a flags that define a way for format data like number, spell, currency etc.
165 uint64_t display_flags() const;
168 /// Get a flags that define how to format currency
170 uint64_t currency_flags() const;
174 /// Get a flags that define how to format date
176 uint64_t date_flags() const;
179 /// Get a flags that define how to format time
181 uint64_t time_flags() const;
184 /// Get a flags that define how to format both date and time
186 uint64_t datetime_flags() const;
189 /// Get special message domain identification
191 int domain_id() const;
194 /// Get time zone for formatting dates and time
196 std::string time_zone() const;
199 /// Get date/time pattern (strftime like)
201 template<typename CharType>
202 std::basic_string<CharType> date_time_pattern() const
204 string_set const &s = date_time_pattern_set();
205 return s.get<CharType>();
216 string_set const &date_time_pattern_set() const;
217 string_set &date_time_pattern_set();
219 class BOOST_LOCALE_DECL string_set {
223 string_set(string_set const &other);
224 string_set const &operator=(string_set const &other);
225 void swap(string_set &other);
227 template<typename Char>
228 void set(Char const *s)
234 while(*end!=0) end++;
235 // if ptr = 0 it does not matter what is value of size
236 size = sizeof(Char)*(end - s+1);
237 ptr = new char[size];
241 template<typename Char>
242 std::basic_string<Char> get() const
244 if(type==0 || *type!=typeid(Char))
245 throw std::bad_cast();
246 std::basic_string<Char> result = reinterpret_cast<Char const *>(ptr);
251 std::type_info const *type;
258 std::string time_zone_;
259 string_set datetime_;
268 /// \brief This namespace includes all manipulators that can be used on IO streams
272 /// \defgroup manipulators I/O Stream manipulators
278 /// Format values with "POSIX" or "C" locale. Note, if locale was created with additional non-classic locale then
279 /// These numbers may be localized
282 inline std::ios_base & posix(std::ios_base & ios)
284 ios_info::get(ios).display_flags(flags::posix);
289 /// Format a number. Note, unlike standard number formatting, integers would be treated like real numbers when std::fixed or
290 /// std::scientific manipulators were applied
292 inline std::ios_base & number(std::ios_base & ios)
294 ios_info::get(ios).display_flags(flags::number);
299 /// Format currency, number is treated like amount of money
301 inline std::ios_base & currency(std::ios_base & ios)
303 ios_info::get(ios).display_flags(flags::currency);
308 /// Format percent, value 0.3 is treated as 30%.
310 inline std::ios_base & percent(std::ios_base & ios)
312 ios_info::get(ios).display_flags(flags::percent);
317 /// Format a date, number is treated as POSIX time
319 inline std::ios_base & date(std::ios_base & ios)
321 ios_info::get(ios).display_flags(flags::date);
326 /// Format a time, number is treated as POSIX time
328 inline std::ios_base & time(std::ios_base & ios)
330 ios_info::get(ios).display_flags(flags::time);
335 /// Format a date and time, number is treated as POSIX time
337 inline std::ios_base & datetime(std::ios_base & ios)
339 ios_info::get(ios).display_flags(flags::datetime);
344 /// Create formatted date time, Please note, this manipulator only changes formatting mode,
345 /// and not format itself, so you are probably looking for ftime manipulator
347 inline std::ios_base & strftime(std::ios_base & ios)
349 ios_info::get(ios).display_flags(flags::strftime);
354 /// Spell the number, like "one hundred and ten"
356 inline std::ios_base & spellout(std::ios_base & ios)
358 ios_info::get(ios).display_flags(flags::spellout);
363 /// Write an order of the number like 4th.
365 inline std::ios_base & ordinal(std::ios_base & ios)
367 ios_info::get(ios).display_flags(flags::ordinal);
372 /// Set default currency formatting style -- national, like "$"
374 inline std::ios_base & currency_default(std::ios_base & ios)
376 ios_info::get(ios).currency_flags(flags::currency_default);
381 /// Set ISO currency formatting style, like "USD", (requires ICU >= 4.2)
383 inline std::ios_base & currency_iso(std::ios_base & ios)
385 ios_info::get(ios).currency_flags(flags::currency_iso);
390 /// Set national currency formatting style, like "$"
392 inline std::ios_base & currency_national(std::ios_base & ios)
394 ios_info::get(ios).currency_flags(flags::currency_national);
399 /// set default (medium) time formatting style
401 inline std::ios_base & time_default(std::ios_base & ios)
403 ios_info::get(ios).time_flags(flags::time_default);
408 /// set short time formatting style
410 inline std::ios_base & time_short(std::ios_base & ios)
412 ios_info::get(ios).time_flags(flags::time_short);
417 /// set medium time formatting style
419 inline std::ios_base & time_medium(std::ios_base & ios)
421 ios_info::get(ios).time_flags(flags::time_medium);
426 /// set long time formatting style
428 inline std::ios_base & time_long(std::ios_base & ios)
430 ios_info::get(ios).time_flags(flags::time_long);
435 /// set full time formatting style
437 inline std::ios_base & time_full(std::ios_base & ios)
439 ios_info::get(ios).time_flags(flags::time_full);
444 /// set default (medium) date formatting style
446 inline std::ios_base & date_default(std::ios_base & ios)
448 ios_info::get(ios).date_flags(flags::date_default);
453 /// set short date formatting style
455 inline std::ios_base & date_short(std::ios_base & ios)
457 ios_info::get(ios).date_flags(flags::date_short);
462 /// set medium date formatting style
464 inline std::ios_base & date_medium(std::ios_base & ios)
466 ios_info::get(ios).date_flags(flags::date_medium);
471 /// set long date formatting style
473 inline std::ios_base & date_long(std::ios_base & ios)
475 ios_info::get(ios).date_flags(flags::date_long);
480 /// set full date formatting style
482 inline std::ios_base & date_full(std::ios_base & ios)
484 ios_info::get(ios).date_flags(flags::date_full);
491 template<typename CharType>
494 std::basic_string<CharType> ftime;
496 void apply(std::basic_ios<CharType> &ios) const
498 ios_info::get(ios).date_time_pattern(ftime);
504 template<typename CharType>
505 std::basic_ostream<CharType> &operator<<(std::basic_ostream<CharType> &out,add_ftime<CharType> const &fmt)
511 template<typename CharType>
512 std::basic_istream<CharType> &operator>>(std::basic_istream<CharType> &in,add_ftime<CharType> const &fmt)
522 /// Set strftime like formatting string
524 /// Please note, formatting flags are very similar but not exactly the same as flags for C function strftime.
525 /// Differences: some flags as "%e" do not add blanks to fill text up to two spaces, not all flags supported.
528 /// - "%a" -- Abbreviated weekday (Sun.)
529 /// - "%A" -- Full weekday (Sunday)
530 /// - "%b" -- Abbreviated month (Jan.)
531 /// - "%B" -- Full month (January)
532 /// - "%c" -- Locale date-time format. **Note:** prefer using "as::datetime"
533 /// - "%d" -- Day of Month [01,31]
534 /// - "%e" -- Day of Month [1,31]
535 /// - "%h" -- Same as "%b"
536 /// - "%H" -- 24 clock hour [00,23]
537 /// - "%I" -- 12 clock hour [01,12]
538 /// - "%j" -- Day of year [1,366]
539 /// - "%m" -- Month [01,12]
540 /// - "%M" -- Minute [00,59]
541 /// - "%n" -- New Line
542 /// - "%p" -- AM/PM in locale representation
543 /// - "%r" -- Time with AM/PM, same as "%I:%M:%S %p"
544 /// - "%R" -- Same as "%H:%M"
545 /// - "%S" -- Second [00,61]
546 /// - "%t" -- Tab character
547 /// - "%T" -- Same as "%H:%M:%S"
548 /// - "%x" -- Local date representation. **Note:** prefer using "as::date"
549 /// - "%X" -- Local time representation. **Note:** prefer using "as::time"
550 /// - "%y" -- Year [00,99]
551 /// - "%Y" -- 4 digits year. (2009)
552 /// - "%Z" -- Time Zone
553 /// - "%%" -- Percent symbol
557 template<typename CharType>
558 #ifdef BOOST_LOCALE_DOXYGEN
561 details::add_ftime<CharType>
563 ftime(std::basic_string<CharType> const &format)
565 details::add_ftime<CharType> fmt;
571 /// See ftime(std::basic_string<CharType> const &format)
573 template<typename CharType>
574 #ifdef BOOST_LOCALE_DOXYGEN
577 details::add_ftime<CharType>
579 ftime(CharType const *format)
581 details::add_ftime<CharType> fmt;
588 struct set_timezone {
591 template<typename CharType>
592 std::basic_ostream<CharType> &operator<<(std::basic_ostream<CharType> &out,set_timezone const &fmt)
594 ios_info::get(out).time_zone(fmt.id);
598 template<typename CharType>
599 std::basic_istream<CharType> &operator>>(std::basic_istream<CharType> &in,set_timezone const &fmt)
601 ios_info::get(in).time_zone(fmt.id);
608 /// Set GMT time zone to stream
610 inline std::ios_base &gmt(std::ios_base &ios)
612 ios_info::get(ios).time_zone("GMT");
617 /// Set local time zone to stream
619 inline std::ios_base &local_time(std::ios_base &ios)
621 ios_info::get(ios).time_zone(time_zone::global());
626 /// Set time zone using \a id
629 #ifdef BOOST_LOCALE_DOXYGEN
632 details::set_timezone
634 time_zone(char const *id)
636 details::set_timezone tz;
642 /// Set time zone using \a id
645 #ifdef BOOST_LOCALE_DOXYGEN
648 details::set_timezone
650 time_zone(std::string const &id)
652 details::set_timezone tz;
673 // vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4