formatting.hpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. //
  2. // Copyright (c) 2009-2011 Artyom Beilis (Tonkikh)
  3. //
  4. // Distributed under the Boost Software License, Version 1.0.
  5. // https://www.boost.org/LICENSE_1_0.txt
  6. #ifndef BOOST_LOCALE_FORMATTING_HPP_INCLUDED
  7. #define BOOST_LOCALE_FORMATTING_HPP_INCLUDED
  8. #include <boost/locale/time_zone.hpp>
  9. #include <boost/locale/util/string.hpp>
  10. #include <boost/assert.hpp>
  11. #include <boost/cstdint.hpp>
  12. #include <cstring>
  13. #include <istream>
  14. #include <ostream>
  15. #include <string>
  16. #include <typeinfo>
  17. #ifdef BOOST_MSVC
  18. # pragma warning(push)
  19. # pragma warning(disable : 4275 4251 4231 4660)
  20. #endif
  21. namespace boost { namespace locale {
  22. /// \brief This namespace holds additional formatting
  23. /// flags that can be set using ios_info.
  24. namespace flags {
  25. /// Formatting flags, each one of them has corresponding manipulation
  26. /// in namespace \a as
  27. enum display_flags_type {
  28. posix = 0,
  29. number = 1,
  30. currency = 2,
  31. percent = 3,
  32. date = 4,
  33. time = 5,
  34. datetime = 6,
  35. strftime = 7,
  36. spellout = 8,
  37. ordinal = 9,
  38. display_flags_mask = 31,
  39. currency_default = 0 << 5,
  40. currency_iso = 1 << 5,
  41. currency_national = 2 << 5,
  42. currency_flags_mask = 3 << 5,
  43. time_default = 0 << 7,
  44. time_short = 1 << 7,
  45. time_medium = 2 << 7,
  46. time_long = 3 << 7,
  47. time_full = 4 << 7,
  48. time_flags_mask = 7 << 7,
  49. date_default = 0 << 10,
  50. date_short = 1 << 10,
  51. date_medium = 2 << 10,
  52. date_long = 3 << 10,
  53. date_full = 4 << 10,
  54. date_flags_mask = 7 << 10,
  55. };
  56. /// Special string patterns that can be used for text formatting
  57. enum pattern_type {
  58. datetime_pattern, ///< strftime like formatting
  59. time_zone_id ///< time zone name
  60. };
  61. /// Special integer values that can be used for formatting
  62. enum value_type {
  63. domain_id ///< Domain code - for message formatting
  64. };
  65. } // namespace flags
  66. /// \brief This class holds external data beyond existing fmtflags that std::ios_base holds
  67. ///
  68. /// You should almost never create this object directly. Instead, you should access it via
  69. /// ios_info::get(stream_object) static member function. It automatically creates default formatting data for that
  70. /// stream
  71. class BOOST_LOCALE_DECL ios_info {
  72. public:
  73. /// \cond INTERNAL
  74. ios_info();
  75. ios_info(const ios_info&);
  76. ios_info& operator=(const ios_info&);
  77. ~ios_info();
  78. /// \endcond
  79. /// Get ios_info instance for specific stream object
  80. static ios_info& get(std::ios_base& ios);
  81. /// Set flags that define how to format data, e.g. number, spell, currency etc.
  82. void display_flags(uint64_t flags);
  83. /// Get flags that define how to format data, e.g. number, spell, currency etc.
  84. uint64_t display_flags() const;
  85. /// Set flags that define how to format currency
  86. void currency_flags(uint64_t flags);
  87. /// Get flags that define how to format currency
  88. uint64_t currency_flags() const;
  89. /// Set flags that define how to format date
  90. void date_flags(uint64_t flags);
  91. /// Get flags that define how to format date
  92. uint64_t date_flags() const;
  93. /// Set flags that define how to format time
  94. void time_flags(uint64_t flags);
  95. /// Get flags that define how to format time
  96. uint64_t time_flags() const;
  97. /// Set special message domain identification
  98. void domain_id(int);
  99. /// Get special message domain identification
  100. int domain_id() const;
  101. /// Set time zone for formatting dates and time
  102. void time_zone(const std::string&);
  103. /// Get time zone for formatting dates and time
  104. std::string time_zone() const;
  105. /// Set date/time pattern (strftime like)
  106. template<typename CharType>
  107. void date_time_pattern(const std::basic_string<CharType>& str)
  108. {
  109. string_set& s = date_time_pattern_set();
  110. s.set(str.c_str());
  111. }
  112. /// Get date/time pattern (strftime like)
  113. template<typename CharType>
  114. std::basic_string<CharType> date_time_pattern() const
  115. {
  116. const string_set& s = date_time_pattern_set();
  117. return s.get<CharType>();
  118. }
  119. /// \cond INTERNAL
  120. void on_imbue();
  121. /// \endcond
  122. private:
  123. class string_set;
  124. const string_set& date_time_pattern_set() const;
  125. string_set& date_time_pattern_set();
  126. class BOOST_LOCALE_DECL string_set {
  127. public:
  128. string_set();
  129. ~string_set();
  130. string_set(const string_set& other);
  131. string_set& operator=(string_set other);
  132. void swap(string_set& other);
  133. template<typename Char>
  134. void set(const Char* s)
  135. {
  136. BOOST_ASSERT(s);
  137. delete[] ptr;
  138. ptr = nullptr;
  139. type = &typeid(Char);
  140. size = sizeof(Char) * (util::str_end(s) - s + 1);
  141. ptr = new char[size];
  142. memcpy(ptr, s, size);
  143. }
  144. template<typename Char>
  145. std::basic_string<Char> get() const
  146. {
  147. if(type == 0 || *type != typeid(Char))
  148. throw std::bad_cast();
  149. std::basic_string<Char> result = reinterpret_cast<const Char*>(ptr);
  150. return result;
  151. }
  152. private:
  153. const std::type_info* type;
  154. size_t size;
  155. char* ptr;
  156. };
  157. uint64_t flags_;
  158. int domain_id_;
  159. std::string time_zone_;
  160. string_set datetime_;
  161. };
  162. /// \brief This namespace includes all manipulators that can be used on IO streams
  163. namespace as {
  164. /// \defgroup manipulators I/O Stream manipulators
  165. ///
  166. /// @{
  167. /// Format values with "POSIX" or "C" locale. Note, if locale was created with additional non-classic locale
  168. /// then These numbers may be localized
  169. inline std::ios_base& posix(std::ios_base& ios)
  170. {
  171. ios_info::get(ios).display_flags(flags::posix);
  172. return ios;
  173. }
  174. /// Format a number. Note, unlike standard number formatting, integers would be treated like real numbers when
  175. /// std::fixed or std::scientific manipulators were applied
  176. inline std::ios_base& number(std::ios_base& ios)
  177. {
  178. ios_info::get(ios).display_flags(flags::number);
  179. return ios;
  180. }
  181. /// Format currency, number is treated like amount of money
  182. inline std::ios_base& currency(std::ios_base& ios)
  183. {
  184. ios_info::get(ios).display_flags(flags::currency);
  185. return ios;
  186. }
  187. /// Format percent, value 0.3 is treated as 30%.
  188. inline std::ios_base& percent(std::ios_base& ios)
  189. {
  190. ios_info::get(ios).display_flags(flags::percent);
  191. return ios;
  192. }
  193. /// Format a date, number is treated as POSIX time
  194. inline std::ios_base& date(std::ios_base& ios)
  195. {
  196. ios_info::get(ios).display_flags(flags::date);
  197. return ios;
  198. }
  199. /// Format a time, number is treated as POSIX time
  200. inline std::ios_base& time(std::ios_base& ios)
  201. {
  202. ios_info::get(ios).display_flags(flags::time);
  203. return ios;
  204. }
  205. /// Format a date and time, number is treated as POSIX time
  206. inline std::ios_base& datetime(std::ios_base& ios)
  207. {
  208. ios_info::get(ios).display_flags(flags::datetime);
  209. return ios;
  210. }
  211. /// Create formatted date time, Please note, this manipulator only changes formatting mode,
  212. /// and not format itself, so you are probably looking for ftime manipulator
  213. inline std::ios_base& strftime(std::ios_base& ios)
  214. {
  215. ios_info::get(ios).display_flags(flags::strftime);
  216. return ios;
  217. }
  218. /// Spell the number, like "one hundred and ten"
  219. inline std::ios_base& spellout(std::ios_base& ios)
  220. {
  221. ios_info::get(ios).display_flags(flags::spellout);
  222. return ios;
  223. }
  224. /// Write an order of the number like 4th.
  225. inline std::ios_base& ordinal(std::ios_base& ios)
  226. {
  227. ios_info::get(ios).display_flags(flags::ordinal);
  228. return ios;
  229. }
  230. /// Set default currency formatting style -- national, like "$"
  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. /// Set ISO currency formatting style, like "USD", (requires ICU >= 4.2)
  237. inline std::ios_base& currency_iso(std::ios_base& ios)
  238. {
  239. ios_info::get(ios).currency_flags(flags::currency_iso);
  240. return ios;
  241. }
  242. /// Set national currency formatting style, like "$"
  243. inline std::ios_base& currency_national(std::ios_base& ios)
  244. {
  245. ios_info::get(ios).currency_flags(flags::currency_national);
  246. return ios;
  247. }
  248. /// set default (medium) time formatting style
  249. inline std::ios_base& time_default(std::ios_base& ios)
  250. {
  251. ios_info::get(ios).time_flags(flags::time_default);
  252. return ios;
  253. }
  254. /// set short time formatting style
  255. inline std::ios_base& time_short(std::ios_base& ios)
  256. {
  257. ios_info::get(ios).time_flags(flags::time_short);
  258. return ios;
  259. }
  260. /// set medium time formatting style
  261. inline std::ios_base& time_medium(std::ios_base& ios)
  262. {
  263. ios_info::get(ios).time_flags(flags::time_medium);
  264. return ios;
  265. }
  266. /// set long time formatting style
  267. inline std::ios_base& time_long(std::ios_base& ios)
  268. {
  269. ios_info::get(ios).time_flags(flags::time_long);
  270. return ios;
  271. }
  272. /// set full time formatting style
  273. inline std::ios_base& time_full(std::ios_base& ios)
  274. {
  275. ios_info::get(ios).time_flags(flags::time_full);
  276. return ios;
  277. }
  278. /// set default (medium) date formatting style
  279. inline std::ios_base& date_default(std::ios_base& ios)
  280. {
  281. ios_info::get(ios).date_flags(flags::date_default);
  282. return ios;
  283. }
  284. /// set short date formatting style
  285. inline std::ios_base& date_short(std::ios_base& ios)
  286. {
  287. ios_info::get(ios).date_flags(flags::date_short);
  288. return ios;
  289. }
  290. /// set medium date formatting style
  291. inline std::ios_base& date_medium(std::ios_base& ios)
  292. {
  293. ios_info::get(ios).date_flags(flags::date_medium);
  294. return ios;
  295. }
  296. /// set long date formatting style
  297. inline std::ios_base& date_long(std::ios_base& ios)
  298. {
  299. ios_info::get(ios).date_flags(flags::date_long);
  300. return ios;
  301. }
  302. /// set full date formatting style
  303. inline std::ios_base& date_full(std::ios_base& ios)
  304. {
  305. ios_info::get(ios).date_flags(flags::date_full);
  306. return ios;
  307. }
  308. /// \cond INTERNAL
  309. namespace detail {
  310. template<typename CharType>
  311. struct add_ftime {
  312. std::basic_string<CharType> ftime;
  313. void apply(std::basic_ios<CharType>& ios) const
  314. {
  315. ios_info::get(ios).date_time_pattern(ftime);
  316. as::strftime(ios);
  317. }
  318. };
  319. template<typename CharType>
  320. std::basic_ostream<CharType>& operator<<(std::basic_ostream<CharType>& out, const add_ftime<CharType>& fmt)
  321. {
  322. fmt.apply(out);
  323. return out;
  324. }
  325. template<typename CharType>
  326. std::basic_istream<CharType>& operator>>(std::basic_istream<CharType>& in, const add_ftime<CharType>& fmt)
  327. {
  328. fmt.apply(in);
  329. return in;
  330. }
  331. } // namespace detail
  332. /// \endcond
  333. /// Set strftime like formatting string
  334. ///
  335. /// Please note, formatting flags are very similar but not exactly the same as flags for C function strftime.
  336. /// Differences: some flags as "%e" do not add blanks to fill text up to two spaces, not all flags supported.
  337. ///
  338. /// Flags:
  339. /// - "%a" -- Abbreviated weekday (Sun.)
  340. /// - "%A" -- Full weekday (Sunday)
  341. /// - "%b" -- Abbreviated month (Jan.)
  342. /// - "%B" -- Full month (January)
  343. /// - "%c" -- Locale date-time format. **Note:** prefer using "as::datetime"
  344. /// - "%d" -- Day of Month [01,31]
  345. /// - "%e" -- Day of Month [1,31]
  346. /// - "%h" -- Same as "%b"
  347. /// - "%H" -- 24 clock hour [00,23]
  348. /// - "%I" -- 12 clock hour [01,12]
  349. /// - "%j" -- Day of year [1,366]
  350. /// - "%m" -- Month [01,12]
  351. /// - "%M" -- Minute [00,59]
  352. /// - "%n" -- New Line
  353. /// - "%p" -- AM/PM in locale representation
  354. /// - "%r" -- Time with AM/PM, same as "%I:%M:%S %p"
  355. /// - "%R" -- Same as "%H:%M"
  356. /// - "%S" -- Second [00,61]
  357. /// - "%t" -- Tab character
  358. /// - "%T" -- Same as "%H:%M:%S"
  359. /// - "%x" -- Local date representation. **Note:** prefer using "as::date"
  360. /// - "%X" -- Local time representation. **Note:** prefer using "as::time"
  361. /// - "%y" -- Year [00,99]
  362. /// - "%Y" -- 4 digits year. (2009)
  363. /// - "%Z" -- Time Zone
  364. /// - "%%" -- Percent symbol
  365. ///
  366. template<typename CharType>
  367. #ifdef BOOST_LOCALE_DOXYGEN
  368. unspecified_type
  369. #else
  370. detail::add_ftime<CharType>
  371. #endif
  372. ftime(const std::basic_string<CharType>& format)
  373. {
  374. detail::add_ftime<CharType> fmt;
  375. fmt.ftime = format;
  376. return fmt;
  377. }
  378. /// See ftime(std::basic_string<CharType> const &format)
  379. template<typename CharType>
  380. #ifdef BOOST_LOCALE_DOXYGEN
  381. unspecified_type
  382. #else
  383. detail::add_ftime<CharType>
  384. #endif
  385. ftime(const CharType* format)
  386. {
  387. detail::add_ftime<CharType> fmt;
  388. fmt.ftime = format;
  389. return fmt;
  390. }
  391. /// \cond INTERNAL
  392. namespace detail {
  393. struct set_timezone {
  394. std::string id;
  395. };
  396. template<typename CharType>
  397. std::basic_ostream<CharType>& operator<<(std::basic_ostream<CharType>& out, const set_timezone& fmt)
  398. {
  399. ios_info::get(out).time_zone(fmt.id);
  400. return out;
  401. }
  402. template<typename CharType>
  403. std::basic_istream<CharType>& operator>>(std::basic_istream<CharType>& in, const set_timezone& fmt)
  404. {
  405. ios_info::get(in).time_zone(fmt.id);
  406. return in;
  407. }
  408. } // namespace detail
  409. /// \endcond
  410. /// Set GMT time zone to stream
  411. inline std::ios_base& gmt(std::ios_base& ios)
  412. {
  413. ios_info::get(ios).time_zone("GMT");
  414. return ios;
  415. }
  416. /// Set local time zone to stream
  417. inline std::ios_base& local_time(std::ios_base& ios)
  418. {
  419. ios_info::get(ios).time_zone(time_zone::global());
  420. return ios;
  421. }
  422. /// Set time zone using \a id
  423. inline
  424. #ifdef BOOST_LOCALE_DOXYGEN
  425. unspecified_type
  426. #else
  427. detail::set_timezone
  428. #endif
  429. time_zone(const char* id)
  430. {
  431. detail::set_timezone tz;
  432. tz.id = id;
  433. return tz;
  434. }
  435. /// Set time zone using \a id
  436. inline
  437. #ifdef BOOST_LOCALE_DOXYGEN
  438. unspecified_type
  439. #else
  440. detail::set_timezone
  441. #endif
  442. time_zone(const std::string& id)
  443. {
  444. detail::set_timezone tz;
  445. tz.id = id;
  446. return tz;
  447. }
  448. /// @}
  449. } // namespace as
  450. }} // namespace boost::locale
  451. #ifdef BOOST_MSVC
  452. # pragma warning(pop)
  453. #endif
  454. #endif