From 19fcec84d8d7d21e796c7624e521b60d28ee21ed Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 7 Apr 2024 20:45:59 +0200 Subject: Adding upstream version 16.2.11+ds. Signed-off-by: Daniel Baumann --- .../date_time/example/local_time/calc_rules.cpp | 47 ++++++++++++ .../libs/date_time/example/local_time/flight.cpp | 60 +++++++++++++++ .../example/local_time/local_date_time.cpp | 45 +++++++++++ .../example/local_time/seconds_since_epoch.cpp | 65 ++++++++++++++++ .../example/local_time/simple_time_zone.cpp | 86 ++++++++++++++++++++++ 5 files changed, 303 insertions(+) create mode 100644 src/boost/libs/date_time/example/local_time/calc_rules.cpp create mode 100644 src/boost/libs/date_time/example/local_time/flight.cpp create mode 100644 src/boost/libs/date_time/example/local_time/local_date_time.cpp create mode 100644 src/boost/libs/date_time/example/local_time/seconds_since_epoch.cpp create mode 100644 src/boost/libs/date_time/example/local_time/simple_time_zone.cpp (limited to 'src/boost/libs/date_time/example/local_time') 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 + +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 + +/* 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 +#include + +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 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 + +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 + +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 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 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) + */ + -- cgit v1.2.3