Boost C++ Libraries of the most highly regarded and expertly designed C++ library projects in the world. Herb Sutter and Andrei Alexandrescu, C++ Coding Standards

This is the documentation for an old version of boost. Click here for the latest Boost documentation.
C++ Boost

Date I/O Localization


Overall Index -- Gregorian Index -- Posix Time Index

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:

                       "Aug","Sep","Oct","Nov","Dec", "NAD"};
 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",
                                             "Nov","Dez", "NAM"};

  const char* const de_long_month_names[]={"Januar","Februar","Marz","April",

  const char* const de_special_value_names[]={"NichtDatumzeit",
  const char* const de_long_weekday_names[]={"Sonntag", "Montag", "Dienstag",
                                             "Mittwoch", "Donnerstag", "Freitag", 

  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, 
                                          '.', //separate parts using a '.'
  // Imbue the stream with the locale
  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, 
                                          '-', //separate parts using a '-'

  std::cout.imbue(german_dates2); //use long month names
  std::cout << d1 << std::endl; //2002-Oktober-01

Class Overview

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

Last modified: Sun Feb 1 09:18:06 MST 2004 by Jeff Garland © 2000-2004