我来这里是为了解决令我感到沮丧的情况.很多.首先,我在Windows上,我使用MinGW作为编译器(C++).
我在使用cpp-netlib和SSL(尝试POST到https站点)的程序中遇到了一些问题.我相信一切都井井有条,除了这一个不断躲避我的错误.
C:\boost_1_50_0\boost_1_50_0\stage\lib\libboost_thread-mgw46-mt-1_50.a(thread.o):thread.cpp|| undefined reference to 'boost::chrono::system_clock::now()'
我确信我已经链接到chrono,以及所有的.a libs BOOST_ROOT/stage/lib.我已经尝试重新排序,所以chrono在线程之前被链接.什么都没有帮助.我已经尝试BOOST_CHRONO_INLINED在我的IDE设置和多个conf.hpps中定义它仅使其成为标题,这没有帮助.
我很确定这是一个新手问题,我希望有人能给我一个快速解决方案.我匆匆写了这篇文章,因为我必须在某个地方,但如果你需要更多信息请说出来,我回家后可以更仔细地写.谢谢!
请考虑以下代码(LWS):
#include <iostream>
#include <chrono>
inline void test(
const std::chrono::high_resolution_clock::time_point& first,
const std::chrono::high_resolution_clock::time_point& second)
{
std::cout << first.time_since_epoch().count() << std::endl;
std::cout << second.time_since_epoch().count() << std::endl;
}
int main(int argc, char* argv[])
{
test(std::chrono::high_resolution_clock::now(),
std::chrono::high_resolution_clock::now());
return 0;
}
Run Code Online (Sandbox Code Playgroud)
你必须多次运行它,因为有时,没有明显的区别.但是,当存在的评价的时间之间的可见的差别first和second,结果是下克++以下内容:
1363376239363175
1363376239363174
Run Code Online (Sandbox Code Playgroud)
英特尔和clang下面的以下内容:
1363376267971435
1363376267971436
Run Code Online (Sandbox Code Playgroud)
这意味着在g ++下,second首先评估参数,在intel和clang下first首先评估参数.
根据C++ 11标准,哪一个是真的?
我依赖可能会或可能不会响应的硬件.因此,我经常最终编写具有超时功能.系统时间是脆性单元测试的已知来源,因此注入受控且稳定的时间似乎是测试的好主意.
我想知道std :: chrono中是否有任何设施可以帮助解决这个问题.我看到的替代方法是围绕系统时间编写一个包装器并依赖于该适配器.
以下是包装器外观的最小示例.
#pragma once
#include <memory>
#include <chrono>
#include <thread>
#include <iostream>
using std::chrono::system_clock;
using std::chrono::milliseconds;
using std::shared_ptr;
using std::make_shared;
class Wrapped_Clock
{
public:
virtual system_clock::time_point Now() { return system_clock::now(); }
virtual void Sleep(milliseconds ms) { std::this_thread::sleep_for(ms); }
};
class Mock_Clock : public Wrapped_Clock
{
private:
system_clock::time_point now;
public:
Mock_Clock() : now(system_clock::now()){}
~Mock_Clock() {}
system_clock::time_point Now() { return now; }
void Sleep(milliseconds ms) { }
};
class CanTimeOut
{
private:
shared_ptr<Wrapped_Clock> sclock;
public:
CanTimeOut(shared_ptr<Wrapped_Clock> sclock = make_shared<Wrapped_Clock>()) …Run Code Online (Sandbox Code Playgroud) 在标准论文P0092R1中,Howard Hinnant写道:
template <class To, class Rep, class Period,
class = enable_if_t<detail::is_duration<To>{}>>
constexpr
To floor(const duration<Rep, Period>& d)
{
To t = duration_cast<To>(d);
if (t > d)
--t;
return t;
}
Run Code Online (Sandbox Code Playgroud)
这段代码怎么样?问题是,operator--a std::chrono::duration不是constexpr操作.它被定义为:
duration& operator--();
Run Code Online (Sandbox Code Playgroud)
然而,这段代码编译,并在编译时给出正确的答案:
static_assert(floor<hours>(minutes{3}).count() == 0, "”);
Run Code Online (Sandbox Code Playgroud)
那是怎么回事?
有这个代码:
#include <cstdio>
#include <chrono>
int main()
{
auto d = std::chrono::microseconds(1).count();
printf("%lld", d);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当在64位模式下编译时,会出现警告:
Run Code Online (Sandbox Code Playgroud)main.cpp: In function ‘int main()’: main.cpp:7:19: warning: format ‘%lld’ expects argument of type ‘long long int’, but argument 2 has type ‘long int’ [-Wformat=] printf("%lld", d); ^
在32位模式下编译时使用此警告(使用-m32标志).它看起来就像std::chrono::duration::rep是一个类型的long int在64位程序,并long long int在32位程序.
有没有一种可移植的方式来打印它像%zu说明符size_t?
我正在将一些代码转换boost::filesystem为std::filesystem.使用的先前代码boost::filesystem::last_write_time()返回time_t与time_t我已经持有的对象如此直接的比较是微不足道的.顺便说一下,这个time_t我持有的文件内容很久以前就已经存在了,所以我坚持使用这个"自unix epoch以来的时间"类型.
std::filesystem::last_write_time返回一个std::filesystem::file_time_type.是否有可移植的方式将a转换file_time_type为a time_t或以其他方式可比较两个对象?
#include <ctime>
#include <filesystem>
std::time_t GetATimeInSecondsSince1970Epoch()
{
return 1207609200; // Some time in April 2008 (just an example!)
}
int main()
{
const std::time_t time = GetATimeInSecondsSince1970Epoch();
const auto lastWriteTime = std::filesystem::last_write_time("c:\\file.txt");
// How to portably compare time and lastWriteTime?
}
Run Code Online (Sandbox Code Playgroud)
编辑:请注意cppreference.comlast_write_time上的示例代码表示它假设时钟是std::chrono::system_clock实现该to_time_t功能的状态.这个假设并不总是如此,并不在我的平台上(VS2017).
我正在编写一些必须处理 NTP 时间的代码。我决定用 来代表他们std::chrono::duration,我希望这能让我的时间数学更容易做。
NTP 时间由无符号 64 位整数表示,高 32 位为自纪元以来的秒数,低 32 位为小数秒。纪元日期是 1900 年 1 月 1 日,但这与我在这里处理的问题是一个单独的问题,而且我已经知道如何处理这个问题。对于我正在使用的示例,为了使数学更简单,假设纪元为 1970 年 1 月 1 日。
持续时间的表示很简单:std::chrono::duration<std::uint64_t, std::ratio<1, INTMAX_C(0x100000000)>>。问题在于当我尝试在 NTP 时间和系统时钟之间进行转换时。
在我的系统上,std::chrono::system_clock::duration是std::chrono::duration<int64_t, std::nano>. 当我尝试std::chrono::duration_cast在这两个持续时间之间进行转换时,我在任一方向上都得到了大量的截断。在调试器中跟踪这一点,我发现它与实现有关duration_cast,在 libstdc++、libc++ 和 boost::chrono 中以同样的方式失败。简而言之,要从系统表示转换为 ntp 表示,它乘以比率 8388608/1953125,并在另一个方向上乘以相反的比率。它首先乘以分子,然后除以分母。数学是正确的,但初始乘法溢出了 64 位表示并被截断,尽管实际转换(除法后)仍然可以轻松地用这些位表示。
我可以手动进行此转换,但我的问题如下:
to_sys和from_sys功能的 NTP 时钟,我是否可以简单地使用std::chrono::clock_cast而不必担心其他微妙的问题?这是我用来测试这个的代码和一些示例输出:
// #define BOOST
#ifdef BOOST
# include <boost/chrono.hpp>
# define …Run Code Online (Sandbox Code Playgroud) 我正在尝试记录一段时间内流逝的毫秒时间。
我有一堂这样的课
// class member declarations
class MyClass {
std::chrono::high_resolution_clock::time_point m_start;
std::chrono::system_clock::duration m_elapsed;
};
Run Code Online (Sandbox Code Playgroud)
我在课堂上有两种方法。一个是从 main 调用的func1CalledFromMainThread。
// Class methods
using namespace std::chrono;
void MyClass::func1CalledFromMainThread() {
m_start = std::chrono::high_resolution_clock::now();
}
Run Code Online (Sandbox Code Playgroud)
另一个是func2CalledFromADifferentThread从不同的线程调用的
void MyClass::func2CalledFromADifferentThread() {
// after some time following line of code runs from a different thread
auto end = high_resolution_clock::now();
m_elapsed = duration_cast<milliseconds>(end - m_start);
std::cout << "Elapsed time in milliseconds is " << m_elapsed.count()/1000 << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
问题出在cout日志记录中。我发现我必须除以1000才能得到毫秒m_elapsed。不 …
我正在 C++ 中创建一个RFC3339时间戳,包括毫秒和 UTC 格式,std::chrono如下所示:
#include <chrono>
#include <ctime>
#include <iomanip>
using namespace std;
using namespace std::chrono;
string now_rfc3339() {
const auto now = system_clock::now();
const auto millis = duration_cast<milliseconds>(now.time_since_epoch()).count() % 1000;
const auto c_now = system_clock::to_time_t(now);
stringstream ss;
ss << put_time(gmtime(&c_now), "%FT%T") <<
'.' << setfill('0') << setw(3) << millis << 'Z';
return ss.str();
}
// output like 2019-01-23T10:18:32.079Z
Run Code Online (Sandbox Code Playgroud)
(原谅s using)
有没有更直接的方法来获取毫秒数now?%1000以毫秒为单位now到达那里似乎有点麻烦。或者关于如何做到这一点更惯用的任何其他评论?
我使用Howard Hinnants date.h库编写了以下代码,以计算当前时间的一年中的小数天。我想知道是否有更短的方法,因为我的代码感觉像是对std::chrono和date调用的过度杀伤。我是否可以直接计算自年初以来的小数天数(以微秒为单位),并避免采用两步法?
#include <iostream>
#include <chrono>
#include "date.h"
int main()
{
// Get actual time.
auto now = std::chrono::system_clock::now();
// Get the number of days since start of the year.
auto ymd = date::year_month_day( date::floor<date::days>(now) );
auto ymd_ref = date::year{ymd.year()}/1/1;
int days = (date::sys_days{ymd} - date::sys_days{ymd_ref}).count();
// Get the fractional number of seconds of the day.
auto microseconds = std::chrono::duration_cast<std::chrono::microseconds>(now - date::floor<date::days>(now));
double seconds_since_midnight = 1e-6*microseconds.count();
// Get fractional day number.
std::cout << "Fractional …Run Code Online (Sandbox Code Playgroud) c++-chrono ×10
c++ ×9
c++11 ×5
arguments ×1
boost ×1
boost-thread ×1
c++14 ×1
c++17 ×1
c++20 ×1
constexpr ×1
date ×1
datetime ×1
io ×1
long-integer ×1
milliseconds ×1
rfc3339 ×1
std ×1
systemtime ×1
time ×1
unit-testing ×1