french.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // french.cpp ----------------------------------------------------------//
  2. // Copyright 2010 Howard Hinnant
  3. // Copyright 2011 Vicente J. Botet Escriba
  4. // Distributed under the Boost Software License, Version 1.0.
  5. // See http://www.boost.org/LICENSE_1_0.txt
  6. // Adapted to Boost from the original Hawards's code
  7. #include <boost/chrono/config.hpp>
  8. #include <boost/chrono/chrono_io.hpp>
  9. #include <boost/chrono/process_cpu_clocks.hpp>
  10. #include <boost/chrono/thread_clock.hpp>
  11. #include <iostream>
  12. #include <locale>
  13. #if BOOST_CHRONO_VERSION==2
  14. #include <boost/chrono/io/duration_units.hpp>
  15. using namespace boost;
  16. using namespace boost::chrono;
  17. template <typename CharT=char>
  18. class duration_units_fr: public duration_units_default<CharT>
  19. {
  20. public:
  21. typedef CharT char_type;
  22. explicit duration_units_fr(size_t refs = 0) :
  23. duration_units_default<CharT>(refs)
  24. {
  25. }
  26. protected:
  27. using duration_units_default<CharT>::do_get_unit;
  28. std::size_t do_get_plural_form(boost::int_least64_t value) const
  29. {
  30. return (value == -1 || value == 0 || value == 1) ? 0 : 1;
  31. }
  32. std::basic_string<CharT> do_get_unit(duration_style style, ratio<1> , std::size_t pf) const
  33. {
  34. static const CharT t[] =
  35. { 's' };
  36. static const std::basic_string<CharT> symbol(t, t + sizeof (t) / sizeof (t[0]));
  37. static const CharT u[] =
  38. { 's', 'e', 'c', 'o', 'n', 'd', 'e' };
  39. static const std::basic_string<CharT> singular(u, u + sizeof (u) / sizeof (u[0]));
  40. static const CharT v[] =
  41. { 's', 'e', 'c', 'o', 'n', 'd', 'e', 's' };
  42. static const std::basic_string<CharT> plural(v, v + sizeof (v) / sizeof (v[0]));
  43. if (style == duration_style::symbol) return symbol;
  44. if (pf == 0) return singular;
  45. if (pf == 1) return plural;
  46. // assert
  47. //throw "exception";
  48. return "";
  49. }
  50. std::basic_string<CharT> do_get_unit(duration_style style, ratio<60> , std::size_t pf) const
  51. {
  52. static const CharT t[] =
  53. { 'm', 'i', 'n' };
  54. static const std::basic_string<CharT> symbol(t, t + sizeof (t) / sizeof (t[0]));
  55. static const CharT u[] =
  56. { 'm', 'i', 'n', 'u', 't', 'e' };
  57. static const std::basic_string<CharT> singular(u, u + sizeof (u) / sizeof (u[0]));
  58. static const CharT v[] =
  59. { 'm', 'i', 'n', 'u', 't', 'e', 's' };
  60. static const std::basic_string<CharT> plural(v, v + sizeof (v) / sizeof (v[0]));
  61. if (style == duration_style::symbol) return symbol;
  62. if (pf == 0) return singular;
  63. if (pf == 1) return plural;
  64. // assert
  65. //throw "exception";
  66. return "";
  67. }
  68. std::basic_string<CharT> do_get_unit(duration_style style, ratio<3600> , std::size_t pf) const
  69. {
  70. static const CharT t[] =
  71. { 'h' };
  72. static const std::basic_string<CharT> symbol(t, t + sizeof (t) / sizeof (t[0]));
  73. static const CharT u[] =
  74. { 'h', 'e', 'u', 'r', 'e' };
  75. static const std::basic_string<CharT> singular(u, u + sizeof (u) / sizeof (u[0]));
  76. static const CharT v[] =
  77. { 'h', 'e', 'u', 'r', 'e', 's' };
  78. static const std::basic_string<CharT> plural(v, v + sizeof (v) / sizeof (v[0]));
  79. if (style == duration_style::symbol) return symbol;
  80. if (pf == 0) return singular;
  81. if (pf == 1) return plural;
  82. // assert
  83. //throw "exception";
  84. return "";
  85. }
  86. };
  87. #endif
  88. int main()
  89. {
  90. using std::cout;
  91. using std::locale;
  92. using namespace boost;
  93. using namespace boost::chrono;
  94. #if BOOST_CHRONO_VERSION==2
  95. cout.imbue(locale(locale(), new duration_units_fr<>()));
  96. #else
  97. cout.imbue(locale(locale(), new duration_punct<char>
  98. (
  99. duration_punct<char>::use_long,
  100. "secondes", "minutes", "heures",
  101. "s", "m", "h"
  102. )));
  103. #endif
  104. hours h(5);
  105. minutes m(45);
  106. seconds s(15);
  107. milliseconds ms(763);
  108. cout << h << ", " << m << ", " << s << " et " << ms << '\n';
  109. cout << hours(0) << ", " << minutes(0) << ", " << s << " et " << ms << '\n';
  110. return 0;
  111. }