C++ 在增压单位和持续时间之间转换

C++ 在增压单位和持续时间之间转换,c++,boost,time,boost-units,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

我正在从事一个涉及许多测量的项目,我想使用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;
}

这就引出了我的问题:是否有某种内置方法可以在
持续时间
(又称
boost::posix_time::time_Duration
)和
持续时间
单位::数量(又称
boost::Units::Quantity
)之间进行转换?看起来这应该是内置的,但我在文档中没有找到任何关于它的信息。

您必须完成以下工作:

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;

#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";
}
#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]