formatting.hpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  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/time_zone.hpp>
  10. #include <boost/assert.hpp>
  11. #include <boost/utility/string_view.hpp>
  12. #include <cstdint>
  13. #include <cstring>
  14. #include <istream>
  15. #include <ostream>
  16. #include <string>
  17. #include <typeinfo>
  18. #ifdef BOOST_MSVC
  19. # pragma warning(push)
  20. # pragma warning(disable : 4275 4251 4231 4660)
  21. #endif
  22. namespace boost { namespace locale {
  23. /// \brief This namespace holds additional formatting
  24. /// flags that can be set using ios_info.
  25. namespace flags {
  26. /// Formatting flags, each one of them has corresponding manipulation
  27. /// in namespace \a as
  28. enum display_flags_type {
  29. posix = 0,
  30. number = 1,
  31. currency = 2,
  32. percent = 3,
  33. date = 4,
  34. time = 5,
  35. datetime = 6,
  36. strftime = 7,
  37. spellout = 8,
  38. ordinal = 9,
  39. display_flags_mask = 31,
  40. currency_default = 0 << 5,
  41. currency_iso = 1 << 5,
  42. currency_national = 2 << 5,
  43. currency_flags_mask = 3 << 5,
  44. time_default = 0 << 7,
  45. time_short = 1 << 7,
  46. time_medium = 2 << 7,
  47. time_long = 3 << 7,
  48. time_full = 4 << 7,
  49. time_flags_mask = 7 << 7,
  50. date_default = 0 << 10,
  51. date_short = 1 << 10,
  52. date_medium = 2 << 10,
  53. date_long = 3 << 10,
  54. date_full = 4 << 10,
  55. date_flags_mask = 7 << 10,
  56. };
  57. /// Special string patterns that can be used for text formatting
  58. enum pattern_type {
  59. datetime_pattern, ///< strftime like formatting
  60. time_zone_id ///< time zone name
  61. };
  62. /// Special integer values that can be used for formatting
  63. enum value_type {
  64. domain_id ///< Domain code - for message formatting
  65. };
  66. } // namespace flags
  67. /// \brief This class holds external data beyond existing fmtflags that std::ios_base holds
  68. ///
  69. /// You should almost never create this object directly. Instead, you should access it via
  70. /// ios_info::get(stream_object) static member function. It automatically creates default formatting data for that
  71. /// stream
  72. class BOOST_LOCALE_DECL ios_info {
  73. public:
  74. /// \cond INTERNAL
  75. ios_info();
  76. ios_info(const ios_info&);
  77. ios_info& operator=(const ios_info&);
  78. ~ios_info();
  79. /// \endcond
  80. /// Get ios_info instance for specific stream object
  81. static ios_info& get(std::ios_base& ios);
  82. /// Set flags that define how to format data, e.g. number, spell, currency etc.
  83. void display_flags(uint64_t flags);
  84. /// Get flags that define how to format data, e.g. number, spell, currency etc.
  85. uint64_t display_flags() const;
  86. /// Set flags that define how to format currency
  87. void currency_flags(uint64_t flags);
  88. /// Get flags that define how to format currency
  89. uint64_t currency_flags() const;
  90. /// Set flags that define how to format date
  91. void date_flags(uint64_t flags);
  92. /// Get flags that define how to format date
  93. uint64_t date_flags() const;
  94. /// Set flags that define how to format time
  95. void time_flags(uint64_t flags);
  96. /// Get flags that define how to format time
  97. uint64_t time_flags() const;
  98. /// Set special message domain identification
  99. void domain_id(int);
  100. /// Get special message domain identification
  101. int domain_id() const;
  102. /// Set time zone for formatting dates and time
  103. void time_zone(const std::string&);
  104. /// Get time zone for formatting dates and time
  105. std::string time_zone() const;
  106. /// Set date/time pattern (strftime like)
  107. template<typename CharType>
  108. void date_time_pattern(const std::basic_string<CharType>& str)
  109. {
  110. date_time_pattern_set().set<CharType>(str);
  111. }
  112. /// Get date/time pattern (strftime like)
  113. template<typename CharType>
  114. std::basic_string<CharType> date_time_pattern() const
  115. {
  116. return date_time_pattern_set().get<CharType>();
  117. }
  118. /// \cond INTERNAL
  119. void on_imbue();
  120. /// \endcond
  121. private:
  122. class string_set;
  123. const string_set& date_time_pattern_set() const;
  124. string_set& date_time_pattern_set();
  125. class BOOST_LOCALE_DECL string_set {
  126. public:
  127. string_set();
  128. ~string_set();
  129. string_set(const string_set& other);
  130. string_set& operator=(string_set other);
  131. void swap(string_set& other);
  132. template<typename Char>
  133. void set(const boost::basic_string_view<Char> s)
  134. {
  135. BOOST_ASSERT(!s.empty());
  136. delete[] ptr;
  137. ptr = nullptr;
  138. type = &typeid(Char);
  139. size = sizeof(Char) * s.size();
  140. ptr = size ? new char[size] : nullptr;
  141. memcpy(ptr, s.data(), size);
  142. }
  143. template<typename Char>
  144. std::basic_string<Char> get() const
  145. {
  146. if(type == nullptr || *type != typeid(Char))
  147. throw std::bad_cast();
  148. std::basic_string<Char> result(size / sizeof(Char), Char(0));
  149. memcpy(&result.front(), ptr, size);
  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. inline bool is_datetime_display_flags(const uint64_t display_flags)
  311. {
  312. return (display_flags == flags::date || display_flags == flags::time || display_flags == flags::datetime
  313. || display_flags == flags::strftime);
  314. }
  315. template<typename CharType>
  316. struct add_ftime {
  317. std::basic_string<CharType> ftime;
  318. void apply(std::basic_ios<CharType>& ios) const
  319. {
  320. ios_info::get(ios).date_time_pattern(ftime);
  321. as::strftime(ios);
  322. }
  323. };
  324. template<typename CharType>
  325. std::basic_ostream<CharType>& operator<<(std::basic_ostream<CharType>& out, const add_ftime<CharType>& fmt)
  326. {
  327. fmt.apply(out);
  328. return out;
  329. }
  330. template<typename CharType>
  331. std::basic_istream<CharType>& operator>>(std::basic_istream<CharType>& in, const add_ftime<CharType>& fmt)
  332. {
  333. fmt.apply(in);
  334. return in;
  335. }
  336. } // namespace detail
  337. /// \endcond
  338. /// Set strftime like formatting string
  339. ///
  340. /// Please note, formatting flags are very similar but not exactly the same as flags for C function strftime.
  341. /// Differences: some flags as "%e" do not add blanks to fill text up to two spaces, not all flags supported.
  342. ///
  343. /// Flags:
  344. /// - "%a" -- Abbreviated weekday (Sun.)
  345. /// - "%A" -- Full weekday (Sunday)
  346. /// - "%b" -- Abbreviated month (Jan.)
  347. /// - "%B" -- Full month (January)
  348. /// - "%c" -- Locale date-time format. **Note:** prefer using "as::datetime"
  349. /// - "%d" -- Day of Month [01,31]
  350. /// - "%e" -- Day of Month [1,31]
  351. /// - "%h" -- Same as "%b"
  352. /// - "%H" -- 24 clock hour [00,23]
  353. /// - "%I" -- 12 clock hour [01,12]
  354. /// - "%j" -- Day of year [1,366]
  355. /// - "%m" -- Month [01,12]
  356. /// - "%M" -- Minute [00,59]
  357. /// - "%n" -- New Line
  358. /// - "%p" -- AM/PM in locale representation
  359. /// - "%r" -- Time with AM/PM, same as "%I:%M:%S %p"
  360. /// - "%R" -- Same as "%H:%M"
  361. /// - "%S" -- Second [00,61]
  362. /// - "%t" -- Tab character
  363. /// - "%T" -- Same as "%H:%M:%S"
  364. /// - "%x" -- Local date representation. **Note:** prefer using "as::date"
  365. /// - "%X" -- Local time representation. **Note:** prefer using "as::time"
  366. /// - "%y" -- Year [00,99]
  367. /// - "%Y" -- 4 digits year. (2009)
  368. /// - "%Z" -- Time Zone
  369. /// - "%%" -- Percent symbol
  370. ///
  371. template<typename CharType>
  372. #ifdef BOOST_LOCALE_DOXYGEN
  373. unspecified_type
  374. #else
  375. detail::add_ftime<CharType>
  376. #endif
  377. ftime(const std::basic_string<CharType>& format)
  378. {
  379. detail::add_ftime<CharType> fmt;
  380. fmt.ftime = format;
  381. return fmt;
  382. }
  383. /// See ftime(std::basic_string<CharType> const &format)
  384. template<typename CharType>
  385. #ifdef BOOST_LOCALE_DOXYGEN
  386. unspecified_type
  387. #else
  388. detail::add_ftime<CharType>
  389. #endif
  390. ftime(const CharType* format)
  391. {
  392. detail::add_ftime<CharType> fmt;
  393. fmt.ftime = format;
  394. return fmt;
  395. }
  396. /// \cond INTERNAL
  397. namespace detail {
  398. struct set_timezone {
  399. std::string id;
  400. };
  401. template<typename CharType>
  402. std::basic_ostream<CharType>& operator<<(std::basic_ostream<CharType>& out, const set_timezone& fmt)
  403. {
  404. ios_info::get(out).time_zone(fmt.id);
  405. return out;
  406. }
  407. template<typename CharType>
  408. std::basic_istream<CharType>& operator>>(std::basic_istream<CharType>& in, const set_timezone& fmt)
  409. {
  410. ios_info::get(in).time_zone(fmt.id);
  411. return in;
  412. }
  413. } // namespace detail
  414. /// \endcond
  415. /// Set GMT time zone to stream
  416. inline std::ios_base& gmt(std::ios_base& ios)
  417. {
  418. ios_info::get(ios).time_zone("GMT");
  419. return ios;
  420. }
  421. /// Set local time zone to stream
  422. inline std::ios_base& local_time(std::ios_base& ios)
  423. {
  424. ios_info::get(ios).time_zone(time_zone::global());
  425. return ios;
  426. }
  427. /// Set time zone using \a id
  428. inline
  429. #ifdef BOOST_LOCALE_DOXYGEN
  430. unspecified_type
  431. #else
  432. detail::set_timezone
  433. #endif
  434. time_zone(const char* id)
  435. {
  436. detail::set_timezone tz;
  437. tz.id = id;
  438. return tz;
  439. }
  440. /// Set time zone using \a id
  441. inline
  442. #ifdef BOOST_LOCALE_DOXYGEN
  443. unspecified_type
  444. #else
  445. detail::set_timezone
  446. #endif
  447. time_zone(const std::string& id)
  448. {
  449. detail::set_timezone tz;
  450. tz.id = id;
  451. return tz;
  452. }
  453. /// @}
  454. } // namespace as
  455. }} // namespace boost::locale
  456. #ifdef BOOST_MSVC
  457. # pragma warning(pop)
  458. #endif
  459. #endif