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