...one of the most highly
regarded and expertly designed C++ library projects in the
world.
— Herb Sutter and Andrei
Alexandrescu, C++
Coding Standards
Introduction -- Class Overview
The library provides for localized stream-based I/O of dates and times. Note that this support is not supported for all compilers. See the build information page for details.
To provide for date output (and eventually input) the library uses a specialized facet class that provides the month name strings (both short and long -- eg: Jan, January), order of the date output (year-month-day, day-month-year, etc), and the delimeters between various parts of the date. To localize the date the process entails constructing the facet with the desired parameters, calling imbue on the stream, and then streaming the date.
By default, the date operator << uses the following default strings:
us_short_month_names={"Jan","Feb","Mar","Apr","May","Jun","Jul", "Aug","Sep","Oct","Nov","Dec", "NAD"}; us_long_month_names={"January","February","March","April","May","June","July", "August","September","October","November","December","Not-A-Date"}; us_special_value_names={"Not-A-Date","-infinity", "+infinity"}; us_long_weekday_names={"Sunday", "Monday", "Tuesday","Wenesday", "Thursday", "Friday", "Saturday"}; us_short_weekday_names={"Sun", "Mon", "Tue","Wed", "Thu", "Fri", "Sat"};
The following example shows how to output dates using German names first with short month names and then with long month names.
using namespace boost::gregorian; typedef boost::date_time::all_date_names_put<greg_facet_config> date_facet; 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"}; std::locale default_locale; //Create the date facet based on the default locale std::locale german_dates(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, '.', //separate parts using a '.' boost::date_time::ymd_order_ymd, boost::date_time::month_as_short_string)); // Imbue the stream with the locale std::cout.imbue(german_dates); date d1(2002, Oct, 1); // output the date in German using short month names std::cout << d1 << std::endl; //01.Okt.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, '-', //separate parts using a '-' boost::date_time::ymd_order_iso, boost::date_time::month_as_long_string)); std::cout.imbue(german_dates2); //use long month names std::cout << d1 << std::endl; //2002-Oktober-01
Class | Construction Parameters | Description |
date_facet | const char* const month_short_names[] const char* const month_long_names[] const char* const special_value_names[] const char* const weekday_short_names[] const char* const weekday_long_names[] char separator_char = '-' ymd_order_spec order_spec = ymd_order_iso month_format_spec month_format = month_as_short_string |
Constructor for date facet |