time_point_output.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. // Copyright 2011 Vicente J. Botet Escriba
  2. // Copyright (c) Microsoft Corporation 2014
  3. // Distributed under the Boost Software License, Version 1.0.
  4. // See http://www.boost.org/LICENSE_1_0.txt
  5. #include <boost/chrono/chrono_io.hpp>
  6. #include <sstream>
  7. #include <boost/detail/lightweight_test.hpp>
  8. #include <boost/chrono/system_clocks.hpp>
  9. #include <boost/chrono/thread_clock.hpp>
  10. #include <boost/chrono/process_cpu_clocks.hpp>
  11. #include <locale>
  12. #include <ctime>
  13. #include <cstdio>
  14. template <typename Clock, typename D>
  15. void test_good_prefix(const char* str, D d)
  16. {
  17. std::ostringstream out;
  18. boost::chrono::time_point<Clock, D> tp(d);
  19. out << tp;
  20. BOOST_TEST(out.good());
  21. //std::cout << "Expected= " << std::string(str) + boost::chrono::clock_string<Clock, char>::since() << std::endl;
  22. //std::cout << "Obtained= " << out.str() << std::endl;
  23. BOOST_TEST( (out.str() == std::string(str) + boost::chrono::clock_string<Clock, char>::since()));
  24. }
  25. template <typename D>
  26. void test_good_prefix_system_clock(const char* str, D d)
  27. {
  28. typedef boost::chrono::system_clock Clock;
  29. std::ostringstream out;
  30. boost::chrono::time_point<Clock, D> tp(d);
  31. out << tp;
  32. BOOST_TEST(out.good());
  33. std::cout << "Expected= " << str << std::endl;
  34. std::cout << "Obtained= " << out.str() << std::endl;
  35. BOOST_TEST( (out.str() == std::string(str) ));
  36. }
  37. template <typename Clock, typename D>
  38. void test_good_symbol(const char* str, D d)
  39. {
  40. std::ostringstream out;
  41. boost::chrono::time_point<Clock, D> tp(d);
  42. #if BOOST_CHRONO_VERSION>=2
  43. out << boost::chrono::duration_fmt(boost::chrono::duration_style::symbol) << tp;
  44. #else
  45. out << boost::chrono::duration_short << tp;
  46. #endif
  47. BOOST_TEST(out.good());
  48. BOOST_TEST( (out.str() == std::string(str) + boost::chrono::clock_string<Clock, char>::since()));
  49. }
  50. #if BOOST_CHRONO_VERSION>=2
  51. template <typename D>
  52. void test_good_symbol_system_clock(const char* str, D d)
  53. {
  54. typedef boost::chrono::system_clock Clock;
  55. std::ostringstream out;
  56. boost::chrono::time_point<Clock, D> tp(d);
  57. out << boost::chrono::duration_fmt(boost::chrono::duration_style::symbol) << tp;
  58. BOOST_TEST(out.good());
  59. std::cout << "Expected= " << str << std::endl;
  60. std::cout << "Obtained= " << out.str() << std::endl;
  61. BOOST_TEST( (out.str() == std::string(str) ));
  62. }
  63. template <typename D>
  64. void test_good_utc_fmt_system_clock(const char* str, const char* fmt, D d)
  65. {
  66. typedef boost::chrono::system_clock Clock;
  67. std::ostringstream out;
  68. boost::chrono::time_point<Clock, D> tp(d);
  69. boost::chrono::time_fmt_io_saver<> fmts(out);
  70. boost::chrono::timezone_io_saver tzs(out);
  71. out << time_fmt(boost::chrono::timezone::utc, fmt) << tp;
  72. BOOST_TEST(out.good());
  73. std::cout << "Expected= " << str << std::endl;
  74. std::cout << "Obtained= " << out.str() << std::endl;
  75. BOOST_TEST_EQ( out.str() , std::string(str) );
  76. }
  77. template <typename D>
  78. void test_good_utc_fmt_system_clock2(const char* str, const char* fmt, D d)
  79. {
  80. typedef boost::chrono::system_clock Clock;
  81. std::ostringstream out;
  82. boost::chrono::time_point<Clock, D> tp(d);
  83. boost::chrono::time_fmt_io_saver<> fmts(out, fmt);
  84. boost::chrono::timezone_io_saver tzs(out, boost::chrono::timezone::utc);
  85. out << tp;
  86. BOOST_TEST(out.good());
  87. std::cout << "Expected= " << str << std::endl;
  88. std::cout << "Obtained= " << out.str() << std::endl;
  89. BOOST_TEST_EQ( out.str() , std::string(str) );
  90. }
  91. template<typename Clock, typename D>
  92. void test_good(const char* str, D d, boost::chrono::duration_style style)
  93. {
  94. std::ostringstream out;
  95. boost::chrono::time_point<Clock,D> tp(d);
  96. out << boost::chrono::duration_fmt(style) << tp;
  97. BOOST_TEST(out.good());
  98. BOOST_TEST((out.str() == std::string(str)+boost::chrono::clock_string<Clock,char>::since()));
  99. }
  100. template<typename D>
  101. void test_good_system_clock(const char* str, D d, boost::chrono::duration_style style)
  102. {
  103. typedef boost::chrono::system_clock Clock;
  104. std::ostringstream out;
  105. boost::chrono::time_point<Clock,D> tp(d);
  106. out << boost::chrono::duration_fmt(style) << tp;
  107. BOOST_TEST(out.good());
  108. std::cout << "Expected= " << str << std::endl;
  109. std::cout << "Obtained= " << out.str() << std::endl;
  110. BOOST_TEST((out.str() == std::string(str) ));
  111. }
  112. #endif
  113. template <typename Clock>
  114. void check_all()
  115. {
  116. using namespace boost::chrono;
  117. using namespace boost;
  118. #if BOOST_CHRONO_VERSION>=2
  119. test_good<Clock>("2 hours", hours(2), duration_style::prefix);
  120. test_good<Clock>("2 h", hours(2), duration_style::symbol);
  121. #endif
  122. test_good_prefix<Clock> ("2 hours", hours(2));
  123. test_good_prefix<Clock> ("2 minutes", minutes(2));
  124. test_good_prefix<Clock> ("2 seconds", seconds(2));
  125. test_good_prefix<Clock> ("1 second", seconds(1));
  126. test_good_prefix<Clock> ("-1 second", seconds(-1));
  127. test_good_prefix<Clock> ("0 seconds", seconds(0));
  128. test_good_prefix<Clock> ("2 milliseconds", milliseconds(2));
  129. test_good_prefix<Clock> ("2 microseconds", microseconds(2));
  130. test_good_prefix<Clock> ("2 nanoseconds", nanoseconds(2));
  131. test_good_prefix<Clock> ("2 deciseconds", duration<boost::int_least64_t, deci> (2));
  132. test_good_prefix<Clock> ("2 [1/30]seconds", duration<boost::int_least64_t, ratio<1, 30> > (2));
  133. test_good_symbol<Clock> ("2 h", hours(2));
  134. #if BOOST_CHRONO_VERSION>=2
  135. test_good_symbol<Clock>("2 min", minutes(2));
  136. #else
  137. test_good_symbol<Clock> ("2 m", minutes(2));
  138. #endif
  139. test_good_symbol<Clock> ("2 s", seconds(2));
  140. test_good_symbol<Clock> ("2 ms", milliseconds(2));
  141. test_good_symbol<Clock> ("2 ns", nanoseconds(2));
  142. test_good_symbol<Clock> ("2 ds", duration<boost::int_least64_t, deci> (2));
  143. test_good_symbol<Clock> ("2 [1/30]s", duration<boost::int_least64_t, ratio<1, 30> > (2));
  144. }
  145. #if BOOST_CHRONO_VERSION >= 2
  146. void check_all_system_clock()
  147. {
  148. using namespace boost::chrono;
  149. using namespace boost;
  150. test_good_system_clock("1970-01-01 02:00:00.000000000 +0000", hours(2), duration_style::prefix);
  151. test_good_system_clock("1970-01-01 02:00:00.000000000 +0000", hours(2), duration_style::symbol);
  152. test_good_prefix_system_clock("1970-01-01 02:00:00.000000000 +0000", hours(2));
  153. test_good_prefix_system_clock("1970-01-01 00:02:00.000000000 +0000", minutes(2));
  154. test_good_prefix_system_clock("1970-01-01 00:00:02.000000000 +0000", seconds(2));
  155. test_good_prefix_system_clock("1970-01-01 00:00:01.000000000 +0000", seconds(1));
  156. test_good_prefix_system_clock("1969-12-31 23:59:59.000000000 +0000", seconds(-1));
  157. test_good_prefix_system_clock("1970-01-01 00:00:00.000000000 +0000", seconds(0));
  158. test_good_prefix_system_clock("1970-01-01 00:00:00.002000000 +0000", milliseconds(2));
  159. test_good_prefix_system_clock("1970-01-01 00:00:00.000002000 +0000", microseconds(2));
  160. test_good_prefix_system_clock("1970-01-01 00:00:00.000000002 +0000", nanoseconds(2));
  161. test_good_prefix_system_clock("1970-01-01 00:00:00.200000000 +0000", duration<boost::int_least64_t, deci> (2));
  162. test_good_prefix_system_clock("1970-01-01 00:00:00.066666667 +0000", duration<boost::int_least64_t, ratio<1, 30> > (2));
  163. test_good_symbol_system_clock("1970-01-01 02:00:00.000000000 +0000", hours(2));
  164. test_good_symbol_system_clock("1970-01-01 00:02:00.000000000 +0000", minutes(2));
  165. test_good_symbol_system_clock("1970-01-01 00:00:02.000000000 +0000", seconds(2));
  166. test_good_symbol_system_clock("1970-01-01 00:00:00.002000000 +0000", milliseconds(2));
  167. test_good_symbol_system_clock("1970-01-01 00:00:00.000000002 +0000", nanoseconds(2));
  168. test_good_symbol_system_clock("1970-01-01 00:00:00.200000000 +0000", duration<boost::int_least64_t, deci> (2));
  169. test_good_symbol_system_clock("1970-01-01 00:00:00.066666667 +0000", duration<boost::int_least64_t, ratio<1, 30> > (2));
  170. test_good_utc_fmt_system_clock("1970-01-01 02:00:00", "%Y-%m-%d %H:%M:%S", hours(2));
  171. test_good_utc_fmt_system_clock("1970-01-01 02", "%Y-%m-%d %H", hours(2));
  172. #if ! defined(BOOST_CHRONO_WINDOWS_API)
  173. test_good_utc_fmt_system_clock ("1970-01-01 02:00:00", "%Y-%m-%d %T", hours(2));
  174. test_good_utc_fmt_system_clock ("1970-01-01 02:00", "%Y-%m-%d %R", hours(2));
  175. test_good_utc_fmt_system_clock ("% 1970-01-01 02:00", "%% %Y-%m-%d %R", hours(2));
  176. test_good_utc_fmt_system_clock ("1970-01-01 02:00 Thursday January", "%Y-%m-%d %R %A %B", hours(2));
  177. #endif
  178. test_good_utc_fmt_system_clock2("1970-01-01 02:00:00", "%Y-%m-%d %H:%M:%S", hours(2));
  179. test_good_utc_fmt_system_clock2("1970-01-01 02", "%Y-%m-%d %H", hours(2));
  180. #if ! defined(BOOST_CHRONO_WINDOWS_API)
  181. test_good_utc_fmt_system_clock2 ("1970-01-01 02:00:00", "%Y-%m-%d %T", hours(2));
  182. test_good_utc_fmt_system_clock2 ("1970-01-01 02:00", "%Y-%m-%d %R", hours(2));
  183. test_good_utc_fmt_system_clock2 ("% 1970-01-01 02:00", "%% %Y-%m-%d %R", hours(2));
  184. test_good_utc_fmt_system_clock2 ("1970-01-01 02:00 Thursday January", "%Y-%m-%d %R %A %B", hours(2));
  185. #endif
  186. }
  187. #endif
  188. #if defined BOOST_CHRONO_INTERNAL_GMTIME
  189. #elif BOOST_CHRONO_VERSION == 2
  190. void test_gmtime(std::time_t t)
  191. {
  192. std::cout << "t " << t << std::endl;
  193. std::puts(ctime(&t));
  194. std::tm tm;
  195. std::memset(&tm, 0, sizeof(std::tm));
  196. if (boost::chrono::detail::internal_gmtime(&t, &tm))
  197. {
  198. tm.tm_isdst = -1;
  199. (void)mktime(&tm);
  200. std::tm tm2;
  201. std::memset(&tm2, 0, sizeof(std::tm));
  202. if (gmtime_r(&t, &tm2))
  203. {
  204. tm2.tm_isdst = -1;
  205. (void)mktime(&tm2);
  206. BOOST_TEST_EQ( tm.tm_year , tm2.tm_year );
  207. BOOST_TEST_EQ( tm.tm_mon , tm2.tm_mon );
  208. BOOST_TEST_EQ( tm.tm_mday , tm2.tm_mday );
  209. BOOST_TEST_EQ( tm.tm_hour , tm2.tm_hour);
  210. BOOST_TEST_EQ( tm.tm_min , tm2.tm_min );
  211. BOOST_TEST_EQ( tm.tm_sec , tm2.tm_sec );
  212. BOOST_TEST_EQ( tm.tm_wday , tm2.tm_wday );
  213. BOOST_TEST_EQ( tm.tm_yday , tm2.tm_yday );
  214. BOOST_TEST_EQ( tm.tm_isdst , tm2.tm_isdst );
  215. }
  216. }
  217. }
  218. #endif
  219. int main()
  220. {
  221. #if defined BOOST_CHRONO_INTERNAL_GMTIME
  222. #elif BOOST_CHRONO_VERSION == 2
  223. test_gmtime( 0 );
  224. test_gmtime( -1 );
  225. test_gmtime( +1 );
  226. test_gmtime( 0 - (3600 * 24) );
  227. test_gmtime( -1 - (3600 * 24) );
  228. test_gmtime( +1 - (3600 * 24) );
  229. test_gmtime( 0 + (3600 * 24) );
  230. test_gmtime( -1 + (3600 * 24) );
  231. test_gmtime( +1 + (3600 * 24) );
  232. test_gmtime( 0 + 365*(3600 * 24) );
  233. test_gmtime( 0 + 10LL*365*(3600 * 24) );
  234. test_gmtime( 0 + 15LL*365*(3600 * 24) );
  235. test_gmtime( 0 + 17LL*365*(3600 * 24) );
  236. test_gmtime( 0 + 18LL*365*(3600 * 24) );
  237. test_gmtime( 0 + 19LL*365*(3600 * 24) );
  238. test_gmtime( 0 + 19LL*365*(3600 * 24)+ (3600 * 24));
  239. test_gmtime( 0 + 19LL*365*(3600 * 24)+ 3*(3600 * 24));
  240. test_gmtime( 0 + 19LL*365*(3600 * 24)+ 4*(3600 * 24));
  241. test_gmtime( 0 + 20LL*365*(3600 * 24) );
  242. test_gmtime( 0 + 40LL*365*(3600 * 24) );
  243. #endif
  244. std::cout << "high_resolution_clock=" << std::endl;
  245. check_all<boost::chrono::high_resolution_clock> ();
  246. #ifdef BOOST_CHRONO_HAS_CLOCK_STEADY
  247. std::cout << "steady_clock=" << std::endl;
  248. check_all<boost::chrono::steady_clock> ();
  249. #endif
  250. std::cout << "system_clock=" << std::endl;
  251. #if BOOST_CHRONO_VERSION >= 2 && defined BOOST_CHRONO_PROVIDES_DATE_IO_FOR_SYSTEM_CLOCK_TIME_POINT
  252. check_all_system_clock();
  253. #else
  254. check_all<boost::chrono::system_clock> ();
  255. #endif
  256. #if defined(BOOST_CHRONO_HAS_THREAD_CLOCK)
  257. std::cout << "thread_clock="<< std::endl;
  258. check_all<boost::chrono::thread_clock>();
  259. #endif
  260. #if defined(BOOST_CHRONO_HAS_PROCESS_CLOCKS)
  261. std::cout << "process_real_cpu_clock=" << std::endl;
  262. check_all<boost::chrono::process_real_cpu_clock> ();
  263. #if ! BOOST_OS_WINDOWS || BOOST_PLAT_WINDOWS_DESKTOP
  264. std::cout << "process_user_cpu_clock=" << std::endl;
  265. check_all<boost::chrono::process_user_cpu_clock> ();
  266. std::cout << "process_system_cpu_clock=" << std::endl;
  267. check_all<boost::chrono::process_system_cpu_clock> ();
  268. std::cout << "process_cpu_clock=" << std::endl;
  269. check_all<boost::chrono::process_cpu_clock> ();
  270. #endif
  271. #endif
  272. #if defined BOOST_CHRONO_INTERNAL_GMTIME
  273. #elif BOOST_CHRONO_VERSION == 2
  274. boost::chrono::system_clock::time_point tp = boost::chrono::system_clock::now();
  275. std::cout << tp << std::endl;
  276. time_t t = boost::chrono::system_clock::to_time_t(tp);
  277. test_gmtime( t );
  278. #endif
  279. return boost::report_errors();
  280. }