File: localization.cpp

package info (click to toggle)
boost 1.32.0-6
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 93,952 kB
  • ctags: 128,458
  • sloc: cpp: 492,477; xml: 52,125; python: 13,519; ansic: 13,013; sh: 1,773; yacc: 853; makefile: 526; perl: 418; lex: 110; csh: 6
file content (148 lines) | stat: -rw-r--r-- 5,184 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/* The following shows the creation of a facet for the output of 
 * dates in German (please forgive me for any errors in my German --
 * I'm not a native speaker).
 */

#include "boost/date_time/gregorian/gregorian.hpp"
#include <iostream>

/* Define a series of char arrays for short and long name strings to be 
 * associated with date output. */
const char* const de_short_month_names[] = 
{
  "Jan", "Feb", "Mar", "Apr", "Mai", "Jun",
  "Jul", "Aug", "Sep", "Okt", "Nov", "Dez", "NAM" 
};
const char* const de_long_month_names[] =
{
  "Januar", "Februar", "Marz", "April", "Mai",
  "Juni", "Juli", "August", "September", "Oktober",
  "November", "Dezember", "NichtDerMonat"
};
const char* const de_special_value_names[] =
{
  "NichtDatumzeit", "-unbegrenztheit", "+unbegrenztheit"
};
const char* const de_long_weekday_names[] = 
{
  "Sonntag", "Montag", "Dienstag", "Mittwoch",
  "Donnerstag", "Freitag", "Samstag"
};
const char* const de_short_weekday_names[] =
{
  "Son", "Mon", "Die","Mit", "Don", "Fre", "Sam"
};

const char* const us_short_month_names[] = 
{
  "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", "NAD"
};
const char* const us_long_month_names[] =
{
  "January", "February", "March", "April", "May",
  "June", "July", "August", "September", "October",
  "November", "December", "Not-A-Date"
};
const char* const us_special_value_names[] =
{ 
  "Not-A-Date", "-infinity", "+infinity"
};
const char* const us_long_weekday_names[] =
{
  "Sunday", "Monday", "Tuesday", "Wenesday", 
  "Thursday", "Friday", "Saturday"
};
const char* const us_short_weekday_names[] =
{
  "Sun", "Mon", "Tue","Wed", "Thu", "Fri", "Sat"
};


int
main() 
{

#ifndef BOOST_DATE_TIME_NO_LOCALE

  using namespace boost::gregorian;
  typedef boost::date_time::all_date_names_put<greg_facet_config> date_facet;
  
  //create a new local
  std::locale default_locale;
  std::locale german_dates1(default_locale, 
                            new date_facet(de_short_month_names, 
                                           de_long_month_names,
                                           de_special_value_names,
                                           de_short_weekday_names,
                                           de_long_weekday_names,
                                           '.',
                                           boost::date_time::ymd_order_dmy,
                                           boost::date_time::month_as_integer));
  
  date d1(2002, Oct, 1);
  std::cout.imbue(german_dates1); 
  // output the date in German using short month names
  std::cout << d1 << std::endl; //01.10.2002
  
  std::locale german_dates2(default_locale, 
                            new date_facet(de_short_month_names, 
                                           de_long_month_names,
                                           de_special_value_names,
                                           de_short_weekday_names,
                                           de_long_weekday_names,
                                           '.',
                                           boost::date_time::ymd_order_dmy,
                                           boost::date_time::month_as_long_string));
  
  std::cout.imbue(german_dates2); 
  greg_month m = d1.month();
  std::cout << m << std::endl; //Oktober
  
  greg_weekday wd = d1.day_of_week();
  std::cout << wd << std::endl; //Dienstag


  //Numeric date format with US month/day/year ordering
  std::locale usa_dates1(default_locale, 
                         new date_facet(us_short_month_names, 
                                        us_long_month_names,
                                        us_special_value_names,
                                        us_short_weekday_names,
                                        us_long_weekday_names,
                                        '/',
                                        boost::date_time::ymd_order_us,
                                        boost::date_time::month_as_integer));
  
  std::cout.imbue(usa_dates1); 
  std::cout << d1 << std::endl; //  10/01/2002
  //English names, iso order (year-month-day), '-' separator
  std::locale usa_dates2(default_locale, 
                         new date_facet(us_short_month_names, 
                                        us_long_month_names,
                                        us_special_value_names,
                                        us_short_weekday_names,
                                        us_long_weekday_names,
                                        '-',
                                        boost::date_time::ymd_order_iso,
                                        boost::date_time::month_as_short_string));

  std::cout.imbue(usa_dates2); 
  std::cout << d1 << std::endl; //  2002-Oct-01
  
  
#else 
  std::cout << "Sorry, localization is not supported by this compiler/library" 
            << std::endl;
#endif
  return 0;

}

/*  Copyright 2001-2004: CrystalClear Software, Inc
 *  http://www.crystalclearsoftware.com
 *
 *  Subject to the Boost Software License, Version 1.0.
 * (See accompanying file LICENSE-1.0 or http://www.boost.org/LICENSE-1.0)
 */