Sadly VS2012's duration_cast is broken, and I actually need the functionality which is broken. So, I wrote my own:
template<typename ToUnit, typename Rep, typename Period>
ToUnit duration_cast_2(const std::chrono::duration<Rep, Period>& right)
{
typedef std::ratio_divide<Period, typename ToUnit::period>::type ratio;
typedef std::common_type<std::common_type<typename ToUnit::rep, Rep>::type, intmax_t>::type common_type;
return ToUnit(static_cast<typename ToUnit::rep>(static_cast<common_type>(right.count()) * static_cast<common_type>(ratio::num) / static_cast<common_type>(ratio::den)));
}
But I'm not entirely confident that it's correct.
std::chrono::duration<long long, std::micro>into astd::chrono::duration<double, std::milli>, for example. That conversion cannot be done implicitly, and needs the cast. – Dave Feb 18 at 16:28