Timepunct_byname

You can use timepunct_byname to get the effects of a named locale for time facets instead of using a named locale:

The time_get_byname and time_put_byname facets do not add any functionality over time_get and time_put.

Listing: Using Timepunct_byname
#include <locale>
#include <iostream>

#include <sstream>

#include "Date.h"

int

main()

{

   std::istringstream in("Saturday February 24 2001");

   Date today;

   in >> today;

   std::cout.imbue(std::locale(std::locale(),

      new std::timepunct_byname<char>("French")));

   std::cout << "En Paris, c'est " << today << '\n';

   std::cout.imbue(std::locale(std::locale(),

      new std::timepunct_byname<char>("US")));

   std::cout << "But in New York it is " << today << '\n';

}

This has the exact same effect as the named locale example.

But the timepunct_byname example still uses the files "French" and "US". Below is an example timepunct derived class that avoids files but still captures the functionality of the above examples.

Listing: Example Timepunct Facet Use
// The first job is to create a facet derived from timepunct 
// that stores the desired data in the timepunct:

class FrenchTimepunct

   : public std::timepunct<char>

{

public:

   FrenchTimepunct();

};

FrenchTimepunct::FrenchTimepunct()

{

   __date_ = "%A, le %d %B %Y";

   __weekday_names_[0] = "dimanche";

   __weekday_names_[1] = "lundi";

   __weekday_names_[2] = "mardi";

   __weekday_names_[3] = "mercredi";

   __weekday_names_[4] = "jeudi";

   __weekday_names_[5] = "vendredi";

   __weekday_names_[6] = "samedi";

   __weekday_names_[7] = "dim";

   __weekday_names_[8] = "lun";

   __weekday_names_[9] = "mar";

   __weekday_names_[10] = "mer";

   __weekday_names_[11] = "jeu";

   __weekday_names_[12] = "ven";

   __weekday_names_[13] = "sam";

   __month_names_[0] = "janvier";

   __month_names_[1] = "février";

   __month_names_[2] = "mars";

   __month_names_[3] = "avril";

   __month_names_[4] = "mai";

   __month_names_[5] = "juin";

   __month_names_[6] = "juillet";

   __month_names_[7] = "août";

   __month_names_[8] = "septembre";

   __month_names_[9] = "octobre";

   __month_names_[10] = "novembre";

   __month_names_[11] = "décembre";

   __month_names_[12] = "jan";

   __month_names_[13] = "fév";

   __month_names_[14] = "mar";

   __month_names_[15] = "avr";

   __month_names_[16] = "mai";

   __month_names_[17] = "juin";

   __month_names_[18] = "juil";

   __month_names_[19] = "aoû";

   __month_names_[20] = "sep";

   __month_names_[21] = "oct";

   __month_names_[22] = "nov";

   __month_names_[23] = "déc";

}

//Though tedious, the job is quite simple. 

//Next simply use your facet:

int main()

{

   std::istringstream in("Saturday February 24 2001");

   Date today;

   in >> today;

   std::cout.imbue(std::locale(std::locale(),

   new FrenchTimepunct));

   std::cout << "En Paris, c'est " << today << '\n';

   std::cout.imbue(std::locale::classic());

   std::cout << "But in New York it is " << today << '\n';

}

Here we have explicitly asked for the classic locale, instead of the "US" locale since the two are the same (but executing classic() does not involve file I/O). Using the global locale (locale()) instead of classic() would have been equally fine in this example.