我在使用chrono编译时遇到问题,这里是代码:
Time.hh
#include <chrono>
class Time
{
protected:
std::chrono::steady_clock::time_point _start_t;
std::chrono::steady_clock::time_point _now;
std::chrono::steady_clock::time_point _time;
public:
Time();
Time(const Time &other);
Time &operator=(const Time &other);
~Time();
public:
void start();
double getDurSec();
double getDurMilSec();
private:
void setNow();
};
Run Code Online (Sandbox Code Playgroud)
编译错误:
g++ -W -Wall -Wextra -I./include -std=c++0x -c -o src/Time/Time.o src/Time/Time.cpp
In file included from src/Time/Time.cpp:11:0:
./include/Time/Time.hh:21:3: error: ‘steady_clock’ in namespace ‘std::chrono’ does not name a type
./include/Time/Time.hh:22:3: error: ‘steady_clock’ in namespace ‘std::chrono’ does not name a type
./include/Time/Time.hh:23:3: error: ‘steady_clock’ in namespace ‘std::chrono’ does …Run Code Online (Sandbox Code Playgroud) 我有一个以毫秒为单位的起始时间点,如下所示:
using namespace std::chrono;
typedef time_point<system_clock, milliseconds> MyTimePoint;
MyTimePoint startTimePoint = time_point_cast<MyTimePoint::duration>(system_clock::time_point(steady_clock::now()));
Run Code Online (Sandbox Code Playgroud)
现在我将有一定的小时数我要添加或减去startTimePoint.
int numHours = -5//or 5 etc (Can be a plus or minus number)
Run Code Online (Sandbox Code Playgroud)
如何将这段时间添加到原始startTimePoint?
我正在尝试最近的std::chronoapi,我发现在64位Linux体系结构和gcc编译器上time_point,duration类和类无法以最大分辨率(纳秒)处理操作系统的最大时间范围.实际上,似乎这些类的存储是一个64位的整数类型,相比于timespec和timeval其内部使用两个64位整数,一个用于秒,一个用于纳秒:
#include <iostream>
#include <chrono>
#include <typeinfo>
#include <time.h>
using namespace std;
using namespace std::chrono;
int main()
{
cout << sizeof(time_point<nanoseconds>) << endl; // 8
cout << sizeof(time_point<nanoseconds>::duration) << endl; // 8
cout << sizeof(time_point<nanoseconds>::duration::rep) << endl; // 8
cout << typeid(time_point<nanoseconds>::duration::rep).name() << endl; // l
cout << sizeof(struct timespec) << endl; // 16
cout << sizeof(struct timeval) << endl; // 16
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在64位Windows(MSVC2017)上,情况非常相似:存储类型也是64位整数.在处理稳定(也称为单调)时钟时这不是问题,但存储限制使得不同的API实现不适合存储更大的日期和更宽的时间跨度,从而为类似Y2K的错误创造了基础.这个问题得到了承认吗?是否有更好的实施或API改进的计划?
所以在c ++ 11中,Chrono Library提供了duration_cast:
计算以可用的最宽类型完成,并且仅在完成时将其转换为结果类型,就像通过static_cast一样
返回ToDuration中可表示的最大持续时间t,该持续时间小于或等于d
因此,对于所有人来说x,这两个调用的结果是相同的:
chrono::duration_cast<chrono::seconds>(x)chrono::floor<chrono::seconds>(x)我有以下代码:
// #includes for <chrono>, <iostream>, etc.
using namespace std;
using namespace chrono;
int main(int argc, char* argv[]) {
auto sysStart = system_clock::now();
LogInit(); // Opens a log file for reading/writing
// Last shutdown time log entry is marked by a preceding null byte
logFile.ignore(numeric_limits<streamsize>::max(), '\0');
if (logFile.fail() || logFile.bad()) {
// Calls GetLastError() and logs the error code and message
LogError("main");
}
// Parse the timestamp at the start of the shutdown log entry
tm end = { 0 …Run Code Online (Sandbox Code Playgroud) 我正在尝试将std::chrono::duration对象格式化为 HH:MM::SS 格式,例如 16:42:02 是小时 (16)、分钟 (42) 和秒 (2)。
该库fmt为此提供了有用的格式说明符:
using namespace std::chrono;
auto start = high_resolution_clock::now();
auto end = start + 4s;
fmt::print("{:%H:%M:%S} \n", end);
Run Code Online (Sandbox Code Playgroud)
不幸的是,它以小数形式打印秒
16:58:55.359425076
Run Code Online (Sandbox Code Playgroud)
我想将其四舍五入到最接近的整数,但无法弄清楚在哪里放置精度说明符(精度 2 仅在测试方面):
fmt::print("{:.2%H:%M:%S} \n", end); // error
fmt::print("{:.2f%H:%M:%S} \n", end); // error
fmt::print("{:%H:%M:.2%S} \n", end); // nonsense: 17:07:.202.454873454
Run Code Online (Sandbox Code Playgroud)
我盯着chrono 格式规范的细节有点迷失......
上面的编译器资源管理器示例在这里。
我完全希望这会在一两天内关闭,因为这是一个主观的话题,但无论如何都要说:为什么在 C++ 中获取日期/时间至少需要 5 行代码?
这是我在 C 中学会的第一件事,但那是很久以前的事了……我记得当时我花了一段时间才掌握整个概念。现在我更有经验了,但是在习惯了 C# 和 Java 等高级语言之后,我真的很恼火,这么简单的事情需要所有这些:
#include <iostream>
#include <chrono>
#include <ctime>
using namespace std::chrono;
// First get an epoch value
auto time = system_clock::to_time_t(system_clock::now());
// Now we need a char buffer to hold the output
char timeString[20] = "";
// Oh and a tm struct too! Gotta have that, just to make it more complicated!
tm tmStruct;
// Oh and BTW, you can't just get your local time directly;
// you …Run Code Online (Sandbox Code Playgroud) 要创建当前时间的时间点,您可以使用:std::chrono::system_clock::now()。
但是,我不知道如何创建自 UNIX 纪元以来给定毫秒数的时间点?
另外,std::chrono::time_point甚至是推荐的方式来及时表示“瞬间”?还是应该std::time_t优先考虑?
我有一个使用 date.h 库在 C++14 下工作的函数,但我正在将程序转换为使用 C++20,但它不再工作。请问我做错了什么?
我的C++14/date.h代码如下:
#include <date/date.h> // latest, installed via vcpkg
#include <chrono>
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);
}
Run Code Online (Sandbox Code Playgroud)
我转换后的C++20函数如下:
#include <chrono>
auto StringToUnix(const std::string& source) -> std::time_t
{
using namespace std::chrono;
auto in = std::istringstream(source);
auto tp = sys_seconds{};
in >> parse("%Y-%m-%d %H:%M:%S", tp);
return system_clock::to_time_t(tp);
}
Run Code Online (Sandbox Code Playgroud)
我在 VS2019 社区(最新)中收到的错误是:
E0304 no instance of overloaded function "parse" matches the …Run Code Online (Sandbox Code Playgroud) std::chrono::parse并std::chrono::from_stream允许我们解析许多不同的日期/时间格式。
但我最近得到了这样的字符串:
2022-07-10 22:00 GMT+2- 我不确定应该使用什么格式。
我尝试过:
%F %T GMT%z- 失败
%F %T GMT%Z- 失败
%F %T %Z- 失败
或者也许我只需要解析时间/日期,然后手动获取区域?
c++-chrono ×10
c++ ×9
c++11 ×4
c++20 ×4
c++17 ×2
compilation ×1
ctime ×1
date ×1
floor ×1
fmt ×1
formatting ×1
g++ ×1
gcc ×1
localtime ×1
time ×1
visual-c++ ×1
year2038 ×1