c++ - 在 boost 单位和持续时间之间转换

标签 c++ boost time boost-units

我正在从事一个涉及许多测量的项目,我想使用 boost 单位来确保正确转换单位。我首先引入了一些 typedef 来简化符号:

#include <boost/units/cmath.hpp>
#include <boost/units/io.hpp>
#include <boost/units/systems/si.hpp>
#include <boost/units/systems/si/io.hpp>

#include <boost/date_time/posix_time/posix_time.hpp>

namespace Time = boost::posix_time;

typedef Time::ptime DateTime;
typedef Time::time_duration Duration;

namespace Units
{
  using namespace boost::units;

  namespace SI
  {
    using namespace boost::units::si;
  }

  template <class U> using Quantity = quantity<U>;

  typedef Quantity<SI::length> Length;
  typedef Quantity<SI::velocity> Velocity;
  typedef Quantity<SI::time> Time;
}

我已经编写了一些代码来使用这些单位计算距离和旅行时间:

// a computation of distances which yields a length
Units::Length distance = origin.distance(destination);

Units::Velocity flight_speed(100 * Units::SI::meter / Units::SI::second);

Units::Time flight_time = distance / flight_speed;

DateTime departure_time = ...

DateTime arrival_time = departure_time + flight_time; // does not work..

这引出了我的问题:是否有一些内置的方法可以在 Duration 之间进行转换? (又名 boost::posix_time::time_duration )和 time_duration Units::Quantity<SI::time> (又名 boost::units::quantity<boost::units::si::time> )?看起来这应该是内置的,但我没有在文档中找到任何关于它的内容。

最佳答案

你必须做的工作:

DateTime arrival_time = departure_time +
     boost::posix_time::seconds(flight_time / Units::SI::second);

当然,您可以将转换隐藏在某种帮助程序中:

static inline DateTime operator+(DateTime const &lhs, Units::Time const &rhs) {
    return lhs + Time::seconds(rhs / Units::SI::second);
}
static inline DateTime operator-(DateTime const &lhs, Units::Time const &rhs) {
    return lhs - Time::seconds(rhs / Units::SI::second);
}

现在你可以写了

auto arrival_time = departure_time + flight_time;

Live On Coliru

#include <boost/units/cmath.hpp>
#include <boost/units/io.hpp>
#include <boost/units/systems/si.hpp>
#include <boost/units/systems/si/io.hpp>

#include <boost/date_time/posix_time/posix_time.hpp>
namespace Time = boost::posix_time;

typedef Time::ptime         DateTime;
typedef Time::time_duration Duration;

namespace Units {
    using namespace boost::units;

    namespace SI {
        using namespace boost::units::si;
    }

    template <class U> using Quantity = quantity<U>;

    typedef Quantity<SI::length>   Length;
    typedef Quantity<SI::velocity> Velocity;
    typedef Quantity<SI::time>     Time;
} // namespace Units

#include <boost/geometry.hpp>
#include <boost/geometry/geometries/point_xy.hpp>
struct MockLocation {
    boost::geometry::model::d2::point_xy<double> point;

    Units::Length distance(MockLocation const& other) const {
        return boost::geometry::distance(point, other.point) * 1000.0 * Units::SI::meter;
    }

    friend std::istream& operator>>(std::istream& is, MockLocation& ml) {
        double x,y;
        if (is >> x >> y)
            ml.point = {x,y};
        return is;
    }
};

#include <iostream>

static inline DateTime operator+(DateTime const &lhs, Units::Time const &rhs) {
    return lhs + Time::seconds(rhs / Units::SI::second);
}
static inline DateTime operator-(DateTime const &lhs, Units::Time const &rhs) {
    return lhs - Time::seconds(rhs / Units::SI::second);
}

int main() try {
    MockLocation origin, destination;
    std::cin.exceptions(std::ios::failbit);
    std::cout << "Enter origin      x,y (km): "; std::cin >> origin;
    std::cout << "Enter destination x,y (km): "; std::cin >> destination;

    // a computation of distances which yields a length
    Units::Length distance = origin.distance(destination);

    Units::Velocity flight_speed(100 * Units::SI::meter / Units::SI::second);

    Units::Time flight_time = distance / flight_speed;

    DateTime departure_time = Time::second_clock::local_time();

    using Period = Time::time_period;
    std::cout 
        << "\nDistance " << distance 
        << " at " << flight_speed 
        << " Schedule: " << Period(departure_time, departure_time+flight_time) 
        << "\n";

} catch(std::ios::failure const& e) {
    std::cerr << "Input error: " << e.what() << "\n";
}

打印

Enter origin      x,y (km): Enter destination x,y (km): 
Distance 721110 m at 100 m s^-1 Schedule: [2018-Feb-23 14:22:55/2018-Feb-23 16:23:05.999999]

关于c++ - 在 boost 单位和持续时间之间转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48948459/

相关文章:

c++ - cppcheck 为 "Redundant code: Found a statement that begins with numeric constant"语句报告 'using'

python - 如何在 Y 时间内打印 X 数量的字符串?

c++ - 如何将 `std::chrono::milliseconds` 转换为 `boost::posix_time::milliseconds`

c++ - 如何使用 boost::python 从 Python 类型中提取包装的 C++ 类型?

java - BackgroundSubtractorMOG2 中的 "history"是什么意思?

c++ - 如何在 Qt 中管理文件(替换、删除...)?

c++ - boost 协程在 Windows x86_64 上不起作用吗?

c++ - 如何在c++中实现函数超时

python - 如何在一段时间后停止Python程序

c++ - 如何概括具有变体/访问者的树结构