formatting.hpp 15 KB

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