【问题标题】:Epoch time to date/time format in boost c++boost c ++中的纪元时间到日期/时间格式
【发布时间】:2018-02-28 02:01:40
【问题描述】:

在 Linux 中,我从“/proc/stat”读取纪元时间作为 btime,我想使用 c++ boost 转换为可读的日期和时间格式。

我已经尝试了以下事情并且日期工作正常。

    time_t btime_ = 1505790902; //This is epoch time read from "/proc/stat" file.

    std::wstring currentDate_ = L"";
    boost::gregorian::date current_date_ = 
             boost::posix_time::from_time_t(btime_).date();

    std::wstring year_ = boost::lexical_cast<std::wstring>
                                         (current_date_.year());
    std::wstring day_ = boost::lexical_cast<std::wstring>
                                         (current_date_.day());

在这里,我得到了正确的年份和日期。但是我怎样才能从上面的纪元时间获得时间(HH::MM:SS)?让我提示一下 - 我可以试试。

提前致谢。

【问题讨论】:

    标签: c++ boost boost-date-time


    【解决方案1】:

    只是:

    Live On Coliru

    #include <ctime>
    #include <boost/date_time/posix_time/posix_time_io.hpp>
    
    int main() {
        std::time_t btime_ = 1505790902; //This is epoch time read from "/proc/stat" file.
    
        std::cout << boost::posix_time::from_time_t(btime_) << "\n";
    
        std::cout.imbue(std::locale(std::cout.getloc(), new boost::posix_time::time_facet("%H:%M:%S")));
        std::cout << boost::posix_time::from_time_t(btime_) << "\n";
    }
    

    打印

    2017-Sep-19 03:15:02
    03:15:02
    

    更新

    到评论:

    Live On Coliru

    #include <boost/date_time/posix_time/posix_time_io.hpp>
    #include <boost/date_time/c_local_time_adjustor.hpp>
    
    namespace pt = boost::posix_time;
    namespace g  = boost::gregorian;
    using local_adj = boost::date_time::c_local_adjustor<pt::ptime>;
    
    int main() {
        std::cout.imbue(std::locale(std::cout.getloc(), new pt::time_facet("%H:%M:%S")));
    
        std::time_t btime_ = 1505790902; // This is epoch time read from "/proc/stat" file.
    
        pt::ptime const timestamp = pt::from_time_t(btime_);
    
        std::cout << timestamp << "\n";
    
        // This local adjustor depends on the machine TZ settings
        std::cout << local_adj::utc_to_local(timestamp) << " local time\n";
    }
    

    打印

    + TZ=CEST
    + ./a.out
    03:15:02
    03:15:02 local time
    + TZ=MST
    + ./a.out
    03:15:02
    20:15:02 local time
    

    【讨论】:

    • 谢谢。它是有益的。现在我有一个问题。在这里,我们得到了 UTC 时间 (03:15:02)。我们可以根据系统时区获得这个时间吗?
    • @Neel 那不相关。我刚刚从the docs 中截取了样本并更新了我的答案。看到它Live On Coliru
    【解决方案2】:

    您可以使用time_facet。这是一个打印 UTC 日期/时间的示例:

    std::string PrintDateTime()
    {
        std::stringstream str;
        boost::posix_time::time_facet *facet = new boost::posix_time::time_facet("%d.%m.%Y-%H:%M:%S-UTC");
        str.imbue(std::locale(str.getloc(), facet));
        str << boost::posix_time::second_clock::universal_time(); //your time point goes here
        return str.str();
    }
    

    请注意,您无需担心facet 的内存管理。它已经在 boost 内部得到了处理。

    【讨论】:

    • 这里,纪元时间被使用了。您能否建议我如何使用“btime_”并在 HH:MM:SS 中获得时间?
    • 你只要把对象boost::posix_time::from_time_t(btime_)放在我在代码中注释的地方就可以了。
    猜你喜欢
    • 2016-11-02
    • 2019-11-14
    • 2015-01-23
    • 2019-02-04
    • 1970-01-01
    • 2023-03-27
    • 2018-09-21
    • 2017-06-22
    • 2017-12-28
    相关资源
    最近更新 更多