Converting epoch time to “real” date/time

后端 未结 6 677
情歌与酒
情歌与酒 2020-11-30 09:38

What I want to do is convert an epoch time (seconds since midnight 1/1/1970) to \"real\" time (m/d/y h:m:s)

So far, I have the following algorithm, which to me feels

6条回答
  •  孤街浪徒
    2020-11-30 10:31

    An easy way (though different than the format you wanted):

    std::time_t result = std::time(nullptr);
    std::cout << std::asctime(std::localtime(&result));
    

    Output: Wed Sep 21 10:27:52 2011

    Notice that the returned result will be automatically concatenated with "\n".. you can remove it using:

    std::string::size_type i = res.find("\n");
    if (i != std::string::npos)
        res.erase(i, res.length());
    

    Taken from: http://en.cppreference.com/w/cpp/chrono/c/time

提交回复
热议问题