C++ - 仅比较时间
C++ - Comparing only time
有没有办法只比较C++的时间?
例如,给定一个预定义时间数组(字符串,例如"04:00"、"07:00"、"13:00"),我希望能够将当前时间与数组中给出的时间范围进行比较,看看哪个适合。
我已经尝试了strptime
但是不断收到未定义的错误。我已经包括了time.h
和stdio.h
.
#include <stdio.h>
#include <ctime>
#include <time.h>
int main() {
//GET CURRENT TIME
time_t now = time(0);
tm *ltm = localtime(&now);
cout << "Time: "<< ltm->tm_hour << ":";
cout << ltm->tm_min << ":";
cout << ltm->tm_sec << endl;
//TRYING TO CONVERT THE TIME FROM STRING TO PROCEED FOR FURTHER COMPARISON
struct tm tm;
std::string s("04:00");
if (strptime(s.c_str(), "%H:%M", &tm)) { //error strptime is undefined
int d = tm.tm_mday,
m = tm.tm_mon + 1,
y = tm.tm_year + 1900;
std::cout << y << "-" << m << "-" << d << " "
<< tm.tm_hour << ":" << tm.tm_min;
}
//ATTEMPT TO DO TIME COMPARISON HERE
}
我错过了任何图书馆吗?
有没有更简单/替代的方法可以做到这一点?
请指导我。谢谢
示例中的时间戳都具有相同的固定格式:两位数小时、冒号、两位数分钟。
这意味着它们完全可以通过std::string
、std::strcmp
、std::strncmp
、std::memcmp
等operator<
进行词典比较。
相关文章:
- 为什么字符串比较的 == 运算符相对于任一字符串长度线性时间(似乎)?
- 比较向量中的元素时所花费的时间呈指数级增长
- 与操作员比较两个计时时间点
- 如何使用 CTIME/Chrono Libarey 将开始时间与系统时钟进行比较
- 将系统时间与变量进行比较 - C++
- 如何比较两个经过的时间?
- Rcpp/C++/R:比较日期时间矢量与日期时间时出错(错误:"运算符>"的不明确重载)
- C++ - 仅比较时间
- 比较12小时格式时间
- 使用计时比较C++的持续时间/秒数并不像预期的那样工作
- C++:将当前时间与下一个工作时间进行比较
- 通用恒定时间比较函数C
- 将当前时间 simTime.dbl() 与以前存储的双精度值进行比较并不总是给出正确的答案......为什么?
- 比较向量元件的运行时间复杂性的提高
- 可靠地比较不同机器的时间戳C++
- 运行时间比较 - 类似的代码运行速度慢 4 倍
- 如何测量C代码的运行时间比较
- 在c++11中进行相对时间比较的最干净的方法
- 这是c++和Python之间CPU时间比较的有效方法吗?
- 模板参数的编译时间比较