formatting_and_parsing.txt 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. //
  2. // Copyright (c) 2009-2011 Artyom Beilis (Tonkikh)
  3. //
  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)
  7. //
  8. // vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4 filetype=cpp.doxygen
  9. /*!
  10. \page formatting_and_parsing Numbers, Time and Currency formatting and parsing
  11. All formatting and parsing is performed via the standard I/O streams. Each of the above information types is represented as a number.
  12. The formatting information is set using iostream manipulators. All manipulators are placed in the boost::locale::as namespace.
  13. For example:
  14. \code
  15. cout << as::currency << 123.45 << endl;
  16. // display 123.45 in local currency representation.
  17. cin >> as::currency >> x ;
  18. // Parse currency representation and store it in x
  19. \endcode
  20. There is a special manipulator \c as::posix that "unsets" locale-specific settings and returns them to the default \c iostream formatting
  21. and parsing methods. Please note, such formats may still be localized by the default \c std::num_put and \c std::num_get facets.
  22. \section numbers_formatting Numbers and number manipulators
  23. Here are the manipulators for number formatting:
  24. - \c as::number -- format number according to local specifications, it takes into account various \c std::ios_base flags like scientific
  25. format and precision.
  26. \n
  27. - \c as::percent -- format number as "percent" format. For example:
  28. \code
  29. cout << as::percent << 0.25 <<endl;
  30. \endcode
  31. Would create an output that may look like this:
  32. \verbatim
  33. 25%
  34. \endverbatim
  35. \n
  36. - \c as::spellout -- spell the number. For example, under the English locale, 103 may be displayed as "one hundred three".
  37. \b Note: not all locales provide rules for spelling numbers. In such a case the number would be displayed in decimal format.
  38. \n
  39. - \c as::ordinal -- display an order-of element. For example "2" would be displayed as "2nd" under the English locale. As in
  40. the above case, not all locales provide ordinal rules.
  41. \section currency_formatting Currency formatting
  42. These are the manipulators for currency formatting:
  43. - \c as::currency -- set the format to currency mode.
  44. - \c as::currency_iso -- change the currency format to international, like "USD" instead of "$". This flag is supported
  45. when using ICU 4.2 and above.
  46. - \c as::currency_national -- change currency format to national, like "$".
  47. - \c as::currency_default -- return to the default (national) currency format.
  48. \note \c as::currency_XYZ manipulators have no effect on general formatting, only on the currency format. You must use both currency
  49. and number manipulators to use a non-default format.
  50. \section date_and_time_formatting Date and Time formatting
  51. Dates and times are represented as POSIX time. When date-time formatting is turned on in the \c iostream, each number is treated as a
  52. POSIX time. The number may be an integer or a double.
  53. There are four major manipulators for Date and Time formatting:
  54. - \c as::date -- date only
  55. - \c as::time -- time only
  56. - \c as::datetime -- both date and time
  57. - \c as::ftime -- parameterized manipulator that allows specification of time in the format that is used in the \c strftime function.
  58. \b Note: not all formatting flags of \c strftime are supported.
  59. For example:
  60. \code
  61. time_t now=time(0);
  62. cout << "Today is "<< as::date << now << " and tomorrow is " << now+24*3600 << endl;
  63. cout << "Current time is "<< as::time << now << endl;
  64. cout << "The current weekday is "<< as::ftime("%A") << now << endl;
  65. \endcode
  66. More fine-grained control of date-time formatting is also available:
  67. - \c as::time_default , \c as::time_short , \c as::time_medium , \c as::time_long , \c as::time_full -- change time formatting.
  68. - \c as::date_default , \c as::date_short , \c as::date_medium , \c as::date_long , \c as::date_full -- change date formatting.
  69. These manipulators, when used together with the \c as::date, \c as::time, or \c as::datetime manipulators, change the date-time representation.
  70. The default format is medium.
  71. By default, the date and time are shown in the local time zone. This behavior may be changed with the following manipulators:
  72. - \c as::gmt -- display date and time in GMT.
  73. - \c as::local_time -- display in local time zone (default).
  74. - \c as::time_zone -- parameterized manipulator that sets the time-zone ID for date-time formatting and parsing. It
  75. takes a string parameter that represents the time zone ID.
  76. For example:
  77. \code
  78. double now=time(0);
  79. cout << as::datetime << as::local_time << "Local time is: "<< now << endl;
  80. cout << as::gmt << "GMT Time is: "<< now <<endl;
  81. cout << as::time_zone("EST") << "Eastern Standard Time is: "<< now <<endl;
  82. \endcode
  83. There is a list of supported \c strftime flags by ICU backend:
  84. - \c \%a -- Abbreviated weekday (Sun.)
  85. - \c \%A -- Full weekday (Sunday)
  86. - \c \%b -- Abbreviated month (Jan.)
  87. - \c \%B -- Full month (January)
  88. - \c \%c -- Locale date-time format. \b Note: prefer using \c as::datetime
  89. - \c \%d -- Day of Month [01,31]
  90. - \c \%e -- Day of Month [1,31]
  91. - \c \%h -- Same as \c \%b
  92. - \c \%H -- 24 clock hour [00,23]
  93. - \c \%I -- 12 clock hour [01,12]
  94. - \c \%j -- Day of year [1,366]
  95. - \c \%m -- Month [01,12]
  96. - \c \%M -- Minute [00,59]
  97. - \c \%n -- New Line
  98. - \c \%p -- AM/PM in locale representation
  99. - \c \%r -- Time with AM/PM, same as \c \%I:\%M:\%S \%p
  100. - \c \%R -- Same as \c \%H:\%M
  101. - \c \%S -- Second [00,61]
  102. - \c \%t -- Tab character
  103. - \c \%T -- Same as \c \%H:\%M:\%S
  104. - \c \%x -- Local date representation. **Note:** prefer using \c as::date
  105. - \c \%X -- Local time representation. **Note:** prefer using \c as::time
  106. - \c \%y -- Year [00,99]
  107. - \c \%Y -- 4 digits year. (2009)
  108. - \c \%Z -- Time Zone
  109. - \c \%\% -- Percent symbol
  110. Unsupported \c strftime flags are: \c \%C , \c \%u , \c \%U , \c \%V , \c \%w , \c \%W . Also, the \c O and \c E modifiers are not supported.
  111. \b General \b recommendations
  112. - Prefer using generic date-time manipulators rather than specifying the full format using \c as::ftime.
  113. - Remember that current calendars may be not Gregorian.
  114. \section formatting_internals Internals
  115. Formatting information is stored in a stream class by using the \c xalloc, \c pword, and \c register_callback member functions
  116. of \c std::ios_base . All the information is stored and managed using a special object bound to \c iostream, and the manipulators just
  117. change its state.
  118. When a number is written to or read from the stream, a custom Boost.Locale facet accesses the object and checks the required formatting
  119. information. Then it creates a special object that actually formats the number and caches it in the \c iostream. The
  120. next time a number is written to the stream, the same formatter would be used unless some flags had changed and formatter object is
  121. invalid.
  122. */