C++11 C++;如何减去两个时间变量
我有一系列的时间变量,格式是“日-月-年H:M”,例如,14-03-15:25。如何正确测量两个时间变量之间的差异并输出持续时间类型C++11 C++;如何减去两个时间变量,c++11,chrono,C++11,Chrono,我有一系列的时间变量,格式是“日-月-年H:M”,例如,14-03-15:25。如何正确测量两个时间变量之间的差异并输出持续时间类型 std::string t1 = 14-03-15 15:25; std::string t2 = 19-05-15 7:32; template <typename Duration> auto diff(std::string& t1, std::string& t2) { // How to do from here? }
std::string t1 = 14-03-15 15:25;
std::string t2 = 19-05-15 7:32;
template <typename Duration>
auto diff(std::string& t1, std::string& t2) {
// How to do from here?
}
auto ds = diff<std::chrono::seconds>(t1, t2);
auto dm = diff<std::chrono::minutes>(t1, t2);
std::string t1=14-03-15:25;
标准:字符串t2=19-05-15 7:32;
模板
自动差异(标准::字符串和t1,标准::字符串和t2){
//从这里怎么办?
}
自动ds=差异(t1,t2);
自动dm=diff(t1,t2);
解决了将字符串转换为时间的问题后
std::tm tm = {};
std::stringstream ss(t1);
ss >> std::get_time(&tm, "%d-%m-%y %H:%M");
auto tp1 = std::chrono::system_clock::from_time_t(std::mktime(&tm));
或
你可以减去时钟
// floating-point duration: no duration_cast needed
std::chrono::duration<double, Duration> duration = tp2 - tp1;
//浮点持续时间:不需要持续时间\u强制转换
std::chrono::duration duration=tp2-tp1;
解决了将字符串转换为时间的问题后
std::tm tm = {};
std::stringstream ss(t1);
ss >> std::get_time(&tm, "%d-%m-%y %H:%M");
auto tp1 = std::chrono::system_clock::from_time_t(std::mktime(&tm));
或
你可以减去时钟
// floating-point duration: no duration_cast needed
std::chrono::duration<double, Duration> duration = tp2 - tp1;
//浮点持续时间:不需要持续时间\u强制转换
std::chrono::duration duration=tp2-tp1;
这个问题很有趣,因为它揭示了关于时间的问题是多么模棱两可
t1
和t2
是否表示UTC时间?或者时区的时间?如果是后者,计算机当前的本地时区,还是其他时区
虽然这些输入可能不重要,但对于秒精度计算,闰秒是否重要
每个问题的答案都会影响结果。当前C和C++ STD API不足以解决这些问题的整体性。
可以给您正确的结果,但可以根据上述问题对输入的期望解释给出不同的答案
最简单的解释是:这些时间代表UTC,闰秒无关紧要:
#include "date/date.h"
#include <chrono>
#include <iostream>
#include <sstream>
#include <stdexcept>
#include <string>
std::string t1 = "14-03-15 15:25";
std::string t2 = "19-05-15 7:32";
template <typename Duration>
auto
diff(const std::string& t1, const std::string& t2)
{
using namespace std;
using namespace date;
istringstream in{t1};
sys_time<Duration> d1;
in >> parse("%d-%m-%y %H:%M", d1);
if (in.fail())
throw runtime_error("didn't parse " + t1);
in.clear();
in.str(t2);
sys_time<Duration> d2;
in >> parse("%d-%m-%y %H:%M", d2);
if (in.fail())
throw runtime_error("didn't parse " + t2);
return d2 - d1;
}
int
main()
{
using date::operator<<;
auto ds = diff<std::chrono::seconds>(t1, t2);
std::cout << ds << '\n';
auto dm = diff<std::chrono::minutes>(t1, t2);
std::cout << dm << '\n';
}
对于稍有不同的输入,假设输入位于“美国/纽约”,则输出将不同。如果t1
在01-01-15之前漂移,则闰秒的问题可能会影响输出
如果输入解释的这些细节很重要,那么使用现有的C/C++API,实际上肯定会导致计算错误(但这种错误很少发生,而且是最危险的一种)。这个问题很有趣,因为它揭示了关于时间的问题是多么模糊
t1
和t2
是否表示UTC时间?或者时区的时间?如果是后者,计算机当前的本地时区,还是其他时区
虽然这些输入可能不重要,但对于秒精度计算,闰秒是否重要
每个问题的答案都会影响结果。当前C和C++ STD API不足以解决这些问题的整体性。
可以给您正确的结果,但可以根据上述问题对输入的期望解释给出不同的答案
最简单的解释是:这些时间代表UTC,闰秒无关紧要:
#include "date/date.h"
#include <chrono>
#include <iostream>
#include <sstream>
#include <stdexcept>
#include <string>
std::string t1 = "14-03-15 15:25";
std::string t2 = "19-05-15 7:32";
template <typename Duration>
auto
diff(const std::string& t1, const std::string& t2)
{
using namespace std;
using namespace date;
istringstream in{t1};
sys_time<Duration> d1;
in >> parse("%d-%m-%y %H:%M", d1);
if (in.fail())
throw runtime_error("didn't parse " + t1);
in.clear();
in.str(t2);
sys_time<Duration> d2;
in >> parse("%d-%m-%y %H:%M", d2);
if (in.fail())
throw runtime_error("didn't parse " + t2);
return d2 - d1;
}
int
main()
{
using date::operator<<;
auto ds = diff<std::chrono::seconds>(t1, t2);
std::cout << ds << '\n';
auto dm = diff<std::chrono::minutes>(t1, t2);
std::cout << dm << '\n';
}
对于稍有不同的输入,假设输入位于“美国/纽约”,则输出将不同。如果t1
在01-01-15之前漂移,则闰秒的问题可能会影响输出
如果输入解释的这些细节很重要,那么使用现有的C/C++API,实际上肯定会导致计算错误(但这种错误很小,而且很少——最危险的一种)