我想使用 std::chrono::duration ,例如 C1>表示" 10秒",如下: std::chrono::duration millisecs = 10s; 但是,我遇到了这个错误: main.cpp:20:17: error: unable to find numeric literal operator 'operator""s' millisecs = 20s; main.cpp:22:17: note: use -std=gnu++11 or -fext-numeric-literals to enable more built-in suffixes 我已经添加到我的GCC编译命令中: g++ -fext-numeric-literals --std=c++17 -Wall -pedantic -Wextra \ main.cpp -O0 -g -o go 我在做什么错?
以下是关于 chrono 的编程技术问答
我很好奇是否有一种惯用方法来检查chrono::DateTime是否在时间范围内.在我的用例中,我只需要检查DateTime是否在接下来的半小时内,从当前时间开始. 这就是我到目前为止的组合.它使用timestamp()属性来获得我可以使用的RAW(UNIX)时间戳. use chrono::prelude::*; use chrono::Duration; #[inline(always)] pub fn in_next_half_hour(input_dt: DateTime) -> bool { in_future_range(input_dt, 30 * 60) } /// Check if a `DateTime` occurs within the following X seconds from now. pub fn in_future_range(input_dt: DateTime, range_seconds:
std::chrono提供几个时钟来测量时间.同时,我想CPU可以评估时间的唯一方法是计算周期. 问题1: CPU或GPU是否有其他评估时间的方法,而不是计算周期? 如果是这种情况,因为计算机计数循环永远不会像原子时钟那样精确,这意味着计算机的"第二"(period = std::ratio)实际上可以短或比实际更大其次,从长远来看,计算机时钟之间的时间测量差异,例如GPS. 问题2:是正确的吗? 某些硬件具有不同的频率(例如空闲模式和涡轮模式).在这种情况下,这意味着在一秒钟内的周期数将有所不同. 问题3:通过CPU和GPU测量的"循环计数"是否取决于硬件频率?如果是,那么std::chrono如何处理?如果没有,一个周期与什么相对应(例如什么是"基本"时间)?有没有办法在编译时访问转换?有没有办法在运行时访问转换? 解决方案 计数周期,是的,但是的周期? 在现代X86上,内核(内部和clock_gettime和其他系统调用)使用的时间
在标准纸P0092R1中,霍华德·辛南特(Howard Hinnant)写道: template {}>> constexpr To floor(const duration& d) { To t = duration_cast(d); if (t > d) --t; return t; } 此代码如何工作?问题是std::chrono::duration上的operator--不是constexpr操作.它被定义为: duration& operator--(); 但此代码编译,并在编译时间给出正确的答案: static_assert(floor(minutes{3}).count() == 0,
我有一个以下编译器(VS2012)错误: 错误3错误C2679:二进制'+=':找不到操作员 'const std :: chrono ::持续时间'的右手操作数 (或没有可接受的转换)c:\ program文件 (x86)\ Microsoft Visual Studio 11.0 \ VC \ include \ Chrono 749 我对持续时间的定义是: // Tick interval type in nanoseconds typedef std::chrono::duration> tick_interval_type; 当我使用float时相同的错误...仅当持续时间是整数时才编译. 有人可以帮忙吗? 编辑(输出更完整的日志): c:\ program文件(x86)\ Microsoft Visual Studio 11.0 \ vc \ include \ chro
我试图测量在我的代码中执行特定函数所花费的时间.最初,我将clock()函数如下 clock_t start = clock(); do_something(); clock_t end = clock(); printf("Time taken: %f ms\n", ((double) end - start)*1000/CLOCKS_PER_SEC); 后来,我正在阅读C++11中的chrono库,并试图用以下 的std::chrono::steady_clock测量std::chrono::steady_clock using namespace std::chrono; auto start = steady_clock::now(); do_something(); auto end = steady_clock::now(); printf("Time taken: %lld ms\n", duration_cast(end -
c ++ 11定义high_resolution_clock,并具有成员类型period和rep.但是我无法弄清楚如何获得该时钟的精度. 或者,如果我无法达到精确度,我至少可以在tick之间至少在最小代表性持续时间的纳米秒中获得计数吗?可能使用period? #include #include void printPrec() { std::chrono::high_resolution_clock::rep x = 1; // this is not the correct way to initialize 'period': //high_resolution_clock::period y = 1; std::cout
起初,我认为它可以用于性能测量.但这是说 std::chrono::high_resolution_clock可能不稳定(is_steady可能是false) .还说std::chrono::high_resolution_clock甚至可能是std::chrono::system_clock的别名,通常不稳定.因此,我无法用这种类型的时钟测量时间间隔,因为在任何时候可能会调整时钟,并且我的测量结果是错误的. 同时,我无法将std::chrono::high_resolution_clock的时间点转换为日历时间,因为它没有to_time_t方法.因此,我也无法使用这种类型的时钟获得实时. 那么std::chrono::high_resolution_clock可以使用什么? 解决方案 没有. 对不起,如果您想使用high_resolution_clock,请选择steady_clock.无论如何,在libc ++和vs high_resolution_cloc
首先,大家好!这是我在这里的第一个问题,所以我希望我不会搞砸.在这里写作之前,我谷歌搜索了很多.我是编码的新手,是C ++,我自己学习. 考虑到我被告知,这是一个很好的做法(我可能在这里可能错了)一次只播种任何随机引擎,什么是从随机标准库中使用std::mt19937的正确/最佳/更有效的方法在班级内部,由std::chrono::high_resolution_clock::now().time_since_epoch().count()从Chrono Standard库中播种? 我想使用该计时值,因为它的变化很快,并且会产生令人毛骨悚然的数字.我从未考虑过std::random_device,因为我认为这有点阴暗.我可能再次错了. 编辑:在大多数情况下,我用C4Droid IDE在Android手机上学习并学习,因为我没有太多空闲时间坐在合适的计算机上,所以这就是为什么我认为std::random_device不是真正可靠的. 我在知道什么是课之前就成功完成了
我的功能在C ++ 14下使用date.h库,但是我正在将程序转换为使用C ++ 20,并且不再工作.我在做什么错? 我的C ++ 14/date.h代码如下: #include // latest, installed via vcpkg #include auto StringToUnix(const std::string& source) -> std::time_t { auto in = std::istringstream(source); auto tp = date::sys_seconds{}; in >> date::parse("%Y-%m-%d %H:%M:%S", tp); return std::chrono::system_clock::to_time_t(tp); } 我转换后的C ++ 20函数如下: #include auto
我正在编写一些必须处理NTP时间的代码.我决定使用std::chrono::duration代表它们,我希望这会使我的时间数学更容易. NTP时间由一个无符号的64位整数表示,自时代以来的秒数在高32位,而小数秒为低32位.时期日期是1900年1月1日,但这与我在这里处理的问题是一个不同的问题,我已经知道如何处理.对于我正在与之合作的示例,假设1970年1月1日的时代使数学更简单. 持续时间表示很简单:std::chrono::duration>.当我试图在NTP时间和我的系统时钟之间转换时,问题出在其中. 在我的系统上,std::chrono::system_clock::duration是std::chrono::duration.当我尝试使用std::chrono::duration_cast在这两个持续时间之间施放时,我
因此,我从理解这个问题,用于构建A chrono::year 对应于 anno domini 0. 的起源 所以我的问题是,如果我想获得当前chrono::year怎么办.有功能吗?我显然可以做: const auto time = std::time(nullptr); const auto current_date = *std::gmtime(&time); const chrono::year foo{ current_date.tm_year + 1900 }; ,但这似乎是一个非常复杂的过程.我有什么更好的可用吗? 解决方案 using namespace std::chrono; year_month_day ymd = floor(system_clock::now()); const year foo = ymd.year(); 其他解决方案 这是另一种方法: auto now = std::chrono::system_c
给定了新的 C ++ 20中的设施,我如何在日期添加一些天(例如n)? 当我尝试此操作时,我会收到一个编译时间错误: auto d = July/4/2020; auto d2 = d + days{5}; ~ ^ ~~~~~~~ error: invalid operands to binary expression ('std::chrono::year_month_day' and 'std::chrono::days') 解决方案 中的"日期"只是纪念日的时间点.和 "日期时间"只是一个chrono::time_point(通常基于 system_clock)比days更精确.和规范的"日期类型" 是: chrono::time_point 这不过是days -crecision time_point sys
例如,计算一年中一天的各种事实: 给定任意日期,如何如何使用 c ++ 20 Chrono规范来计算这些数字? 解决方案 对于 c ++ 20 Chrono Specification ,这非常容易.在下面,我显示了输入任意日期的函数,并将此信息打印到cout.虽然在撰写本文时,但此答案将采用函数的形式: void info(std::chrono::sys_days sd); sys_days是system_clock家族中的日期time_point.这意味着这只是自1970-01-01 00:00:00 UTC以来的几天.类型Alias sys_days是新的,具有C ++ 20,但是自C ++ 11(time_point>>)以来,基础类型已可用.如果您使用下面的代码假定函数本地: using namespace std; using namespace std::chr
C ++ 20 Chrono类型/值month{7}和months{7}之间有什么区别?有两个这样的类似名称不是很困惑吗? 解决方案 是的,在第一次遇到此库时,同时拥有month和months可能会令人困惑.但是,该库中有一致的命名约定,以帮助减少这种困惑.好处是在保留简短的直觉名称的同时明确地分离了不同的语义. months 所有"预定" chrono::duration类型都是复数: nanoseconds microseconds milliseconds seconds minutes hours days weeks months years so months是 chrono::duration type : using months = duration
这是一个非常微不足道的问题.但是我找不到这个微不足道的答案. 我有一个带有微秒精度的时间戳字符串,例如20:38:42.444491.我如何将其转换为适当的时间对象,以便可以及时使用它,等等... 我试图使用例如 #include #include "date.h" using namespace std; // This works fine chrono::system_clock::time_point makeDateTime(const string& s) { istringstream in{s}; chrono::system_clock::time_point tp; in >> date::parse("%d/%m/%y %T", tp); return tp; } // This always returns epoch. How to fix this ? chrono::system_
我正在使用C ++ 11,我写了一个功能以获取当前时间点: template using Clock = std::chrono::time_point; // get current time point template inline Clock getCurrentTimePoint(int8_t timeZone = 0) { return std::chrono::time_point_cast(std::chrono::system_clock::now()) + std::chrono::hours {timezone}; } 但是,我只是测试了该功能,它给了我一个非常奇怪的输出. auto now1
简单的问题,如何使用尽可能少的代码正确转换为std::string? 注意:我不想在cout上使用cout. C ++ 11和C ++ 14溶液被接受. 解决方案 #include "date/date.h" #include int main() { auto s = date::format("%F %T", std::chrono::system_clock::now()); static_assert(std::is_same, ""); } date/date.h找到在这里.它是仅标题库,C ++ 11/14/17.它具有书面文档和a 视频简介. 更新: 在C ++ 20中,语法为: #include #include #include int main() { auto