localization.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /* The following shows the creation of a facet for the output of
  2. * dates in German (please forgive me for any errors in my German --
  3. * I'm not a native speaker).
  4. */
  5. #include "boost/date_time/gregorian/gregorian.hpp"
  6. #include <iostream>
  7. #include <algorithm>
  8. /* Define a series of char arrays for short and long name strings
  9. * to be associated with German date output (US names will be
  10. * retrieved from the locale). */
  11. const char* const de_short_month_names[] =
  12. {
  13. "Jan", "Feb", "Mar", "Apr", "Mai", "Jun",
  14. "Jul", "Aug", "Sep", "Okt", "Nov", "Dez", "NAM"
  15. };
  16. const char* const de_long_month_names[] =
  17. {
  18. "Januar", "Februar", "Marz", "April", "Mai",
  19. "Juni", "Juli", "August", "September", "Oktober",
  20. "November", "Dezember", "NichtDerMonat"
  21. };
  22. const char* const de_long_weekday_names[] =
  23. {
  24. "Sonntag", "Montag", "Dienstag", "Mittwoch",
  25. "Donnerstag", "Freitag", "Samstag"
  26. };
  27. const char* const de_short_weekday_names[] =
  28. {
  29. "Son", "Mon", "Die","Mit", "Don", "Fre", "Sam"
  30. };
  31. int main()
  32. {
  33. using namespace boost::gregorian;
  34. // create some gregorian objects to output
  35. date d1(2002, Oct, 1);
  36. greg_month m = d1.month();
  37. greg_weekday wd = d1.day_of_week();
  38. // create a facet and a locale for German dates
  39. date_facet* german_facet = new date_facet();
  40. std::cout.imbue(std::locale(std::locale::classic(), german_facet));
  41. // create the German name collections
  42. date_facet::input_collection_type short_months, long_months,
  43. short_weekdays, long_weekdays;
  44. std::copy(&de_short_month_names[0], &de_short_month_names[11],
  45. std::back_inserter(short_months));
  46. std::copy(&de_long_month_names[0], &de_long_month_names[11],
  47. std::back_inserter(long_months));
  48. std::copy(&de_short_weekday_names[0], &de_short_weekday_names[6],
  49. std::back_inserter(short_weekdays));
  50. std::copy(&de_long_weekday_names[0], &de_long_weekday_names[6],
  51. std::back_inserter(long_weekdays));
  52. // replace the default names with ours
  53. // NOTE: date_generators and special_values were not replaced as
  54. // they are not used in this example
  55. german_facet->short_month_names(short_months);
  56. german_facet->long_month_names(long_months);
  57. german_facet->short_weekday_names(short_weekdays);
  58. german_facet->long_weekday_names(long_weekdays);
  59. // output the date in German using short month names
  60. german_facet->format("%d.%m.%Y");
  61. std::cout << d1 << std::endl; //01.10.2002
  62. german_facet->month_format("%B");
  63. std::cout << m << std::endl; //Oktober
  64. german_facet->weekday_format("%A");
  65. std::cout << wd << std::endl; //Dienstag
  66. // Output the same gregorian objects using US names
  67. date_facet* us_facet = new date_facet();
  68. std::cout.imbue(std::locale(std::locale::classic(), us_facet));
  69. us_facet->format("%m/%d/%Y");
  70. std::cout << d1 << std::endl; // 10/01/2002
  71. // English names, iso order (year-month-day), '-' separator
  72. us_facet->format("%Y-%b-%d");
  73. std::cout << d1 << std::endl; // 2002-Oct-01
  74. return 0;
  75. }
  76. /* Copyright 2001-2005: CrystalClear Software, Inc
  77. * http://www.crystalclearsoftware.com
  78. *
  79. * Subject to the Boost Software License, Version 1.0.
  80. * (See accompanying file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
  81. */