period_formatter.hpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. #ifndef DATETIME_PERIOD_FORMATTER_HPP___
  2. #define DATETIME_PERIOD_FORMATTER_HPP___
  3. /* Copyright (c) 2002-2004 CrystalClear Software, Inc.
  4. * Use, modification and distribution is subject to the
  5. * Boost Software License, Version 1.0. (See accompanying
  6. * file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
  7. * Author: Jeff Garland, Bart Garst
  8. * $Date$
  9. */
  10. #include <iosfwd>
  11. #include <string>
  12. #include <vector>
  13. #include <iterator>
  14. namespace boost { namespace date_time {
  15. //! Not a facet, but a class used to specify and control period formats
  16. /*! Provides settings for the following:
  17. * - period_separator -- default '/'
  18. * - period_open_start_delimeter -- default '['
  19. * - period_open_range_end_delimeter -- default ')'
  20. * - period_closed_range_end_delimeter -- default ']'
  21. * - display_as_open_range, display_as_closed_range -- default closed_range
  22. *
  23. * Thus the default formatting for a period is as follows:
  24. *@code
  25. * [period.start()/period.last()]
  26. *@endcode
  27. * So for a typical date_period this would be
  28. *@code
  29. * [2004-Jan-04/2004-Feb-01]
  30. *@endcode
  31. * where the date formatting is controlled by the date facet
  32. */
  33. template <class CharT, class OutItrT = std::ostreambuf_iterator<CharT, std::char_traits<CharT> > >
  34. class period_formatter {
  35. public:
  36. typedef std::basic_string<CharT> string_type;
  37. typedef CharT char_type;
  38. typedef typename std::basic_string<char_type>::const_iterator const_itr_type;
  39. typedef std::vector<std::basic_string<CharT> > collection_type;
  40. static const char_type default_period_separator[2];
  41. static const char_type default_period_start_delimeter[2];
  42. static const char_type default_period_open_range_end_delimeter[2];
  43. static const char_type default_period_closed_range_end_delimeter[2];
  44. enum range_display_options { AS_OPEN_RANGE, AS_CLOSED_RANGE };
  45. //! Constructor that sets up period formatter options -- default should suffice most cases.
  46. period_formatter(range_display_options range_option_in = AS_CLOSED_RANGE,
  47. const char_type* const period_separator = default_period_separator,
  48. const char_type* const period_start_delimeter = default_period_start_delimeter,
  49. const char_type* const period_open_range_end_delimeter = default_period_open_range_end_delimeter,
  50. const char_type* const period_closed_range_end_delimeter = default_period_closed_range_end_delimeter) :
  51. m_range_option(range_option_in),
  52. m_period_separator(period_separator),
  53. m_period_start_delimeter(period_start_delimeter),
  54. m_open_range_end_delimeter(period_open_range_end_delimeter),
  55. m_closed_range_end_delimeter(period_closed_range_end_delimeter)
  56. {}
  57. //! Puts the characters between period elements into stream -- default is /
  58. OutItrT put_period_separator(OutItrT& oitr) const
  59. {
  60. const_itr_type ci = m_period_separator.begin();
  61. while (ci != m_period_separator.end()) {
  62. *oitr = *ci;
  63. ci++;
  64. }
  65. return oitr;
  66. }
  67. //! Puts the period start characters into stream -- default is [
  68. OutItrT put_period_start_delimeter(OutItrT& oitr) const
  69. {
  70. const_itr_type ci = m_period_start_delimeter.begin();
  71. while (ci != m_period_start_delimeter.end()) {
  72. *oitr = *ci;
  73. ci++;
  74. }
  75. return oitr;
  76. }
  77. //! Puts the period end characters into stream as controled by open/closed range setting.
  78. OutItrT put_period_end_delimeter(OutItrT& oitr) const
  79. {
  80. const_itr_type ci, end;
  81. if (m_range_option == AS_OPEN_RANGE) {
  82. ci = m_open_range_end_delimeter.begin();
  83. end = m_open_range_end_delimeter.end();
  84. }
  85. else {
  86. ci = m_closed_range_end_delimeter.begin();
  87. end = m_closed_range_end_delimeter.end();
  88. }
  89. while (ci != end) {
  90. *oitr = *ci;
  91. ci++;
  92. }
  93. return oitr;
  94. }
  95. range_display_options range_option() const
  96. {
  97. return m_range_option;
  98. }
  99. //! Reset the range_option control
  100. void
  101. range_option(range_display_options option) const
  102. {
  103. m_range_option = option;
  104. }
  105. //! Change the delimiter strings
  106. void delimiter_strings(const string_type& separator,
  107. const string_type& start_delim,
  108. const string_type& open_end_delim,
  109. const string_type& closed_end_delim)
  110. {
  111. m_period_separator = separator;
  112. m_period_start_delimeter = start_delim;
  113. m_open_range_end_delimeter = open_end_delim;
  114. m_closed_range_end_delimeter = closed_end_delim;
  115. }
  116. //! Generic code to output a period -- no matter the period type.
  117. /*! This generic code will output any period using a facet to
  118. * to output the 'elements'. For example, in the case of a date_period
  119. * the elements will be instances of a date which will be formatted
  120. * according the to setup in the passed facet parameter.
  121. *
  122. * The steps for formatting a period are always the same:
  123. * - put the start delimiter
  124. * - put start element
  125. * - put the separator
  126. * - put either last or end element depending on range settings
  127. * - put end delimeter depending on range settings
  128. *
  129. * Thus for a typical date period the result might look like this:
  130. *@code
  131. *
  132. * [March 01, 2004/June 07, 2004] <-- closed range
  133. * [March 01, 2004/June 08, 2004) <-- open range
  134. *
  135. *@endcode
  136. */
  137. template<class period_type, class facet_type>
  138. OutItrT put_period(OutItrT next,
  139. std::ios_base& a_ios,
  140. char_type a_fill,
  141. const period_type& p,
  142. const facet_type& facet) const {
  143. put_period_start_delimeter(next);
  144. next = facet.put(next, a_ios, a_fill, p.begin());
  145. put_period_separator(next);
  146. if (m_range_option == AS_CLOSED_RANGE) {
  147. facet.put(next, a_ios, a_fill, p.last());
  148. }
  149. else {
  150. facet.put(next, a_ios, a_fill, p.end());
  151. }
  152. put_period_end_delimeter(next);
  153. return next;
  154. }
  155. private:
  156. range_display_options m_range_option;
  157. string_type m_period_separator;
  158. string_type m_period_start_delimeter;
  159. string_type m_open_range_end_delimeter;
  160. string_type m_closed_range_end_delimeter;
  161. };
  162. template <class CharT, class OutItrT>
  163. const typename period_formatter<CharT, OutItrT>::char_type
  164. period_formatter<CharT, OutItrT>::default_period_separator[2] = {'/'};
  165. template <class CharT, class OutItrT>
  166. const typename period_formatter<CharT, OutItrT>::char_type
  167. period_formatter<CharT, OutItrT>::default_period_start_delimeter[2] = {'['};
  168. template <class CharT, class OutItrT>
  169. const typename period_formatter<CharT, OutItrT>::char_type
  170. period_formatter<CharT, OutItrT>::default_period_open_range_end_delimeter[2] = {')'};
  171. template <class CharT, class OutItrT>
  172. const typename period_formatter<CharT, OutItrT>::char_type
  173. period_formatter<CharT, OutItrT>::default_period_closed_range_end_delimeter[2] = {']'};
  174. } } //namespace boost::date_time
  175. #endif