添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more

In Linux, i am reading epoch time from "/proc/stat" as btime and i want to convert to readable date and time format with c++ boost.

I have tried below things and date is working properly.

    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());

Here i am getting correct year and day. BUT How can i get time( HH::MM:SS) from above epoch time ? Let me give hint - i can try.

Thanks in Advance.

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";

Prints

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

UPDATE

To the comment:

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";

Prints

+ TZ=CEST
+ ./a.out
03:15:02
03:15:02 local time
+ TZ=MST
+ ./a.out
03:15:02
20:15:02 local time
                Thanks. it is useful. Now I have one question. here we get the time (03:15:02) in UTC. Can we get this time depending on system timezone ?
– Neel
                Sep 22 '17 at 5:10
                @Neel That's unrelated. I just nicked the sample from the docs and updated my answer. See it Live On Coliru
– sehe
                Sep 22 '17 at 10:06

You can use a time_facet. Here's an example that prints UTC date/time:

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();

Notice that you don't need to worry about the memory management of facet. It's taken care of already from within boost.

Here, epoch time is getting used. Can you please suggest how can i use "btime_" and get time in HH:MM:SS ? – Neel Sep 19 '17 at 12:00 You just have to put the object boost::posix_time::from_time_t(btime_) in the place where I made a comment in the code. – The Quantum Physicist Sep 20 '17 at 11:21

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.