From 5cd11255f906cc3ae9dc910eff2e586fed148018 Mon Sep 17 00:00:00 2001 From: Jeff Garland Date: Sun, 29 Sep 2002 20:26:23 +0000 Subject: [PATCH] merge changes from RC_1_29_0 -- fix locale to support date order [SVN r15560] --- example/gregorian/localization.cpp | 84 +++++++++++++++---- .../date_time/date_formatting_locales.hpp | 59 +++++++++---- include/boost/date_time/date_names_put.hpp | 43 +++++++++- .../boost/date_time/gregorian/greg_facet.hpp | 2 +- test/gregorian/testfacet.cpp | 66 +++++++++++++-- 5 files changed, 208 insertions(+), 46 deletions(-) diff --git a/example/gregorian/localization.cpp b/example/gregorian/localization.cpp index 6a51d7e..8f542f7 100644 --- a/example/gregorian/localization.cpp +++ b/example/gregorian/localization.cpp @@ -21,6 +21,15 @@ const char* const de_long_weekday_names[]={"Sonntag", "Montag", "Dienstag","Mitt 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() @@ -33,26 +42,67 @@ main() //create a new local std::locale 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, - '.')); + 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 - date d1(2002, Oct, 1); - std::cout.imbue(german_dates); - // output the date in German using short month names - std::cout << d1 << std::endl; //2002-Okt-01 - 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; diff --git a/include/boost/date_time/date_formatting_locales.hpp b/include/boost/date_time/date_formatting_locales.hpp index 4dcdf95..427cf1b 100644 --- a/include/boost/date_time/date_formatting_locales.hpp +++ b/include/boost/date_time/date_formatting_locales.hpp @@ -33,11 +33,10 @@ namespace date_time { //! Formats a month as as string into an output iterator static void format_month(const month_type& month, ostream_type& os, - const facet_type& f, - month_format_spec fs) + const facet_type& f) { - switch (fs) + switch (f.month_format()) { case month_as_short_string: { @@ -122,27 +121,55 @@ namespace date_time { // return ss.str(); // } + // Put ymd to ostream -- part of ostream refactor static void ymd_put(ymd_type ymd, ostream_type& os, const facet_type& f) { - os << ymd.year; std::ostreambuf_iterator oitr(os); - if (f.has_date_sep_chars()) { - f.month_sep_char(oitr); + switch (f.date_order()) { + case ymd_order_iso: { + os << ymd.year; + if (f.has_date_sep_chars()) { + f.month_sep_char(oitr); + } + month_formatter::format_month(ymd.month, os, f); + if (f.has_date_sep_chars()) { + f.day_sep_char(oitr); + } + os << std::setw(2) << std::setfill('0') + << ymd.day; + break; + } + case ymd_order_us: { + month_formatter::format_month(ymd.month, os, f); + if (f.has_date_sep_chars()) { + f.day_sep_char(oitr); + } + os << std::setw(2) << std::setfill('0') + << ymd.day; + if (f.has_date_sep_chars()) { + f.month_sep_char(oitr); + } + os << ymd.year; + break; + } + case ymd_order_dmy: { + os << std::setw(2) << std::setfill('0') + << ymd.day; + if (f.has_date_sep_chars()) { + f.day_sep_char(oitr); + } + month_formatter::format_month(ymd.month, os, f); + if (f.has_date_sep_chars()) { + f.month_sep_char(oitr); + } + os << ymd.year; + break; + } } - month_formatter::format_month(ymd.month, os, - f, month_as_short_string); - - - if (f.has_date_sep_chars()) { - f.day_sep_char(oitr); - } - os << std::setw(2) << std::setfill('0') - << ymd.day; } - }; diff --git a/include/boost/date_time/date_names_put.hpp b/include/boost/date_time/date_names_put.hpp index c075390..deb7283 100644 --- a/include/boost/date_time/date_names_put.hpp +++ b/include/boost/date_time/date_names_put.hpp @@ -12,6 +12,7 @@ #include "boost/date_time/special_defs.hpp" #include "boost/date_time/date_defs.hpp" +#include "boost/date_time/parse_format_base.hpp" #include @@ -84,6 +85,16 @@ namespace date_time { { do_day_sep_char(oitr); } + //! Determines the order to put the date elements + ymd_order_spec date_order() const + { + return do_date_order(); + } + //! Determines if month is displayed as integer, short or long string + month_format_spec month_format() const + { + return do_month_format(); + } protected: //! Default facet implementation uses month_type defaults @@ -145,7 +156,16 @@ namespace date_time { { put_string(oitr, "-"); } - + //! Default for date order + virtual ymd_order_spec do_date_order() const + { + return ymd_order_iso; + } + //! Default month format + virtual month_format_spec do_month_format() const + { + return month_as_short_string; + } void put_string(iter_type& oi, const char* const s) const { string_type s1(s); @@ -169,12 +189,16 @@ namespace date_time { const char* const special_value_names[], const char* const weekday_short_names[], const char* const weekday_long_names[], - char separator_char = '-') : + char separator_char = '-', + ymd_order_spec order_spec = ymd_order_iso, + month_format_spec month_format = month_as_short_string) : month_short_names_(month_short_names), month_long_names_(month_long_names), special_value_names_(special_value_names), weekday_short_names_(weekday_short_names), - weekday_long_names_(weekday_long_names) + weekday_long_names_(weekday_long_names), + order_spec_(order_spec), + month_format_spec_(month_format) { separator_char_[0] = separator_char; separator_char_[1] = '\0'; @@ -219,6 +243,16 @@ namespace date_time { { put_string(oitr, separator_char_); } + //! Set the date ordering + virtual ymd_order_spec do_date_order() const + { + return order_spec_; + } + //! Set the date ordering + virtual month_format_spec do_month_format() const + { + return month_format_spec_; + } private: const char* const* month_short_names_; @@ -227,7 +261,8 @@ namespace date_time { const char* const* weekday_short_names_; const char* const* weekday_long_names_; char separator_char_[2]; - + ymd_order_spec order_spec_; + month_format_spec month_format_spec_; }; } } //namespace boost::date_time diff --git a/include/boost/date_time/gregorian/greg_facet.hpp b/include/boost/date_time/gregorian/greg_facet.hpp index 258969a..5afd6f1 100644 --- a/include/boost/date_time/gregorian/greg_facet.hpp +++ b/include/boost/date_time/gregorian/greg_facet.hpp @@ -45,7 +45,7 @@ namespace gregorian { std::locale locale = os.getloc(); if (std::has_facet(locale)) { const greg_base_facet& f = std::use_facet(locale); - greg_month_formatter::format_month(m, os, f, date_time::month_as_long_string); + greg_month_formatter::format_month(m, os, f); } else { //default to numeric diff --git a/test/gregorian/testfacet.cpp b/test/gregorian/testfacet.cpp index 39d9f96..19458ba 100644 --- a/test/gregorian/testfacet.cpp +++ b/test/gregorian/testfacet.cpp @@ -14,7 +14,7 @@ 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_short_weekday_names[]={"Son", "Mon", "Diens"}; +const char* const de_short_weekday_names[]={"Son", "Mon", "Die","Mit", "Don", "Fre", "Sam"}; const char* const de_long_weekday_names[]={"Sonntag", "Montag", "Dienstag","Mittwoch", "Donnerstag", "Freitag", "Samstag"}; @@ -40,7 +40,9 @@ main() date_facet gdnp(de_short_month_names, de_long_month_names, de_special_value_names, de_long_weekday_names, - de_long_weekday_names); + de_long_weekday_names, + '.', + boost::date_time::ymd_order_dmy); std::stringstream ss; std::ostreambuf_iterator coi(ss); @@ -50,13 +52,13 @@ main() ss.str(""); //reset string stream greg_month m(Oct); - month_formatter::format_month(m, ss, gdnp, boost::date_time::month_as_short_string); + month_formatter::format_month(m, ss, gdnp); check("check german short month: " + ss.str(), ss.str() == std::string("Okt")); ss.str(""); //reset string stream - month_formatter::format_month(m, ss, gdnp, boost::date_time::month_as_long_string); - check("check german long month: " + ss.str(), - ss.str() == std::string("Oktober")); +// month_formatter::format_month(m, ss, gdnp); +// check("check german long month: " + ss.str(), +// ss.str() == std::string("Oktober")); greg_year_month_day ymd(2002,Oct,1); @@ -64,7 +66,7 @@ main() ss.str(""); //reset string stream ymd_formatter::ymd_put(ymd, ss, gdnp); check("check ymd: " + ss.str(), - ss.str() == std::string("2002-Okt-01")); + ss.str() == std::string("01.Okt.2002")); typedef boost::date_time::ostream_date_formatter datef; @@ -73,7 +75,7 @@ main() date d1(2002, Oct, 1); datef::date_put(d1, os, gdnp); check("ostream low level check string:"+os.str(), - os.str() == std::string("2002-Okt-01")); + os.str() == std::string("01.Okt.2002")); // //Locale tests std::locale global; @@ -128,6 +130,54 @@ main() f << d1 << std::endl; // // date formatter that takes locale and gets facet from locale + std::locale german_dates1(global, + 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)); + + os3.imbue(german_dates1); + os3.str(""); + os3 << d1; + check("check date order: "+os3.str(), + os3.str() == std::string("01.10.2002")); + + std::locale german_dates2(global, + 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_iso, + boost::date_time::month_as_short_string)); + + os3.imbue(german_dates2); + os3.str(""); + os3 << d1; + check("check date order: "+os3.str(), + os3.str() == std::string("2002 Okt 01")); + + std::locale german_dates3(global, + 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_us, + boost::date_time::month_as_long_string)); + + os3.imbue(german_dates3); + os3.str(""); + os3 << d1; + check("check date order: "+os3.str(), + os3.str() == std::string("Oktober 01 2002")); + #else