e26fd2ed98
* Synchronize each miscrosec test to the next second (#109) * Synchronize each miscrosec test to the next second The aim is to avoid false positives in test_microsec when the seconds, minutes or hours change during time read between the second_clock and the microsec_clock. * Improved readability of the microcec_time_clock test * Improve performance of adding and subtracting time durations from a ptime. (#99) Modifying ptime objects by adding and subtracting time durations was inefficient because it extracted the date and time of day and then re-constructed a ptime using the date and modified time of day. This can be avoided by using the existing time_system utilities which perform the operation by adjusting the number of ticks. Performance is improved by a factor of 48 on my system. * Update CI Scripts * Add time_duration helper functions: (#113) 1. is_positive() - Return boolean value to indicate whether or not time duration is positive. 2. is_zero() - Return boolean value to indicate whether or not time duration is zero. 3. abs() - Return a time_duration which is the absolute value of time duration. Added documentation for these helper functions and improved existing documentation to indicate constness, return values or static functions. * Cease dependence on MPL (#115)
66 lines
2.2 KiB
C++
66 lines
2.2 KiB
C++
/* 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)
|
|
*/
|