diff options
Diffstat (limited to 'src/boost/libs/date_time/example')
26 files changed, 1631 insertions, 0 deletions
diff --git a/src/boost/libs/date_time/example/Jamfile b/src/boost/libs/date_time/example/Jamfile new file mode 100644 index 000000000..89e0ad166 --- /dev/null +++ b/src/boost/libs/date_time/example/Jamfile @@ -0,0 +1,47 @@ +project libs/date_time/example ; + +exe dates_as_strings : gregorian/dates_as_strings.cpp ; +exe days_alive : gregorian/days_alive.cpp ; +exe days_till_new_year : gregorian/days_till_new_year.cpp ; + +exe days_between_new_years : gregorian/days_between_new_years.cpp ; +exe find_last_day_of_months : gregorian/find_last_day_of_months.cpp ; + +exe month_add : gregorian/month_add.cpp ; + +exe localization : gregorian/localization.cpp ; +exe print_holidays : gregorian/print_holidays.cpp ; +exe print_month : gregorian/print_month.cpp ; +exe period_calc : gregorian/period_calc.cpp ; + +exe days_since_year_start : gregorian/days_since_year_start.cpp ; + + +exe local_utc_conversion : posix_time/local_utc_conversion.cpp + : <define>BOOST_DATE_TIME_POSIX_TIME_STD_CONFIG + ; + +exe print_hours : posix_time/print_hours.cpp + : <define>BOOST_DATE_TIME_POSIX_TIME_STD_CONFIG + ; + +exe time_math : posix_time/time_math.cpp + : <define>BOOST_DATE_TIME_POSIX_TIME_STD_CONFIG + ; + +exe time_periods : posix_time/time_periods.cpp + : <define>BOOST_DATE_TIME_POSIX_TIME_STD_CONFIG + ; + +exe simple_time_zone : local_time/simple_time_zone.cpp ; +exe calc_rules : local_time/calc_rules.cpp ; +exe seconds_since_epoch : local_time/seconds_since_epoch.cpp ; +exe flight : local_time/flight.cpp ; +exe io_tutorial : tutorial/io_tutorial.cpp ; + +# Copyright (c) 2002-2005 +# CrystalClear Software, Inc. +# Subject to the Boost Software License, Version 1.0. +# (See accompanying file LICENSE_1_0.txt or +# http://www.boost.org/LICENSE_1_0.txt) + diff --git a/src/boost/libs/date_time/example/gregorian/Jamfile.v2 b/src/boost/libs/date_time/example/gregorian/Jamfile.v2 new file mode 100644 index 000000000..39a6c219f --- /dev/null +++ b/src/boost/libs/date_time/example/gregorian/Jamfile.v2 @@ -0,0 +1,20 @@ +# Copyright (c) 2002-2006 CrystalClear Software, Inc. +# Use, modification and distribution is subject to the +# Boost Software License, Version 1.0. (See accompanying +# file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt) +# + +project + : requirements <library>../../build/boost_date_time + ; + +exe dates_as_strings : dates_as_strings.cpp ; +exe end_of_month_day : end_of_month_day.cpp ; +exe period_calc : period_calc.cpp ; +exe days_alive : days_alive.cpp ; +exe print_holidays : print_holidays.cpp ; +exe days_since_year_start : days_since_year_start.cpp ; +exe print_month : print_month.cpp ; +exe days_till_new_year : days_till_new_year.cpp ; +exe date_localization : localization.cpp ; +
\ No newline at end of file diff --git a/src/boost/libs/date_time/example/gregorian/date_serialization_demo.cpp b/src/boost/libs/date_time/example/gregorian/date_serialization_demo.cpp new file mode 100644 index 000000000..3c22129c4 --- /dev/null +++ b/src/boost/libs/date_time/example/gregorian/date_serialization_demo.cpp @@ -0,0 +1,127 @@ + + +#include "boost/date_time/gregorian/gregorian.hpp" +#include "boost/date_time/gregorian/greg_serialize.hpp" +#include "boost/serialization/set.hpp" +#include "boost/serialization/list.hpp" +#include "boost/archive/text_oarchive.hpp" +#include "boost/archive/text_iarchive.hpp" +#include <iostream> +#include <fstream> + +using namespace boost::gregorian; +typedef std::set<date> date_set; +typedef std::list<date> date_list; + +void print(std::ostream& os, const date_set& ds) +{ + os << "******** Date Set *********" << std::endl; + date_set::const_iterator itr = ds.begin(); + while (itr != ds.end()) + { + os << (*itr) << " "; + itr++; + } + os << "\n***************************" << std::endl; +} + +class foo { + public: + foo(date d = date(not_a_date_time), + int i = 0) : + my_date(d), + my_int(i) + {} + void insert_date(date d) + { + my_dates.push_back(d); + } + void print(std::ostream& os) const + { + os << "foo= my_date is: " << my_date + << " my_int is: " << my_int; + date_list::const_iterator i = my_dates.begin(); + os << " Important dates: "; + while (i != my_dates.end()) { + os << (*i) << " "; + i++; + } + os << std::endl; + } + private: + friend class boost::serialization::access; + + // is a type of input archive the & operator is defined similar to >>. + template<class Archive> + void serialize(Archive & ar, const unsigned int version) + { + ar & my_date; + ar & my_int; + ar & my_dates; + } + + date my_date; + int my_int; + date_list my_dates; +}; + + +int +main() +{ + try { + date d(2004, Apr, 5); + std::cout << "Date: " << to_iso_string(d) << std::endl; + std::cout << "Date: " << d << std::endl; + std::ofstream ofs("date_demo.txt"); + boost::archive::text_oarchive oa(ofs); + oa << d; + + std::cout << "Construct a foo" << std::endl; + foo f(d, 1); + f.insert_date(d+days(1)); + f.insert_date(d+days(2)); + f.insert_date(d+days(3)); + f.print(std::cout); + oa << f; + + date_set dates; + dates.insert(date(2004, Apr,1)); + dates.insert(date(2004, Apr,10)); + dates.insert(date(2004, Apr,15)); + print(std::cout, dates); + + oa << dates; + ofs.close(); + + std::cout << "Now do the input streaming" << std::endl; + date d2(not_a_date_time); + std::ifstream ifs("date_demo.txt"); + boost::archive::text_iarchive ia(ifs); + ia >> d2; + + std::cout << "New date is: " << d2 << std::endl; + + foo f2; + ia >> f2; + f2.print(std::cout); + + date_set dates2; + ia >> dates2; //exception here + print(std::cout, dates2); + + } + catch(std::exception& e) { + std::cout << "Caught Exception: " << e.what() << std::endl; + } + +} + + +/* Copyright 2001-2004: CrystalClear Software, Inc + * http://www.crystalclearsoftware.com + * + * Subject to the Boost Software License, Version 1.0. + * (See accompanying file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt) + */ + diff --git a/src/boost/libs/date_time/example/gregorian/dates_as_strings.cpp b/src/boost/libs/date_time/example/gregorian/dates_as_strings.cpp new file mode 100644 index 000000000..097fffbc8 --- /dev/null +++ b/src/boost/libs/date_time/example/gregorian/dates_as_strings.cpp @@ -0,0 +1,63 @@ +/* The following is a simple example that shows conversion of dates + * to and from a std::string. + * + * Expected output: + * 2001-Oct-09 + * 2001-10-09 + * Tuesday October 9, 2001 + * An expected exception is next: + * Exception: Month number is out of range 1..12 + */ + +#include "boost/date_time/gregorian/gregorian.hpp" +#include <iostream> +#include <string> + +int +main() +{ + + using namespace boost::gregorian; + + try { + // The following date is in ISO 8601 extended format (CCYY-MM-DD) + std::string s("2001-10-9"); //2001-October-09 + date d(from_simple_string(s)); + std::cout << to_simple_string(d) << std::endl; + + //Read ISO Standard(CCYYMMDD) and output ISO Extended + std::string ud("20011009"); //2001-Oct-09 + date d1(from_undelimited_string(ud)); + std::cout << to_iso_extended_string(d1) << std::endl; + + //Output the parts of the date - Tuesday October 9, 2001 + date::ymd_type ymd = d1.year_month_day(); + greg_weekday wd = d1.day_of_week(); + std::cout << wd.as_long_string() << " " + << ymd.month.as_long_string() << " " + << ymd.day << ", " << ymd.year + << std::endl; + + //Let's send in month 25 by accident and create an exception + std::string bad_date("20012509"); //2001-??-09 + std::cout << "An expected exception is next: " << std::endl; + date wont_construct(from_undelimited_string(bad_date)); + //use wont_construct so compiler doesn't complain, but you wont get here! + std::cout << "oh oh, you shouldn't reach this line: " + << to_iso_string(wont_construct) << std::endl; + } + catch(std::exception& e) { + std::cout << " Exception: " << e.what() << std::endl; + } + + + return 0; +} + +/* Copyright 2001-2004: CrystalClear Software, Inc + * http://www.crystalclearsoftware.com + * + * Subject to the Boost Software License, Version 1.0. + * (See accompanying file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt) + */ + diff --git a/src/boost/libs/date_time/example/gregorian/days_alive.cpp b/src/boost/libs/date_time/example/gregorian/days_alive.cpp new file mode 100644 index 000000000..83068ecfd --- /dev/null +++ b/src/boost/libs/date_time/example/gregorian/days_alive.cpp @@ -0,0 +1,47 @@ +/* Short example that calculates the number of days since user was born. + * Demonstrates comparisons of durations, use of the day_clock, + * and parsing a date from a string. + */ + +#include "boost/date_time/gregorian/gregorian.hpp" +#include <iostream> + +int +main() +{ + + using namespace boost::gregorian; + std::string s; + std::cout << "Enter birth day YYYY-MM-DD (eg: 2002-02-01): "; + std::cin >> s; + try { + date birthday(from_simple_string(s)); + date today = day_clock::local_day(); + days days_alive = today - birthday; + days one_day(1); + if (days_alive == one_day) { + std::cout << "Born yesterday, very funny" << std::endl; + } + else if (days_alive < days(0)) { + std::cout << "Not born yet, hmm: " << days_alive.days() + << " days" <<std::endl; + } + else { + std::cout << "Days alive: " << days_alive.days() << std::endl; + } + + } + catch(...) { + std::cout << "Bad date entered: " << s << std::endl; + } + return 0; +} + + +/* Copyright 2001-2004: CrystalClear Software, Inc + * http://www.crystalclearsoftware.com + * + * Subject to the Boost Software License, Version 1.0. + * (See accompanying file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt) + */ + diff --git a/src/boost/libs/date_time/example/gregorian/days_between_new_years.cpp b/src/boost/libs/date_time/example/gregorian/days_between_new_years.cpp new file mode 100644 index 000000000..d1d1d3897 --- /dev/null +++ b/src/boost/libs/date_time/example/gregorian/days_between_new_years.cpp @@ -0,0 +1,38 @@ +/* Provides a simple example of using a date_generator, and simple + * mathematical operatorations, to calculate the days since + * New Years day of this year, and days until next New Years day. + * + * Expected results: + * Adding together both durations will produce 365 (366 in a leap year). + */ + +#include "boost/date_time/gregorian/gregorian.hpp" +#include <iostream> + +int +main() +{ + + using namespace boost::gregorian; + + date today = day_clock::local_day(); + partial_date new_years_day(1,Jan); + //Subtract two dates to get a duration + days days_since_year_start = today - new_years_day.get_date(today.year()); + std::cout << "Days since Jan 1: " << days_since_year_start.days() + << std::endl; + + days days_until_year_start = new_years_day.get_date(today.year()+1) - today; + std::cout << "Days until next Jan 1: " << days_until_year_start.days() + << std::endl; + return 0; +} + + +/* Copyright 2001-2004: CrystalClear Software, Inc + * http://www.crystalclearsoftware.com + * + * Subject to the Boost Software License, Version 1.0. + * (See accompanying file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt) + */ + diff --git a/src/boost/libs/date_time/example/gregorian/days_since_year_start.cpp b/src/boost/libs/date_time/example/gregorian/days_since_year_start.cpp new file mode 100644 index 000000000..50e08d0fc --- /dev/null +++ b/src/boost/libs/date_time/example/gregorian/days_since_year_start.cpp @@ -0,0 +1,25 @@ + +#include "boost/date_time/gregorian/gregorian.hpp" +#include <iostream> + +int +main() +{ + + using namespace boost::gregorian; + + date today = day_clock::local_day(); + //Subtract two dates to get a duration + date_duration days_since_year_start = today - date(today.year(),Jan,1); + std::cout << "Days since Jan 1: " << days_since_year_start.days() + << std::endl; + return 0; +} + +/* Copyright 2001-2004: CrystalClear Software, Inc + * http://www.crystalclearsoftware.com + * + * Subject to the Boost Software License, Version 1.0. + * (See accompanying file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt) + */ + diff --git a/src/boost/libs/date_time/example/gregorian/days_till_new_year.cpp b/src/boost/libs/date_time/example/gregorian/days_till_new_year.cpp new file mode 100644 index 000000000..96dacbd5d --- /dev/null +++ b/src/boost/libs/date_time/example/gregorian/days_till_new_year.cpp @@ -0,0 +1,26 @@ +// This example displays the amount of time until new year's in days + +#include "boost/date_time/gregorian/gregorian.hpp" +#include <iostream> + +int +main() +{ + + using namespace boost::gregorian; + + date::ymd_type today = day_clock::local_day_ymd(); + date new_years_day(today.year + 1,1,1); + date_duration dd = new_years_day - date(today); + + std::cout << "Days till new year: " << dd.days() << std::endl; + return 0; +} + +/* Copyright 2001-2004: CrystalClear Software, Inc + * http://www.crystalclearsoftware.com + * + * Subject to the Boost Software License, Version 1.0. + * (See accompanying file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt) + */ + diff --git a/src/boost/libs/date_time/example/gregorian/end_of_month_day.cpp b/src/boost/libs/date_time/example/gregorian/end_of_month_day.cpp new file mode 100644 index 000000000..ecb3919a6 --- /dev/null +++ b/src/boost/libs/date_time/example/gregorian/end_of_month_day.cpp @@ -0,0 +1,46 @@ +/* Simple program that uses the gregorian calendar to find the last + * day of the month and then display the last day of every month left + * in the year. + */ + +#include "boost/date_time/gregorian/gregorian.hpp" +#include <iostream> + +int +main() +{ + using namespace boost::gregorian; + + std::cout << " Enter Year(ex: 2002): "; + int year, month; + std::cin >> year; + std::cout << " Enter Month(1..12): "; + std::cin >> month; + try { + int day = gregorian_calendar::end_of_month_day(year,month); + date end_of_month(year,month,day); + + //Iterate thru by months -- + month_iterator mitr(end_of_month,1); + date start_of_next_year(year+1, Jan, 1); + //loop thru the days and print each one + while (mitr < start_of_next_year){ + std::cout << to_simple_string(*mitr) << std::endl; + ++mitr; + } + + } + catch(...) { + std::cout << "Invalid Date Entered" << std::endl; + } + return 0; + +} + +/* Copyright 2001-2004: CrystalClear Software, Inc + * http://www.crystalclearsoftware.com + * + * Subject to the Boost Software License, Version 1.0. + * (See accompanying file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt) + */ + diff --git a/src/boost/libs/date_time/example/gregorian/find_last_day_of_months.cpp b/src/boost/libs/date_time/example/gregorian/find_last_day_of_months.cpp new file mode 100644 index 000000000..b0c3640be --- /dev/null +++ b/src/boost/libs/date_time/example/gregorian/find_last_day_of_months.cpp @@ -0,0 +1,54 @@ +/* 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 <iostream> + +int +main() +{ + using namespace boost::gregorian; + + greg_year year(1400); + greg_month month(1); + + // get a month and a year from the user + try { + greg_year::value_type y; + greg_month::value_type 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(const bad_year& by) { + std::cout << "Invalid Year Entered: " << by.what() << '\n' + << "Using minimum values for month and year." << std::endl; + } + catch(const 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.txt or http://www.boost.org/LICENSE_1_0.txt) + */ + diff --git a/src/boost/libs/date_time/example/gregorian/localization.cpp b/src/boost/libs/date_time/example/gregorian/localization.cpp new file mode 100644 index 000000000..25856f638 --- /dev/null +++ b/src/boost/libs/date_time/example/gregorian/localization.cpp @@ -0,0 +1,100 @@ +/* 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). + */ + +#include "boost/date_time/gregorian/gregorian.hpp" +#include <iostream> +#include <algorithm> + +/* 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", + "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_long_weekday_names[] = +{ + "Sonntag", "Montag", "Dienstag", "Mittwoch", + "Donnerstag", "Freitag", "Samstag" +}; +const char* const de_short_weekday_names[] = +{ + "Son", "Mon", "Die","Mit", "Don", "Fre", "Sam" +}; + + +int main() +{ + using namespace boost::gregorian; + + // create some gregorian objects to output + date d1(2002, Oct, 1); + 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 + + german_facet->month_format("%B"); + std::cout << m << std::endl; //Oktober + + german_facet->weekday_format("%A"); + std::cout << wd << std::endl; //Dienstag + + + // 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)); + + 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 + + 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.txt or http://www.boost.org/LICENSE_1_0.txt) + */ + diff --git a/src/boost/libs/date_time/example/gregorian/month_add.cpp b/src/boost/libs/date_time/example/gregorian/month_add.cpp new file mode 100644 index 000000000..ee3f05ef8 --- /dev/null +++ b/src/boost/libs/date_time/example/gregorian/month_add.cpp @@ -0,0 +1,58 @@ +/* Simple program that uses the gregorian calendar to progress by exactly + * one month, regardless of how many days are in that month. + * + * This method can be used as an alternative to iterators + */ + +#include "boost/date_time/gregorian/gregorian.hpp" +#include <iostream> + +int +main() +{ + + using namespace boost::gregorian; + + date d = day_clock::local_day(); + date d2 = d + months(1); + date d3 = d - months(1); + std::cout << "Today is: " << to_simple_string(d) << ".\n" + << "One month from today will be: " << to_simple_string(d2) << ".\n" + << "One month ago was: " << to_simple_string(d3) + << std::endl; + std::cout << "******** Warning read this ***********************\n"; + std::cout << "Be aware that adding a month is not exactly like regular numeric math.\n" + << "Addition/Subtraction of months will 'snap to the end' of a month that\n" + << "is shorter than the current month. For example consider " + << "Jan 31, 2004 + (1 month)\n"; + date d4(2004, Jan, 31); + date d5 = d4 + months(1); + std::cout << "Result is: " << to_simple_string(d5) + << std::endl; + + std::cout << "\nSo what does this mean? It means the result of adding months is order\n" + << "dependent, non-commutative, and may create problems for applications.\n" + << "Consider: \n" + << "Jan 30, 2004 + (1 month) + (1 month) != Jan 30, 2004 + (2 months)\n" + << "Why not? Because Jan 30, 2004 + 1 month is Feb 29 + 1 month is Mar 31st.\n" + << "while Jan 30, 2004 + 2 months is Mar 30th.\n" + << "All of this clears up as long as all the starting dates before the 28th of\n" + << "the month -- then all the behavior follows classical mathematical rules.\n"; + + date d6(2004, Jan, 30); + date d7 = d6 + months(1) + months(1); + date d8 = d6 + months(2); + std::cout << "2004-01-30 + (1 month) + (1 month) is: " << to_simple_string(d7) << ".\n" + << "2004-01-30 + (2 months) is: " << to_simple_string(d8) + << std::endl; + + 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.txt or http://www.boost.org/LICENSE_1_0.txt) + */ + diff --git a/src/boost/libs/date_time/example/gregorian/period_calc.cpp b/src/boost/libs/date_time/example/gregorian/period_calc.cpp new file mode 100644 index 000000000..31875fe95 --- /dev/null +++ b/src/boost/libs/date_time/example/gregorian/period_calc.cpp @@ -0,0 +1,86 @@ +/* +This example demonstrates a simple use of periods for the calculation +of date information. + +The example calculates if a given date is a weekend or holiday +given an exclusion set. That is, each weekend or holiday is +entered into the set as a time interval. Then if a given date +is contained within any of the intervals it is considered to +be within the exclusion set and hence is a offtime. + +Output: +Number Excluded Periods: 5 +20020202/20020203 +20020209/20020210 +20020212/20020212 +20020216/20020217 +In Exclusion Period: 20020216 --> 20020216/20020217 +20020223/20020224 + +*/ + + +#include "boost/date_time/gregorian/gregorian.hpp" +#include <set> +#include <algorithm> +#include <iostream> + +typedef std::set<boost::gregorian::date_period> date_period_set; + +//Simple population of the exclusion set +date_period_set +generateExclusion() +{ + using namespace boost::gregorian; + date_period periods_array[] = + { date_period(date(2002,Feb,2), date(2002,Feb,4)),//weekend of 2nd-3rd + date_period(date(2002,Feb,9), date(2002,Feb,11)), + date_period(date(2002,Feb,16), date(2002,Feb,18)), + date_period(date(2002,Feb,23), date(2002,Feb,25)), + date_period(date(2002,Feb,12), date(2002,Feb,13))//a random holiday 2-12 + }; + const int num_periods = sizeof(periods_array)/sizeof(date_period); + + date_period_set ps; + //insert the periods in the set + std::insert_iterator<date_period_set> itr(ps, ps.begin()); + std::copy(periods_array, periods_array+num_periods, itr ); + return ps; + +} + + +int main() +{ + using namespace boost::gregorian; + + date_period_set ps = generateExclusion(); + std::cout << "Number Excluded Periods: " << ps.size() << std::endl; + + date d(2002,Feb,16); + date_period_set::const_iterator i = ps.begin(); + //print the periods, check for containment + for (;i != ps.end(); i++) { + std::cout << to_iso_string(*i) << std::endl; + //if date is in exclusion period then print it + if (i->contains(d)) { + std::cout << "In Exclusion Period: " + << to_iso_string(d) << " --> " << to_iso_string(*i) + << std::endl; + } + } + + return 0; + +} + + + + +/* Copyright 2001-2004: CrystalClear Software, Inc + * http://www.crystalclearsoftware.com + * + * Subject to the Boost Software License, Version 1.0. + * (See accompanying file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt) + */ + diff --git a/src/boost/libs/date_time/example/gregorian/print_holidays.cpp b/src/boost/libs/date_time/example/gregorian/print_holidays.cpp new file mode 100644 index 000000000..2d2fa53b1 --- /dev/null +++ b/src/boost/libs/date_time/example/gregorian/print_holidays.cpp @@ -0,0 +1,83 @@ +/* Generate a set of dates using a collection of date generators + * Output looks like: + * Enter Year: 2002 + * 2002-Jan-01 [Tue] + * 2002-Jan-21 [Mon] + * 2002-Feb-12 [Tue] + * 2002-Jul-04 [Thu] + * 2002-Sep-02 [Mon] + * 2002-Nov-28 [Thu] + * 2002-Dec-25 [Wed] + * Number Holidays: 7 + */ + +#include "boost/date_time/gregorian/gregorian.hpp" +#include <algorithm> +#include <functional> +#include <vector> +#include <iostream> +#include <set> + +void +print_date(boost::gregorian::date d) +{ + using namespace boost::gregorian; +#if defined(BOOST_DATE_TIME_NO_LOCALE) + std::cout << to_simple_string(d) << " [" << d.day_of_week() << "]\n"; +#else + std::cout << d << " [" << d.day_of_week() << "]\n"; +#endif +} + + +int +main() { + + using namespace boost::gregorian; + + std::cout << "Enter Year: "; + greg_year::value_type year; + std::cin >> year; + + //define a collection of holidays fixed by month and day + std::vector<year_based_generator*> holidays; + holidays.push_back(new partial_date(1,Jan)); //Western New Year + holidays.push_back(new partial_date(4,Jul)); //US Independence Day + holidays.push_back(new partial_date(25, Dec));//Christmas day + + + //define a shorthand for the nth_day_of_the_week_in_month function object + typedef nth_day_of_the_week_in_month nth_dow; + + //US labor day + holidays.push_back(new nth_dow(nth_dow::first, Monday, Sep)); + //MLK Day + holidays.push_back(new nth_dow(nth_dow::third, Monday, Jan)); + //Pres day + holidays.push_back(new nth_dow(nth_dow::second, Tuesday, Feb)); + //Thanksgiving + holidays.push_back(new nth_dow(nth_dow::fourth, Thursday, Nov)); + + typedef std::set<date> date_set; + date_set all_holidays; + + for(std::vector<year_based_generator*>::iterator it = holidays.begin(); + it != holidays.end(); ++it) + { + all_holidays.insert((*it)->get_date(year)); + } + + //print the holidays to the screen + std::for_each(all_holidays.begin(), all_holidays.end(), print_date); + std::cout << "Number Holidays: " << all_holidays.size() << std::endl; + + return 0; +} + +/* Copyright 2001-2004: CrystalClear Software, Inc + * http://www.crystalclearsoftware.com + * + * Subject to the Boost Software License, Version 1.0. + * (See accompanying file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt) + */ + diff --git a/src/boost/libs/date_time/example/gregorian/print_month.cpp b/src/boost/libs/date_time/example/gregorian/print_month.cpp new file mode 100644 index 000000000..34e741aea --- /dev/null +++ b/src/boost/libs/date_time/example/gregorian/print_month.cpp @@ -0,0 +1,64 @@ +/* This example prints all the dates in a month. It demonstrates + * the use of iterators as well as functions of the gregorian_calendar + * + * Output: + * Enter Year: 2002 + * Enter Month(1..12): 2 + * 2002-Feb-01 [Fri] + * 2002-Feb-02 [Sat] + * 2002-Feb-03 [Sun] + * 2002-Feb-04 [Mon] + * 2002-Feb-05 [Tue] + * 2002-Feb-06 [Wed] + * 2002-Feb-07 [Thu] + */ + +#include "boost/date_time/gregorian/gregorian.hpp" +#include <iostream> + +int +main() +{ + using namespace boost::gregorian; + + std::cout << "Enter Year: "; + greg_year::value_type year; + greg_month::value_type month; + std::cin >> year; + std::cout << "Enter Month(1..12): "; + std::cin >> month; + + try { + //Use the calendar to get the last day of the month + greg_day::value_type eom_day = gregorian_calendar::end_of_month_day(year,month); + date endOfMonth(year,month,eom_day); + + //construct an iterator starting with firt day of the month + day_iterator ditr(date(year,month,1)); + //loop thru the days and print each one + for (; ditr <= endOfMonth; ++ditr) { +#if defined(BOOST_DATE_TIME_NO_LOCALE) + std::cout << to_simple_string(*ditr) << " [" +#else + std::cout << *ditr << " [" +#endif + << ditr->day_of_week() << " week: " + << ditr->week_number() << "]" + << std::endl; + } + } + catch(std::exception& e) { + + std::cout << "Error bad date, check your entry: \n" + << " Details: " << e.what() << std::endl; + } + return 0; +} + +/* Copyright 2001-2004: CrystalClear Software, Inc + * http://www.crystalclearsoftware.com + * + * Subject to the Boost Software License, Version 1.0. + * (See accompanying file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt) + */ + diff --git a/src/boost/libs/date_time/example/local_time/calc_rules.cpp b/src/boost/libs/date_time/example/local_time/calc_rules.cpp new file mode 100644 index 000000000..f8a21ed49 --- /dev/null +++ b/src/boost/libs/date_time/example/local_time/calc_rules.cpp @@ -0,0 +1,47 @@ +/* A simple example for creating various dst_calc_rule instances + */ + +#include "boost/date_time/gregorian/gregorian.hpp" +#include "boost/date_time/local_time/local_time.hpp" +#include <iostream> + +int +main() +{ + using namespace boost; + using namespace local_time; + using namespace gregorian; + + /***** create the necessary date_generator objects *****/ + // starting generators + first_day_of_the_week_in_month fd_start(Sunday, May); + last_day_of_the_week_in_month ld_start(Sunday, May); + nth_day_of_the_week_in_month nkd_start(nth_day_of_the_week_in_month::third, + Sunday, May); + partial_date pd_start(1, May); + // ending generators + first_day_of_the_week_in_month fd_end(Sunday, Oct); + last_day_of_the_week_in_month ld_end(Sunday, Oct); + nth_day_of_the_week_in_month nkd_end(nth_day_of_the_week_in_month::third, + Sunday, Oct); + partial_date pd_end(31, Oct); + + /***** create the various dst_calc_rule objects *****/ + dst_calc_rule_ptr pdr(new partial_date_dst_rule(pd_start, pd_end)); + dst_calc_rule_ptr flr(new first_last_dst_rule(fd_start, ld_end)); + dst_calc_rule_ptr llr(new last_last_dst_rule(ld_start, ld_end)); + dst_calc_rule_ptr nlr(new nth_last_dst_rule(nkd_start, ld_end)); + dst_calc_rule_ptr ndr(new nth_day_of_the_week_in_month_dst_rule(nkd_start, nkd_end)); + + std::cout << "Program run successfully" << std::endl; + + 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.txt or http://www.boost.org/LICENSE_1_0.txt) + */ + diff --git a/src/boost/libs/date_time/example/local_time/flight.cpp b/src/boost/libs/date_time/example/local_time/flight.cpp new file mode 100644 index 000000000..170f482b4 --- /dev/null +++ b/src/boost/libs/date_time/example/local_time/flight.cpp @@ -0,0 +1,60 @@ + +#include "boost/date_time/local_time/local_time.hpp" +#include <iostream> + +/* This example shows a program that calculates the arrival time of a plane + * that flys from Phoenix to New York. During the flight New York shifts + * into daylight savings time (Phoenix doesn't because Arizona doesn't use + * DST). + * + * + */ + +int main() +{ + using namespace boost::gregorian; + using namespace boost::local_time; + using namespace boost::posix_time; + + + //setup some timezones for creating and adjusting local times + //This user editable file can be found in libs/date_time/data. + tz_database tz_db; + try { + tz_db.load_from_file("../../data/date_time_zonespec.csv"); + }catch(const data_not_accessible& dna) { + std::cerr << "Error with time zone data file: " << dna.what() << std::endl; + exit(EXIT_FAILURE); + }catch(const bad_field_count& bfc) { + std::cerr << "Error with time zone data file: " << bfc.what() << std::endl; + exit(EXIT_FAILURE); + } + time_zone_ptr nyc_tz = tz_db.time_zone_from_region("America/New_York"); + //Use a newly created time zone rule + time_zone_ptr phx_tz(new posix_time_zone("MST-07:00:00")); + + //local departure time in Phoenix is 11 pm on March 13 2010 + // (NY changes to DST on March 14 at 2 am) + local_date_time phx_departure(date(2010, Mar, 13), hours(23), + phx_tz, + local_date_time::NOT_DATE_TIME_ON_ERROR); + local_date_time nyc_departure = phx_departure.local_time_in(nyc_tz); + + time_duration flight_length = hours(4) + minutes(30); + local_date_time phx_arrival = phx_departure + flight_length; + local_date_time nyc_arrival = phx_arrival.local_time_in(nyc_tz); + + std::cout << "departure PHX time: " << phx_departure << std::endl; + std::cout << "departure NYC time: " << nyc_departure << std::endl; + std::cout << "arrival PHX time: " << phx_arrival << std::endl; + std::cout << "arrival NYC time: " << nyc_arrival << std::endl; + +} + + +/* Copyright 2005: CrystalClear Software, Inc + * http://www.crystalclearsoftware.com + * + * Subject to the Boost Software License, Version 1.0. + * (See accompanying file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt) + */ diff --git a/src/boost/libs/date_time/example/local_time/local_date_time.cpp b/src/boost/libs/date_time/example/local_time/local_date_time.cpp new file mode 100644 index 000000000..8e1669526 --- /dev/null +++ b/src/boost/libs/date_time/example/local_time/local_date_time.cpp @@ -0,0 +1,45 @@ +#include "boost/date_time/gregorian/gregorian.hpp" +#include "boost/date_time/posix_time/posix_time.hpp" +#include "boost/date_time/local_time/local_time.hpp" +#include <iostream> +#include <locale> + +int main() { + using namespace boost::gregorian; + using namespace boost::posix_time; + using namespace boost::local_time; + + tz_database tz_db; + try { + tz_db.load_from_file("../../data/date_time_zonespec.csv"); + }catch(data_not_accessible dna) { + std::cerr << "Error with time zone data file: " << dna.what() << std::endl; + exit(EXIT_FAILURE); + }catch(bad_field_count bfc) { + std::cerr << "Error with time zone data file: " << bfc.what() << std::endl; + exit(EXIT_FAILURE); + } + + time_zone_ptr nyc = tz_db.time_zone_from_region("America/New_York"); + local_date_time ny_time(date(2004, Aug, 30), hours(10), nyc, true); + + typedef boost::date_time::time_facet<local_date_time, char> ldt_facet; + ldt_facet* timefacet = new ldt_facet("%Y-%b-%d %H:%M:%S""%F %Z"); + std::locale loc(std::locale::classic(), timefacet); + + std::cout << ny_time << std::endl; + // 2004-Aug-30 10:00:00 EDT + std::cout.imbue(loc); + std::cout << ny_time << std::endl; + // 2004-Aug-30 10:00:00 Eastern Daylight Time + + return 0; +} + + +/* Copyright 2004-2005: CrystalClear Software, Inc + * http://www.crystalclearsoftware.com + * + * Subject to the Boost Software License, Version 1.0. + * (See accompanying file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt) + */ diff --git a/src/boost/libs/date_time/example/local_time/seconds_since_epoch.cpp b/src/boost/libs/date_time/example/local_time/seconds_since_epoch.cpp new file mode 100644 index 000000000..decaf33bb --- /dev/null +++ b/src/boost/libs/date_time/example/local_time/seconds_since_epoch.cpp @@ -0,0 +1,65 @@ +/* This example demonstrates the use of the time zone database and + * local time to calculate the number of seconds since the UTC + * time_t epoch 1970-01-01 00:00:00. Note that the selected timezone + * could be any timezone supported in the time zone database file which + * can be modified and updated as needed by the user. + * + * To solve this problem the following steps are required: + * 1) Get a timezone from the tz database for the local time + * 2) Construct a local time using the timezone + * 3) Construct a posix_time::ptime for the time_t epoch time + * 4) Convert the local_time to utc and subtract the epoch time + * + */ + +#include "boost/date_time/local_time/local_time.hpp" +#include <iostream> + +int main() +{ + using namespace boost::gregorian; + using namespace boost::local_time; + using namespace boost::posix_time; + + tz_database tz_db; + try { + tz_db.load_from_file("../data/date_time_zonespec.csv"); + }catch(const data_not_accessible& dna) { + std::cerr << "Error with time zone data file: " << dna.what() << std::endl; + exit(EXIT_FAILURE); + }catch(const bad_field_count& bfc) { + std::cerr << "Error with time zone data file: " << bfc.what() << std::endl; + exit(EXIT_FAILURE); + } + + time_zone_ptr nyc_tz = tz_db.time_zone_from_region("America/New_York"); + date in_date(2004,10,04); + time_duration td(12,14,32); + // construct with local time value + // create not-a-date-time if invalid (eg: in dst transition) + local_date_time nyc_time(in_date, + td, + nyc_tz, + local_date_time::NOT_DATE_TIME_ON_ERROR); + + std::cout << nyc_time << std::endl; + + ptime time_t_epoch(date(1970,1,1)); + std::cout << time_t_epoch << std::endl; + + // first convert nyc_time to utc via the utc_time() + // call and subtract the ptime. + time_duration diff = nyc_time.utc_time() - time_t_epoch; + + //Expected 1096906472 + std::cout << "Seconds diff: " << diff.total_seconds() << std::endl; + +} + + +/* Copyright 2005: CrystalClear Software, Inc + * http://www.crystalclearsoftware.com + * + * Subject to the Boost Software License, Version 1.0. + * (See accompanying file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt) + */ diff --git a/src/boost/libs/date_time/example/local_time/simple_time_zone.cpp b/src/boost/libs/date_time/example/local_time/simple_time_zone.cpp new file mode 100644 index 000000000..6675774bf --- /dev/null +++ b/src/boost/libs/date_time/example/local_time/simple_time_zone.cpp @@ -0,0 +1,86 @@ +/* A simple example for using a custom_time_zone and a posix_time_zone. + */ + +#include "boost/date_time/local_time/local_time.hpp" +#include <iostream> + +int +main() +{ + using namespace boost; + using namespace local_time; + using namespace gregorian; + using posix_time::time_duration; + + /***** custom_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); + dst_adjustment_offsets adj_offsets(time_duration(1,0,0), + time_duration(2,0,0), + time_duration(2,0,0)); + // rules for this zone are: + // start on first Sunday of April at 2 am + // end on last Sunday of October at 2 am + // so we use a first_last_dst_rule + first_day_of_the_week_in_month start_rule(Sunday, Apr); + last_day_of_the_week_in_month end_rule(Sunday, Oct); + shared_ptr<dst_calc_rule> nyc_rules(new first_last_dst_rule(start_rule, + end_rule)); + // 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); + dst_adjustment_offsets adj_offsets2(time_duration(0,0,0), + time_duration(0,0,0), + time_duration(0,0,0)); + // no dst means we need a null pointer to the rules + shared_ptr<dst_calc_rule> 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 + // 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"; + // nyc_string = "EST-05EDT,M4.1.0,M10.5.0"; // shorter when defaults used + phx_string = "MST-07"; // no-dst + time_zone_ptr nyc_2(new posix_time_zone(nyc_string)); + time_zone_ptr phx_2(new posix_time_zone(phx_string)); + + + /***** show the sets are equal *****/ + + std::cout << "The first zone is in daylight savings from:\n " + << nyc_1->dst_local_start_time(2004) << " through " + << nyc_1->dst_local_end_time(2004) << std::endl; + + std::cout << "The second zone is in daylight savings from:\n " + << nyc_2->dst_local_start_time(2004) << " through " + << nyc_2->dst_local_end_time(2004) << std::endl; + + std::cout << "The third zone (no daylight savings):\n " + << phx_1->std_zone_abbrev() << " and " + << phx_1->base_utc_offset() << std::endl; + + std::cout << "The fourth zone (no daylight savings):\n " + << phx_2->std_zone_abbrev() << " and " + << phx_2->base_utc_offset() << std::endl; + + 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.txt or http://www.boost.org/LICENSE_1_0.txt) + */ + diff --git a/src/boost/libs/date_time/example/posix_time/Jamfile.v2 b/src/boost/libs/date_time/example/posix_time/Jamfile.v2 new file mode 100644 index 000000000..b7bf07e6c --- /dev/null +++ b/src/boost/libs/date_time/example/posix_time/Jamfile.v2 @@ -0,0 +1,13 @@ +# Copyright (c) 2002-2005 CrystalClear Software, Inc. +# Use, modification and distribution is subject to the +# Boost Software License, Version 1.0. (See accompanying +# file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt) + +project + : requirements <library>../../build/boost_date_time + ; + +exe time_math : time_math.cpp ; +exe local_utc_conversion : local_utc_conversion.cpp ; +exe print_hours : print_hours.cpp ; +exe time_periods : time_periods.cpp ; diff --git a/src/boost/libs/date_time/example/posix_time/local_utc_conversion.cpp b/src/boost/libs/date_time/example/posix_time/local_utc_conversion.cpp new file mode 100644 index 000000000..5e712e88e --- /dev/null +++ b/src/boost/libs/date_time/example/posix_time/local_utc_conversion.cpp @@ -0,0 +1,90 @@ +/* Demonstrate conversions between a local time and utc + * Output: + * + * UTC <--> New York while DST is NOT active (5 hours) + * 2001-Dec-31 19:00:00 in New York is 2002-Jan-01 00:00:00 UTC time + * 2002-Jan-01 00:00:00 UTC is 2001-Dec-31 19:00:00 New York time + * + * UTC <--> New York while DST is active (4 hours) + * 2002-May-31 20:00:00 in New York is 2002-Jun-01 00:00:00 UTC time + * 2002-Jun-01 00:00:00 UTC is 2002-May-31 20:00:00 New York time + * + * UTC <--> Arizona (7 hours) + * 2002-May-31 17:00:00 in Arizona is 2002-Jun-01 00:00:00 UTC time + */ + +#include "boost/date_time/posix_time/posix_time.hpp" +#include "boost/date_time/local_time_adjustor.hpp" +#include "boost/date_time/c_local_time_adjustor.hpp" +#include <iostream> + +int +main() +{ + using namespace boost::posix_time; + using namespace boost::gregorian; + + //This local adjustor depends on the machine TZ settings-- highly dangerous! + typedef boost::date_time::c_local_adjustor<ptime> local_adj; + ptime t10(date(2002,Jan,1), hours(7)); + ptime t11 = local_adj::utc_to_local(t10); + std::cout << "UTC <--> Zone base on TZ setting" << std::endl; + std::cout << to_simple_string(t11) << " in your TZ is " + << to_simple_string(t10) << " UTC time " + << std::endl; + time_duration td = t11 - t10; + std::cout << "A difference of: " + << to_simple_string(td) << std::endl; + + + //eastern timezone is utc-5 + typedef boost::date_time::local_adjustor<ptime, -5, us_dst> us_eastern; + + ptime t1(date(2001,Dec,31), hours(19)); //5 hours b/f midnight NY time + + std::cout << "\nUTC <--> New York while DST is NOT active (5 hours)" + << std::endl; + ptime t2 = us_eastern::local_to_utc(t1); + std::cout << to_simple_string(t1) << " in New York is " + << to_simple_string(t2) << " UTC time " + << std::endl; + + ptime t3 = us_eastern::utc_to_local(t2);//back should be the same + std::cout << to_simple_string(t2) << " UTC is " + << to_simple_string(t3) << " New York time " + << "\n\n"; + + ptime t4(date(2002,May,31), hours(20)); //4 hours b/f midnight NY time + std::cout << "UTC <--> New York while DST is active (4 hours)" << std::endl; + ptime t5 = us_eastern::local_to_utc(t4); + std::cout << to_simple_string(t4) << " in New York is " + << to_simple_string(t5) << " UTC time " + << std::endl; + + ptime t6 = us_eastern::utc_to_local(t5);//back should be the same + std::cout << to_simple_string(t5) << " UTC is " + << to_simple_string(t6) << " New York time " + << "\n" << std::endl; + + + //Arizona timezone is utc-7 with no dst + typedef boost::date_time::local_adjustor<ptime, -7, no_dst> us_arizona; + + ptime t7(date(2002,May,31), hours(17)); + std::cout << "UTC <--> Arizona (7 hours)" << std::endl; + ptime t8 = us_arizona::local_to_utc(t7); + std::cout << to_simple_string(t7) << " in Arizona is " + << to_simple_string(t8) << " UTC time " + << std::endl; + + return 0; +} + + +/* Copyright 2001-2004: CrystalClear Software, Inc + * http://www.crystalclearsoftware.com + * + * Subject to the Boost Software License, Version 1.0. + * (See accompanying file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt) + */ + diff --git a/src/boost/libs/date_time/example/posix_time/print_hours.cpp b/src/boost/libs/date_time/example/posix_time/print_hours.cpp new file mode 100644 index 000000000..3877a3c73 --- /dev/null +++ b/src/boost/libs/date_time/example/posix_time/print_hours.cpp @@ -0,0 +1,53 @@ +/* Print the remaining hours of the day + * Uses the clock to get the local time + * Use an iterator to iterate over the remaining hours + * Retrieve the date part from a time + * + * Expected Output something like: + * + * 2002-Mar-08 16:30:59 + * 2002-Mar-08 17:30:59 + * 2002-Mar-08 18:30:59 + * 2002-Mar-08 19:30:59 + * 2002-Mar-08 20:30:59 + * 2002-Mar-08 21:30:59 + * 2002-Mar-08 22:30:59 + * 2002-Mar-08 23:30:59 + * Time left till midnight: 07:29:01 + */ + +#include "boost/date_time/posix_time/posix_time.hpp" +#include <iostream> + +int +main() +{ + using namespace boost::posix_time; + using namespace boost::gregorian; + + //get the current time from the clock -- one second resolution + ptime now = second_clock::local_time(); + //Get the date part out of the time + date today = now.date(); + date tomorrow = today + days(1); + ptime tomorrow_start(tomorrow); //midnight + + //iterator adds by one hour + time_iterator titr(now,hours(1)); + for (; titr < tomorrow_start; ++titr) { + std::cout << to_simple_string(*titr) << std::endl; + } + + time_duration remaining = tomorrow_start - now; + std::cout << "Time left till midnight: " + << to_simple_string(remaining) << std::endl; + return 0; +} + +/* Copyright 2001-2004: CrystalClear Software, Inc + * http://www.crystalclearsoftware.com + * + * Subject to the Boost Software License, Version 1.0. + * (See accompanying file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt) + */ + diff --git a/src/boost/libs/date_time/example/posix_time/time_math.cpp b/src/boost/libs/date_time/example/posix_time/time_math.cpp new file mode 100644 index 000000000..99793ebc7 --- /dev/null +++ b/src/boost/libs/date_time/example/posix_time/time_math.cpp @@ -0,0 +1,36 @@ +/* Some simple examples of constructing and calculating with times + * Output: + * 2002-Feb-01 00:00:00 - 2002-Feb-01 05:04:02.001000000 = -5:04:02.001000000 + */ + +#include "boost/date_time/posix_time/posix_time.hpp" +#include <iostream> + +int +main() +{ + using namespace boost::posix_time; + using namespace boost::gregorian; + + date d(2002,Feb,1); //an arbitrary date + //construct a time by adding up some durations + ptime t1(d, hours(5)+minutes(4)+seconds(2)+milliseconds(1)); + //construct a new time by subtracting some times + ptime t2 = t1 - hours(5)- minutes(4)- seconds(2)- milliseconds(1); + //construct a duration by taking the difference between times + time_duration td = t2 - t1; + + std::cout << to_simple_string(t2) << " - " + << to_simple_string(t1) << " = " + << to_simple_string(td) << std::endl; + + return 0; +} + +/* Copyright 2001-2004: CrystalClear Software, Inc + * http://www.crystalclearsoftware.com + * + * Subject to the Boost Software License, Version 1.0. + * (See accompanying file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt) + */ + diff --git a/src/boost/libs/date_time/example/posix_time/time_periods.cpp b/src/boost/libs/date_time/example/posix_time/time_periods.cpp new file mode 100644 index 000000000..28e38708b --- /dev/null +++ b/src/boost/libs/date_time/example/posix_time/time_periods.cpp @@ -0,0 +1,58 @@ +/* Some simple examples of constructing and calculating with times + * Returns: + * [2002-Feb-01 00:00:00/2002-Feb-01 23:59:59.999999999] contains 2002-Feb-01 03:00:05 + * [2002-Feb-01 00:00:00/2002-Feb-01 23:59:59.999999999] intersected with + * [2002-Feb-01 00:00:00/2002-Feb-01 03:00:04.999999999] is + * [2002-Feb-01 00:00:00/2002-Feb-01 03:00:04.999999999] + */ + +#include "boost/date_time/posix_time/posix_time.hpp" +#include <iostream> + +using namespace boost::posix_time; +using namespace boost::gregorian; + +//Create a simple period class to contain all the times in a day +class day_period : public time_period +{ +public: + day_period(date d) : time_period(ptime(d),//midnight + ptime(d,hours(24))) + {} + +}; + +int +main() +{ + + date d(2002,Feb,1); //an arbitrary date + //a period that represents a day + day_period dp(d); + ptime t(d, hours(3)+seconds(5)); //an arbitray time on that day + if (dp.contains(t)) { + std::cout << to_simple_string(dp) << " contains " + << to_simple_string(t) << std::endl; + } + //a period that represents part of the day + time_period part_of_day(ptime(d, hours(0)), t); + //intersect the 2 periods and print the results + if (part_of_day.intersects(dp)) { + time_period result = part_of_day.intersection(dp); + std::cout << to_simple_string(dp) << " intersected with\n" + << to_simple_string(part_of_day) << " is \n" + << to_simple_string(result) << std::endl; + } + + + return 0; +} + + +/* Copyright 2001-2004: CrystalClear Software, Inc + * http://www.crystalclearsoftware.com + * + * Subject to the Boost Software License, Version 1.0. + * (See accompanying file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt) + */ + diff --git a/src/boost/libs/date_time/example/tutorial/io_tutorial.cpp b/src/boost/libs/date_time/example/tutorial/io_tutorial.cpp new file mode 100644 index 000000000..165a53f93 --- /dev/null +++ b/src/boost/libs/date_time/example/tutorial/io_tutorial.cpp @@ -0,0 +1,194 @@ +#include <iostream> +#include <boost/date_time/local_time/local_time.hpp> + +int main(){ + using namespace boost::gregorian; + using namespace boost::posix_time; + using namespace boost::local_time; + using namespace std; + + /****** basic use ******/ + date d(2004, Feb, 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" + + + /****** format strings ******/ + 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" + + // extra words in format + 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; + + // matching extra words in input + ss.str("The extended ordinal time 2005-128T12:15 can also be represented as Sunday May 08, 2005"); + ss >> ldt; + cout << ldt << endl; // cout is using default format "2005-May-08 12:15:00 UTC" + + /****** content strings ******/ + // 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<string> long_months(&month_names[0], &month_names[12]); + string day_names[7] = { "sunday", "monday", "tuesday", "wednesday", + "thursday", "friday", "saturday" }; + vector<string> long_days(&day_names[0], &day_names[7]); + + // create date_facet and date_input_facet using all defaults + 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)); + + // replace names in the output facet + date_output->long_month_names(long_months); + date_output->long_weekday_names(long_days); + + // replace names in the input facet + date_input->long_month_names(long_months); + date_input->long_weekday_names(long_days); + + // 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(""); + ss << greg_month(3); + cout << ss.str() << endl; // "march" + ss.str(""); + ss << greg_weekday(3); + cout << ss.str() << endl; // "tuesday" + ss.str(""); + ss << date(2005,Jul,4); + cout << ss.str() << endl; // "2005-july-04" + + + /****** special values ******/ + // 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 objects + // and add them to the facets + string sv[5] = {"nadt","neg_inf", "pos_inf", "min_dt", "max_dt" }; + vector<string> 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" + + /****** date/time periods ******/ + // reset all formats to defaults + date_output->format(date_facet::default_date_format); + date_input->format(date_facet::default_date_format); + date_output->month_format("%b"); // abbrev + date_input->month_format("%b"); // abbrev + date_output->weekday_format("%a"); // abbrev + date_input->weekday_format("%a"); // abbrev + + // create our date_period + date_period dp(date(2005,Mar,1), days(31)); // month of march + + // 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" ); + + // default output + ss.str(""); + ss << dp; + cout << ss.str() << endl; // "[2005-Mar-01/2005-Mar-31]" + + // add out custom parser and formatter to the facets + date_output->period_formatter(per_formatter); + date_input->period_parser(per_parser); + + // custom output + ss.str(""); + ss << dp; + cout << ss.str() << endl; // "from 2005-Feb-01 to 2005-Apr-01 exclusive" + + + /****** date generators ******/ + // custom date_generator phrases + string dg_phrases[9] = { "1st", "2nd", "3rd", "4th", "5th", + "final", "prior to", "following", "in" }; + vector<string> phrases(&dg_phrases[0], &dg_phrases[9]); + + // create our date_generator + first_day_of_the_week_before d_gen(Monday); + + // default output + ss.str(""); + ss << d_gen; + cout << ss.str() << endl; // "Mon before" + + // add our custom strings to the date facets + date_output->date_gen_phrase_strings(phrases); + date_input->date_gen_element_strings(phrases); + + // custom output + ss.str(""); + ss << d_gen; + cout << ss.str() << endl; // "Mon prior to" + + 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.txt or http://www.boost.org/LICENSE_1_0.txt) + */ + |