form_date_time.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. * Copyright Andrey Semashev 2007 - 2015.
  3. * Distributed under the Boost Software License, Version 1.0.
  4. * (See accompanying file LICENSE_1_0.txt or copy at
  5. * http://www.boost.org/LICENSE_1_0.txt)
  6. */
  7. /*!
  8. * \file form_date_time.cpp
  9. * \author Andrey Semashev
  10. * \date 07.02.2009
  11. *
  12. * \brief This header contains tests for the date and time formatters.
  13. */
  14. #define BOOST_TEST_MODULE form_date_time
  15. #include <memory>
  16. #include <locale>
  17. #include <string>
  18. #include <iomanip>
  19. #include <ostream>
  20. #include <algorithm>
  21. #include <boost/date_time.hpp>
  22. #include <boost/test/unit_test.hpp>
  23. #include <boost/log/expressions.hpp>
  24. #include <boost/log/attributes/constant.hpp>
  25. #include <boost/log/attributes/attribute_set.hpp>
  26. #include <boost/log/utility/string_literal.hpp>
  27. #include <boost/log/utility/formatting_ostream.hpp>
  28. #include <boost/log/core/record.hpp>
  29. #include <boost/log/support/date_time.hpp>
  30. #include "char_definitions.hpp"
  31. #include "make_record.hpp"
  32. namespace logging = boost::log;
  33. namespace attrs = logging::attributes;
  34. namespace expr = logging::expressions;
  35. namespace keywords = logging::keywords;
  36. typedef boost::posix_time::ptime ptime;
  37. typedef boost::gregorian::date gdate;
  38. typedef ptime::time_duration_type duration;
  39. namespace {
  40. template< typename CharT >
  41. struct date_time_formats;
  42. #ifdef BOOST_LOG_USE_CHAR
  43. template< >
  44. struct date_time_formats< char >
  45. {
  46. typedef logging::basic_string_literal< char > string_literal_type;
  47. static string_literal_type default_date_format() { return logging::str_literal("%Y-%b-%d"); }
  48. static string_literal_type default_time_format() { return logging::str_literal("%H:%M:%S.%f"); }
  49. static string_literal_type default_date_time_format() { return logging::str_literal("%Y-%b-%d %H:%M:%S.%f"); }
  50. static string_literal_type default_time_duration_format() { return logging::str_literal("%-%H:%M:%S.%f"); }
  51. static string_literal_type date_format() { return logging::str_literal("%d/%m/%Y"); }
  52. static string_literal_type time_format() { return logging::str_literal("%H.%M.%S"); }
  53. static string_literal_type date_time_format() { return logging::str_literal("%d/%m/%Y %H.%M.%S"); }
  54. static string_literal_type time_duration_format() { return logging::str_literal("%+%H.%M.%S.%f"); }
  55. };
  56. #endif // BOOST_LOG_USE_CHAR
  57. #ifdef BOOST_LOG_USE_WCHAR_T
  58. template< >
  59. struct date_time_formats< wchar_t >
  60. {
  61. typedef logging::basic_string_literal< wchar_t > string_literal_type;
  62. static string_literal_type default_date_format() { return logging::str_literal(L"%Y-%b-%d"); }
  63. static string_literal_type default_time_format() { return logging::str_literal(L"%H:%M:%S.%f"); }
  64. static string_literal_type default_date_time_format() { return logging::str_literal(L"%Y-%b-%d %H:%M:%S.%f"); }
  65. static string_literal_type default_time_duration_format() { return logging::str_literal(L"%-%H:%M:%S.%f"); }
  66. static string_literal_type date_format() { return logging::str_literal(L"%d/%m/%Y"); }
  67. static string_literal_type time_format() { return logging::str_literal(L"%H.%M.%S"); }
  68. static string_literal_type date_time_format() { return logging::str_literal(L"%d/%m/%Y %H.%M.%S"); }
  69. static string_literal_type time_duration_format() { return logging::str_literal(L"%+%H.%M.%S.%f"); }
  70. };
  71. #endif // BOOST_LOG_USE_WCHAR_T
  72. } // namespace
  73. // The test checks that date_time formatting work
  74. BOOST_AUTO_TEST_CASE_TEMPLATE(date_time, CharT, char_types)
  75. {
  76. typedef logging::attribute_set attr_set;
  77. typedef std::basic_string< CharT > string;
  78. typedef logging::basic_formatting_ostream< CharT > osstream;
  79. typedef logging::record_view record_view;
  80. typedef logging::basic_formatter< CharT > formatter;
  81. typedef test_data< CharT > data;
  82. typedef date_time_formats< CharT > formats;
  83. typedef boost::date_time::time_facet< ptime, CharT > facet;
  84. ptime t1(gdate(2009, 2, 7), ptime::time_duration_type(14, 40, 15));
  85. attrs::constant< ptime > attr1(t1);
  86. attr_set set1;
  87. set1[data::attr1()] = attr1;
  88. record_view rec = make_record_view(set1);
  89. // Check for various formats specification
  90. {
  91. string str1, str2;
  92. osstream strm1(str1), strm2(str2);
  93. formatter f = expr::stream << expr::format_date_time< ptime >(data::attr1(), formats::default_date_time_format().c_str());
  94. f(rec, strm1);
  95. strm2.imbue(std::locale(strm2.getloc(), new facet(formats::default_date_time_format().c_str())));
  96. strm2 << t1;
  97. BOOST_CHECK(equal_strings(strm1.str(), strm2.str()));
  98. }
  99. {
  100. string str1, str2;
  101. osstream strm1(str1), strm2(str2);
  102. formatter f = expr::stream << expr::format_date_time< ptime >(data::attr1(), formats::date_time_format().c_str());
  103. f(rec, strm1);
  104. strm2.imbue(std::locale(strm2.getloc(), new facet(formats::date_time_format().c_str())));
  105. strm2 << t1;
  106. BOOST_CHECK(equal_strings(strm1.str(), strm2.str()));
  107. }
  108. }
  109. // The test checks that date formatting work
  110. BOOST_AUTO_TEST_CASE_TEMPLATE(date, CharT, char_types)
  111. {
  112. typedef logging::attribute_set attr_set;
  113. typedef std::basic_string< CharT > string;
  114. typedef logging::basic_formatting_ostream< CharT > osstream;
  115. typedef logging::record_view record_view;
  116. typedef logging::basic_formatter< CharT > formatter;
  117. typedef test_data< CharT > data;
  118. typedef date_time_formats< CharT > formats;
  119. typedef boost::date_time::date_facet< gdate, CharT > facet;
  120. gdate d1(2009, 2, 7);
  121. attrs::constant< gdate > attr1(d1);
  122. attr_set set1;
  123. set1[data::attr1()] = attr1;
  124. record_view rec = make_record_view(set1);
  125. // Check for various formats specification
  126. {
  127. string str1, str2;
  128. osstream strm1(str1), strm2(str2);
  129. formatter f = expr::stream << expr::format_date_time< gdate >(data::attr1(), formats::default_date_format().c_str());
  130. f(rec, strm1);
  131. strm2.imbue(std::locale(strm2.getloc(), new facet(formats::default_date_format().c_str())));
  132. strm2 << d1;
  133. BOOST_CHECK(equal_strings(strm1.str(), strm2.str()));
  134. }
  135. {
  136. string str1, str2;
  137. osstream strm1(str1), strm2(str2);
  138. formatter f = expr::stream << expr::format_date_time< gdate >(data::attr1(), formats::date_format().c_str());
  139. f(rec, strm1);
  140. strm2.imbue(std::locale(strm2.getloc(), new facet(formats::date_format().c_str())));
  141. strm2 << d1;
  142. BOOST_CHECK(equal_strings(strm1.str(), strm2.str()));
  143. }
  144. }
  145. // The test checks that time_duration formatting work
  146. BOOST_AUTO_TEST_CASE_TEMPLATE(time_duration, CharT, char_types)
  147. {
  148. typedef logging::attribute_set attr_set;
  149. typedef std::basic_string< CharT > string;
  150. typedef logging::basic_formatting_ostream< CharT > osstream;
  151. typedef logging::record_view record_view;
  152. typedef logging::basic_formatter< CharT > formatter;
  153. typedef test_data< CharT > data;
  154. typedef date_time_formats< CharT > formats;
  155. typedef boost::date_time::time_facet< ptime, CharT > facet;
  156. duration t1(14, 40, 15);
  157. attrs::constant< duration > attr1(t1);
  158. attr_set set1;
  159. set1[data::attr1()] = attr1;
  160. record_view rec = make_record_view(set1);
  161. // Check for various formats specification
  162. {
  163. string str1, str2;
  164. osstream strm1(str1), strm2(str2);
  165. formatter f = expr::stream << expr::format_date_time< duration >(data::attr1(), formats::default_time_duration_format().c_str());
  166. f(rec, strm1);
  167. facet* fac = new facet();
  168. fac->time_duration_format(formats::default_time_duration_format().c_str());
  169. strm2.imbue(std::locale(strm2.getloc(), fac));
  170. strm2 << t1;
  171. BOOST_CHECK(equal_strings(strm1.str(), strm2.str()));
  172. }
  173. {
  174. string str1, str2;
  175. osstream strm1(str1), strm2(str2);
  176. formatter f = expr::stream << expr::format_date_time< duration >(data::attr1(), formats::time_duration_format().c_str());
  177. f(rec, strm1);
  178. facet* fac = new facet();
  179. fac->time_duration_format(formats::time_duration_format().c_str());
  180. strm2.imbue(std::locale(strm2.getloc(), fac));
  181. strm2 << t1;
  182. BOOST_CHECK(equal_strings(strm1.str(), strm2.str()));
  183. }
  184. }