首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >优雅的C++11时间打印

优雅的C++11时间打印
EN

Stack Overflow用户
提问于 2014-02-27 09:18:33
回答 2查看 3.5K关注 0票数 13

这就是我在C++11中想要做的:给定两个时间点(例如,一个定时类)作为std::chrono::steady_clock::now(),优雅地打印它们的时间差,例如:

代码语言:javascript
复制
1 day 4 hours 3 minutes 45 seconds

代码语言:javascript
复制
32 minutes 54 seconds 345 milliseconds

请注意,我对使用put_time并不感兴趣,因为我想从最重要的时间单位开始打印。我知道,这是一个解决方案,只是打印差异,但这并不漂亮:我正在寻找一个优雅的解决方案:)

干杯!

EN

回答 2

Stack Overflow用户

发布于 2014-02-27 17:31:44

这里有一个易于扩展的解决方案,使用各种模板和递归。为了便于使用,它定义了ostream& operator<<(ostream&, const duration&)

代码语言:javascript
复制
#include <chrono>
#include <iostream>
#include <tuple>

using day_t = std::chrono::duration<long long, std::ratio<3600 * 24>>;

template<typename> struct duration_traits {};

#define DURATION_TRAITS(Duration, Singular, Plural) \
template<> struct duration_traits<Duration> { \
    constexpr static const char* singular = Singular; \
    constexpr static const char* plural = Plural; \
}

DURATION_TRAITS(std::chrono::milliseconds, "millisecond", "milliseconds");
DURATION_TRAITS(std::chrono::seconds, "second", "seconds");
DURATION_TRAITS(std::chrono::minutes, "minute", "minutes");
DURATION_TRAITS(std::chrono::hours, "hour", "hours");
DURATION_TRAITS(day_t, "day", "days");

using divisions = std::tuple<std::chrono::milliseconds, 
                             std::chrono::seconds, 
                             std::chrono::minutes, 
                             std::chrono::hours, 
                             day_t>;

namespace detail {
template<typename...> struct print_duration_impl_ {};

template<typename Head, typename... Tail>
struct print_duration_impl_<Head, Tail...> {
    template <typename Duration>
    static bool print(std::ostream& os, Duration& dur) {
        const auto started_printing = print_duration_impl_<Tail...>::print(os, dur);

        const auto n = std::chrono::duration_cast<Head>(dur);
        const auto count = n.count();

        if (count == 0) {
            return started_printing;
        }

        if (started_printing) {
            os << ' ';
        }

        using traits = duration_traits<Head>;
        os << count << ' ' << (count == 1 ? traits::singular : traits::plural);
        dur -= n;

        return true;
    }
};

template<>
struct print_duration_impl_<> {
    template <typename Duration>
    static bool print(std::ostream& os, Duration& dur) {
        return false;
    }
};

template<typename...> struct print_duration {};

template<typename... Args>
struct print_duration<std::tuple<Args...>> {
    template<typename Duration>
    static void print(std::ostream& os, Duration dur) {
        print_duration_impl_<Args...>::print(os, dur);
    }
};
}

template<typename Rep, typename Period>
std::ostream& operator<<(std::ostream& os, const std::chrono::duration<Rep, Period>& dur) {
    detail::print_duration<divisions>::print(os, dur);
    return os;
}

新的持续时间是通过专门化的duration_traits并在分区中正确的位置插入类型来添加的。例如,添加一个10毫秒的吉菲类型将涉及:

代码语言:javascript
复制
using jiffy_t = std::chrono::duration<long long, std::centi>;
DURATION_TRAITS(jiffy_t, "jiffy", "jiffies");

using divisions = std::tuple<std::chrono::milliseconds, 
                             jiffy_t,
                             std::chrono::seconds, 
                             std::chrono::minutes, 
                             std::chrono::hours, 
                             day_t>;

三行代码还不错!

ideone.com上的实例。

票数 4
EN

Stack Overflow用户

发布于 2014-02-27 09:37:21

代码语言:javascript
复制
template<typename T>
void print_time_diff(std::ostream& out, T prior, T latter)
{
    namespace sc = std::chrono;
    auto diff = sc::duration_cast<sc::milliseconds>(latter - prior).count();
    auto const msecs = diff % 1000;
    diff /= 1000;
    auto const secs = diff % 60;
    diff /= 60;
    auto const mins = diff % 60;
    diff /= 60;
    auto const hours = diff % 24;
    diff /= 24;
    auto const days = diff;

    bool printed_earlier = false;
    if (days >= 1) {
        printed_earlier = true;
        out << days << (1 != days ? " days" : " day") << ' ';
    }
    if (printed_earlier || hours >= 1) {
        printed_earlier = true;
        out << hours << (1 != hours ? " hours" : " hour") << ' ';
    }
    if (printed_earlier || mins >= 1) {
        printed_earlier = true;
        out << mins << (1 != mins ? " minutes" : " minute") << ' ';
    }
    if (printed_earlier || secs >= 1) {
        printed_earlier = true;
        out << secs << (1 != secs ? " seconds" : " second") << ' ';
    }
    if (printed_earlier || msecs >= 1) {
        printed_earlier = true;
        out << msecs << (1 != msecs ? " milliseconds" : " millisecond");
    }
}

http://ideone.com/bBNHQp

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22063979

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档