From 82db0bf9bc567701e41e8cdc7db1b9ecb7b3bf28 Mon Sep 17 00:00:00 2001 From: Jeff Garland Date: Wed, 25 May 2005 13:34:25 +0000 Subject: [PATCH] various updates / fixes to examples [SVN r29185] --- example/Jamfile | 15 +- example/gregorian/find_last_day_of_months.cpp | 53 ++++++ example/gregorian/localization.cpp | 147 ++++++----------- example/gregorian/month_add.cpp | 3 +- example/local_time/simple_time_zone.cpp | 16 +- example/tutorial/io_tutorial.cpp | 153 ++++++++++++++++++ 6 files changed, 269 insertions(+), 118 deletions(-) create mode 100644 example/gregorian/find_last_day_of_months.cpp create mode 100644 example/tutorial/io_tutorial.cpp diff --git a/example/Jamfile b/example/Jamfile index dc34391..f481db5 100644 --- a/example/Jamfile +++ b/example/Jamfile @@ -23,8 +23,8 @@ exe days_between_new_years : ../build/boost_date_time : $(BOOST_ROOT) $(DATE_TIME_PROPERTIES) ; -exe end_of_month_day : ../build/boost_date_time - gregorian/end_of_month_day.cpp +exe find_last_day_of_months : ../build/boost_date_time + gregorian/find_last_day_of_months.cpp : $(BOOST_ROOT) $(DATE_TIME_PROPERTIES) ; @@ -33,11 +33,8 @@ exe month_add : ../build/boost_date_time : $(BOOST_ROOT) $(DATE_TIME_PROPERTIES) ; -exe localization : ../build/boost_date_time - gregorian/localization.cpp - : $(BOOST_ROOT) -# USE_DATE_TIME_PRE_1_33_FACET_IO - $(DATE_TIME_PROPERTIES) +exe localization : gregorian/localization.cpp + : $(BOOST_ROOT) $(DATE_TIME_PROPERTIES) ; exe print_holidays : ../build/boost_date_time @@ -95,6 +92,10 @@ exe seconds_since_epoch : ../build/boost_date_time : $(BOOST_ROOT) $(DATE_TIME_PROPERTIES) ; +exe io_tutorial : tutorial/io_tutorial.cpp + : $(BOOST_ROOT) + ; + # Copyright (c) 2002-2004 # CrystalClear Software, Inc. # Subject to the Boost Software License, Version 1.0. diff --git a/example/gregorian/find_last_day_of_months.cpp b/example/gregorian/find_last_day_of_months.cpp new file mode 100644 index 0000000..da8eb22 --- /dev/null +++ b/example/gregorian/find_last_day_of_months.cpp @@ -0,0 +1,53 @@ +/* Simple program that finds the last day of the given month, + * then displays the last day of every month left in the given year. + */ + +#include "boost/date_time/gregorian/gregorian.hpp" +#include + +int +main() +{ + using namespace boost::gregorian; + + greg_year year(1400); + greg_month month(1); + + // get a month and a year from the user + try { + int y, m; + std::cout << " Enter Year(ex: 2002): "; + std::cin >> y; + year = greg_year(y); + std::cout << " Enter Month(1..12): "; + std::cin >> m; + month = greg_month(m); + } + catch(bad_year by) { + std::cout << "Invalid Year Entered: " << by.what() << '\n' + << "Using minimum values for month and year." << std::endl; + } + catch(bad_month bm) { + std::cout << "Invalid Month Entered" << bm.what() << '\n' + << "Using minimum value for month. " << std::endl; + } + + date start_of_next_year(year+1, Jan, 1); + date d(year, month, 1); + + // add another month to d until we enter the next year. + while (d < start_of_next_year){ + std::cout << to_simple_string(d.end_of_month()) << std::endl; + d += months(1); + } + + return 0; +} + +/* Copyright 2001-2005: 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) + */ + diff --git a/example/gregorian/localization.cpp b/example/gregorian/localization.cpp index 57f6add..d17871b 100644 --- a/example/gregorian/localization.cpp +++ b/example/gregorian/localization.cpp @@ -1,20 +1,15 @@ /* 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). - * - * This program uses the pre 1.33 version of date_time - * input/output code. Typically the USE_DATE_TIME_PRE_1_33_FACET_IO - * macro would be defined in a Jamfile but it was defined in this - * file for the sake of the example. */ -#define USE_DATE_TIME_PRE_1_33_FACET_IO - #include "boost/date_time/gregorian/gregorian.hpp" #include +#include -/* Define a series of char arrays for short and long name strings to be - * associated with date output. */ +/* Define a series of char arrays for short and long name strings + * to be associated with German date output (US names will be + * retrieved from the locale). */ const char* const de_short_month_names[] = { "Jan", "Feb", "Mar", "Apr", "Mai", "Jun", @@ -26,10 +21,6 @@ const char* const de_long_month_names[] = "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", @@ -40,113 +31,67 @@ 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() +int main() { - -#ifndef BOOST_DATE_TIME_NO_LOCALE - using namespace boost::gregorian; - typedef boost::date_time::all_date_names_put 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)); - + + // create some gregorian objects to output date d1(2002, Oct, 1); - std::cout.imbue(german_dates1); + greg_month m = d1.month(); + greg_weekday wd = d1.day_of_week(); + + // create a facet and a locale for German dates + date_facet* german_facet = new date_facet(); + std::cout.imbue(std::locale(std::locale::classic(), german_facet)); + + // create the German name collections + date_facet::input_collection_type short_months, long_months, + short_weekdays, long_weekdays; + std::copy(&de_short_month_names[0], &de_short_month_names[11], + std::back_inserter(short_months)); + std::copy(&de_long_month_names[0], &de_long_month_names[11], + std::back_inserter(long_months)); + std::copy(&de_short_weekday_names[0], &de_short_weekday_names[6], + std::back_inserter(short_weekdays)); + std::copy(&de_long_weekday_names[0], &de_long_weekday_names[6], + std::back_inserter(long_weekdays)); + + // replace the default names with ours + // NOTE: date_generators and special_values were not replaced as + // they are not used in this example + german_facet->short_month_names(short_months); + german_facet->long_month_names(long_months); + german_facet->short_weekday_names(short_weekdays); + german_facet->long_weekday_names(long_weekdays); + // output the date in German using short month names + german_facet->format("%d.%m.%Y"); 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(); + german_facet->month_format("%B"); std::cout << m << std::endl; //Oktober - greg_weekday wd = d1.day_of_week(); + german_facet->weekday_format("%A"); 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)); + // Output the same gregorian objects using US names + date_facet* us_facet = new date_facet(); + std::cout.imbue(std::locale(std::locale::classic(), us_facet)); - std::cout.imbue(usa_dates2); + us_facet->format("%m/%d/%Y"); + std::cout << d1 << std::endl; // 10/01/2002 + + // English names, iso order (year-month-day), '-' separator + us_facet->format("%Y-%b-%d"); 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 +/* Copyright 2001-2005: CrystalClear Software, Inc * http://www.crystalclearsoftware.com * * Subject to the Boost Software License, Version 1.0. diff --git a/example/gregorian/month_add.cpp b/example/gregorian/month_add.cpp index 188b5cc..3bbe880 100644 --- a/example/gregorian/month_add.cpp +++ b/example/gregorian/month_add.cpp @@ -12,7 +12,6 @@ main() { using namespace boost::gregorian; - typedef boost::date_time::month_functor add_month; date d = day_clock::local_day(); date d2 = d + months(1); @@ -44,7 +43,7 @@ main() return 0; } -/* Copyright 2001-2004: CrystalClear Software, Inc +/* Copyright 2001-2005: CrystalClear Software, Inc * http://www.crystalclearsoftware.com * * Subject to the Boost Software License, Version 1.0. diff --git a/example/local_time/simple_time_zone.cpp b/example/local_time/simple_time_zone.cpp index 51b0f1c..995c1d1 100644 --- a/example/local_time/simple_time_zone.cpp +++ b/example/local_time/simple_time_zone.cpp @@ -1,4 +1,4 @@ -/* A simple example for using a time_zone and a posix_time_zone. +/* A simple example for using a custom_time_zone and a posix_time_zone. */ #include "boost/date_time/local_time/local_time.hpp" @@ -12,9 +12,9 @@ main() using namespace gregorian; using posix_time::time_duration; - /***** time_zone *****/ + /***** custom_time_zone *****/ - // create the dependent objects for a time_zone + // create the dependent objects for a custom_time_zone time_zone_names tzn("Eastern Standard Time", "EST", "Eastern Daylight Time", "EDT"); time_duration utc_offset(-5,0,0); @@ -29,7 +29,7 @@ main() last_day_of_the_week_in_month end_rule(Sunday, Oct); shared_ptr nyc_rules(new first_last_dst_rule(start_rule, end_rule)); - // create more dependent objects for a non-dst time_zone + // create more dependent objects for a non-dst custom_time_zone time_zone_names tzn2("Mountain Standard Time", "MST", "", ""); // no dst means empty dst strings time_duration utc_offset2(-7,0,0); @@ -39,14 +39,14 @@ main() // no dst means we need a null pointer to the rules shared_ptr phx_rules; - // create the time_zones - time_zone_ptr nyc_1(new time_zone(tzn, utc_offset, adj_offsets, nyc_rules)); - time_zone_ptr phx_1(new time_zone(tzn2, utc_offset2, adj_offsets2, phx_rules)); + // create the custom_time_zones + time_zone_ptr nyc_1(new custom_time_zone(tzn, utc_offset, adj_offsets, nyc_rules)); + time_zone_ptr phx_1(new custom_time_zone(tzn2, utc_offset2, adj_offsets2, phx_rules)); /***** posix_time_zone *****/ // create posix_time_zones that are the duplicates of the - // time_zones created above. See posix_time_zone documentation + // custom_time_zones created above. See posix_time_zone documentation // for details on full zone names. std::string nyc_string, phx_string; nyc_string = "EST-05:00:00EDT+01:00:00,M4.1.0/02:00:00,M10.5.0/02:00:00"; diff --git a/example/tutorial/io_tutorial.cpp b/example/tutorial/io_tutorial.cpp new file mode 100644 index 0000000..3d3519e --- /dev/null +++ b/example/tutorial/io_tutorial.cpp @@ -0,0 +1,153 @@ +#include +#include + +int main(){ + using namespace boost::gregorian; + using namespace boost::posix_time; + using namespace boost::local_time; + using namespace std; + + date d(2004, 2, 29); + time_duration td(12,34,56,789); + stringstream ss; + ss << d << ' ' << td; + ptime pt(not_a_date_time); + cout << pt << endl; // "not-a-date-time" + ss >> pt; + cout << pt << endl; // "2004-Feb-29 12:34:56.000789" + ss.str(""); + ss << pt << " EDT-05EDT,M4.1.0,M10.5.0"; + local_date_time ldt(not_a_date_time); + ss >> ldt; + cout << ldt << endl; // " 2004-Feb-29 12:34:56.000789 EDT" + + + local_time_facet* output_facet = new local_time_facet(); + local_time_input_facet* input_facet = new local_time_input_facet(); + ss.imbue(locale(locale::classic(), output_facet)); + ss.imbue(locale(ss.getloc(), input_facet)); + output_facet->format("%a %b %d, %H:%M %z"); + ss.str(""); + ss << ldt; + cout << ss.str() << endl; // "Sun Feb 29, 12:34 EDT" + + output_facet->format(local_time_facet::iso_time_format_specifier); + ss.str(""); + ss << ldt; + cout << ss.str() << endl; // "20040229T123456.000789-0500" + output_facet->format(local_time_facet::iso_time_format_extended_specifier); + ss.str(""); + ss << ldt; + cout << ss.str() << endl; // "2004-02-29 12:34:56.000789-05:00" + + + string my_format("The extended ordinal time %Y-%jT%H:%M can also be represented as %A %B %d, %Y"); + output_facet->format(my_format.c_str()); + input_facet->format(my_format.c_str()); + ss.str(""); + ss << ldt; + cout << ss.str() << endl; + ss.str("The extended ordinal time 2005-128T12:15 can also be represented as Sunday May 08, 2005"); + ss >> ldt; + cout << ldt << endl; + + // reset the formats to defaults + output_facet->format(local_time_facet::default_time_format); + input_facet->format(local_time_input_facet::default_time_input_format); + + // create custom special_values parser and formatter objetcs + // and add them to the facets + string sv[5] = {"nadt","neg_inf", "pos_inf", "min_dt", "max_dt" }; + vector sv_names(&sv[0], &sv[5]); + special_values_parser sv_parser(sv_names.begin(), sv_names.end()); + special_values_formatter sv_formatter(sv_names.begin(), sv_names.end()); + output_facet->special_values_formatter(sv_formatter); + input_facet->special_values_parser(sv_parser); + + ss.str(""); + ldt = local_date_time(not_a_date_time); + ss << ldt; + cout << ss.str() << endl; // "nadt" + ss.str("min_dt"); + ss >> ldt; + ss.str(""); + ss << ldt; + cout << ss.str() << endl; // "1400-Jan-01 00:00:00 UTC" + + // set up the collections of custom strings. + // only the full names are altered for the sake of brevity + string month_names[12] = { "january", "february", "march", + "april", "may", "june", + "july", "august", "september", + "october", "november", "december" }; + vector long_months(&month_names[0], &month_names[12]); + string day_names[7] = { "sunday", "monday", "tuesday", "wednesday", + "thursday", "friday", "saturday" }; + vector long_days(&day_names[0], &day_names[7]); + + // custome date_generator phrases + string dg_phrases[9] = { "1st", "2nd", "3rd", "4th", "5th", + "final", "prior to", "following", "in" }; + vector phrases(&dg_phrases[0], &dg_phrases[9]); + + // custom period formatter and parser + period_formatter per_formatter(period_formatter::AS_OPEN_RANGE, + " to ", "from ", " exclusive", " inclusive" ); + period_parser per_parser(period_parser::AS_OPEN_RANGE, + " to ", "from ", " exclusive" , "inclusive" ); + + // create date_facet and date_input_facet + date_facet* date_output = new date_facet(); + date_input_facet* date_input = new date_input_facet(); + ss.imbue(locale(ss.getloc(), date_output)); + ss.imbue(locale(ss.getloc(), date_input)); + + date_period dp(date(2005,3,1), days(31)); // month of march + first_day_of_the_week_before d_gen(Monday); + // defaults + ss.str(""); + ss << dp << "; " << d_gen; + cout << ss.str() << endl; // "[2005-Mar-01/2005-Mar-31]; Mon before" + + // customize everything in the output facet + date_output->long_month_names(long_months); + date_output->long_weekday_names(long_days); + date_output->date_gen_phrase_strings(phrases); + date_output->period_formatter(per_formatter); + + // customize everything in the output facet + date_input->long_month_names(long_months); + date_input->long_weekday_names(long_days); + date_input->date_gen_element_strings(phrases); + date_input->period_parser(per_parser); + + // customize month, weekday and date formats + date_output->format("%Y-%B-%d"); + date_input->format("%Y-%B-%d"); + date_output->month_format("%B"); // full name + date_input->month_format("%B"); // full name + date_output->weekday_format("%A"); // full name + date_input->weekday_format("%A"); // full name + + ss.str("from 2005-february-01 to 2005-march-01 exclusive"); + ss >> dp; + ss.str("wednesday prior to"); + ss >> d_gen; + ss.str(""); + ss << dp << " is the month of " << dp.begin().month(); + cout << ss.str() << endl; + ss.str(""); + ss << "The " << d_gen << ' ' << dp.begin() << " is " << d_gen.get_date(dp.begin()); + cout << ss.str() << endl; + + return 0; +} + + +/* Copyright 2005: 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) + */ +