【发布时间】:2021-08-29 03:49:02
【问题描述】:
我正在尝试检查我的应用程序还剩下多少天,一个来自当前时间,第二个来自来自数据库的 std::string,但每次我尝试使用减去两个日期时
std::chrono::duration<int>
我得到“expected unqualified-d before = token”,不确定 chrono 在我的代码下面期待什么
void Silo::RevisarDiasRestantes(){ // Check how many days are left, if the serial is 00 is for life
// obtain the current time with std::chrono and convert to struct tm * so it can be convert to an std::string
std::time_t now_c;
std::chrono::time_point<std::chrono::system_clock> now;
typedef std::chrono::duration<int> tiempo;
struct tm * timeinfo;
char buffer[80];
now = std::chrono::system_clock::now();
now_c = std::chrono::system_clock::to_time_t(now);
time (&now_c);
timeinfo = localtime(&now_c);
strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", timeinfo);
std::string str(buffer);
Log("Tiempo usando Chrono " + QString::fromStdString(str));
for (int a{0} ; a<1000 ; ) // just an standard for
{
a++;
}
// convert std::string to std::time_t and then convert to a std::chrono
std::string i = str;
std::chrono::time_point<std::chrono::system_clock> end;
struct std::tm tm;
std::istringstream iss;
iss.str(i);
iss >> std::get_time(&tm,"%Y:%m:%d %H:%M:%S");
std::time_t time = mktime(&tm);
end = std::chrono::system_clock::from_time_t(time);
tiempo = end - now; <-------------------- heres the problem
Log( "Diferencia de tiempo: " + QString::number(tiempo.count()));
}
编辑:直到今天我才注意到一件事,如果我尝试使用 istringstream 到 std::get_time 程序编译但它在运行时失败,在动态库中要求“缺少 basic_istringstream”,所以我不能使用那;是否有另一种方法可以将字符串提供给 get_time?
Edit2:直到 JhonFilleau 指出问题,我才注意到,不再加班,谢谢
【问题讨论】:
-
tiempo等价于std::chrono::duration<int>。您正在输入std::chrono::duration<int> = end - now;。你看到问题了吗?这就像int = 2 - 1;。