Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/153.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用C+中的put#U time以毫秒精度获取当前时间+; 我使用下面的代码获得C++中的当前时间。 std::time_t t = std::time(nullptr); std::time(&t); std::cout << std::put_time(std::localtime(&t), "%X,"); std::time\u t=std::time(nullptr); 标准时间(&t); std::cout_C++_Chrono - Fatal编程技术网

使用C+中的put#U time以毫秒精度获取当前时间+; 我使用下面的代码获得C++中的当前时间。 std::time_t t = std::time(nullptr); std::time(&t); std::cout << std::put_time(std::localtime(&t), "%X,"); std::time\u t=std::time(nullptr); 标准时间(&t); std::cout

使用C+中的put#U time以毫秒精度获取当前时间+; 我使用下面的代码获得C++中的当前时间。 std::time_t t = std::time(nullptr); std::time(&t); std::cout << std::put_time(std::localtime(&t), "%X,"); std::time\u t=std::time(nullptr); 标准时间(&t); std::cout,c++,chrono,C++,Chrono,下面是一个使用一些C++11特性的示例。如果您可以使用C++20,请查看新的功能以获取更多信息,或者查看Howard Hinnants库 被接受的答案是好的,但我想用一个: auto now=std::chrono::system_clock::now(); 这能回答你的问题吗?看到这个答案:。。。你必须用总毫秒减去总秒数来计算毫秒数。Hi@tedlynmo。你是对的。我将其更改回空模板,以便它使用默认时钟格式。但是这个变量需要一个空的模板参数才能编译。@pankycodes啊,当然,你是对的。

下面是一个使用一些C++11
特性的示例。如果您可以使用C++20,请查看新的
功能以获取更多信息,或者查看Howard Hinnants库


被接受的答案是好的,但我想用一个:

auto now=std::chrono::system_clock::now();

这能回答你的问题吗?看到这个答案:。。。你必须用总毫秒减去总秒数来计算毫秒数。Hi@tedlynmo。你是对的。我将其更改回空模板,以便它使用默认时钟格式。但是这个变量需要一个空的模板参数才能编译。@pankycodes啊,当然,你是对的。我做了一个改变,强调这一点!感谢@TEDLYNGOM提供了一个很好的类包装解决方案。但是我想问你,为什么你特别选择了一个friend函数来重载你的“伟大的答案”,但是我建议在最后一分钟之前不要退出类型系统。当您实例化
auto-ms
时,不调用
.count()
并让
ms
成为
std::chrono::millides
。在
duration\u cast中,通过执行
dur%std::chrono::seconds{1}
可以很好地获得剩余部分。这将节省
ms%1000
,虽然正确,但在类型更改时很容易出错或丢失。将
.count()
保存在绝对不可避免的地方,在这种情况下,当流到
os
@BigDaveDev时,我接受了你的建议,它确实变得更好了。
#include <chrono>
#include <cstdint>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <string>
#include <type_traits>

// A C++11 constexpr function template for counting decimals needed for
// selected precision.
template<std::size_t V, std::size_t C = 0,
         typename std::enable_if<(V < 10), int>::type = 0>
constexpr std::size_t log10ish() {
    return C;
}

template<std::size_t V, std::size_t C = 0,
         typename std::enable_if<(V >= 10), int>::type = 0>
constexpr std::size_t log10ish() {
    return log10ish<V / 10, C + 1>();
}

// A class to support using different precisions, chrono clocks and formats
template<class Precision = std::chrono::seconds,
         class Clock = std::chrono::system_clock>
class log_watch {
public:
    // some convenience typedefs and "decimal_width" for sub second precisions
    using precision_type = Precision;
    using ratio_type = typename precision_type::period;
    using clock_type = Clock;
    static constexpr auto decimal_width = log10ish<ratio_type{}.den>();

    static_assert(ratio_type{}.num <= ratio_type{}.den,
                  "Only second or sub second precision supported");
    static_assert(ratio_type{}.num == 1, "Unsupported precision parameter");

    // default format: "%Y-%m-%dT%H:%M:%S"
    log_watch(const std::string& format = "%FT%T") : m_format(format) {}

    template<class P, class C>
    friend std::ostream& operator<<(std::ostream&, const log_watch<P, C>&);

private:
    std::string m_format;
};

template<class Precision, class Clock>
std::ostream& operator<<(std::ostream& os, const log_watch<Precision, Clock>& lw) {
    // get current system clock
    auto time_point = Clock::now();

    // extract std::time_t from time_point
    std::time_t t = Clock::to_time_t(time_point);

    // output the part supported by std::tm
    os << std::put_time(std::localtime(&t), lw.m_format.c_str());

    // only involve chrono duration calc for displaying sub second precisions
    if(lw.decimal_width) { // if constexpr( ... in C++17
        // get duration since epoch
        auto dur = time_point.time_since_epoch();

        // extract the sub second part from the duration since epoch
        auto ss =
            std::chrono::duration_cast<Precision>(dur) % std::chrono::seconds{1};

        // output the sub second part
        os << std::setfill('0') << std::setw(lw.decimal_width) << ss.count();
    }

    return os;
}

int main() {
    // default precision, clock and format
    log_watch<> def_cp; // <= C++14
    // log_watch def;   // >= C++17

    // alt. precision using alternative formats
    log_watch<std::chrono::milliseconds> milli("%X,");
    log_watch<std::chrono::microseconds> micro("%FT%T.");
    // alt. precision and clock - only supported if the clock is an alias for
    // system_clock
    log_watch<std::chrono::nanoseconds,
              std::chrono::high_resolution_clock> nano("%FT%T.");

    std::cout << "def_cp: " << def_cp << "\n";
    std::cout << "milli : " << milli << "\n";
    std::cout << "micro : " << micro << "\n";
    std::cout << "nano  : " << nano << "\n";
}
def_cp: 2019-11-21T13:44:07
milli : 13:44:07,871
micro : 2019-11-21T13:44:07.871939
nano  : 2019-11-21T13:44:07.871986585