|
Boost Users : |
From: Michael Fawcett (michael.fawcett_at_[hidden])
Date: 2007-11-09 12:46:55
What is the best way to ensure that one has a positive time_duration?
I'm dealing with hundreds of thousands of data points and need to
gather the points that are temporally near a given time (based on a
duration). For example, find all points in the collection that are
within 1 hour of Feb 2, 5:00pm. I used the following code to verify
that simply multiplying by -1 would give the desired result, but it
would probably be nicer to provide a std::abs overload.
boost::posix_time::ptime lhs1(boost::gregorian::date(2007, 2, 1),
boost::posix_time::hours(1));
boost::posix_time::ptime rhs1(boost::gregorian::date(2007, 2, 2),
boost::posix_time::hours(1));
boost::posix_time::time_duration elapsed1 = lhs1 - rhs1;
elapsed1 *= -1;
boost::posix_time::ptime lhs2(boost::gregorian::date(2007, 2, 2),
boost::posix_time::hours(1));
boost::posix_time::ptime rhs2(boost::gregorian::date(2007, 2, 1),
boost::posix_time::hours(1));
boost::posix_time::time_duration elapsed2 = lhs2 - rhs2;
assert(elapsed1 == elapsed2);
and the actual code is something like (where duration would be 1 hour
in this example):
boost::posix_time::time_duration elapsed = pt.timestamp - rhs.timestamp;
if (elapsed.is_negative())
elapsed *= -1;
return elapsed <= duration;
I realize that one could also use std::max and std::min, but that
seems a bit inefficient, especially since this is a very performance
critical area of our application.
Thanks,
--Michael Fawcett
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net