两个时间戳计算时间差

时间:2024-11-01 18:38:10
void GetTimeWithDis::GetTimeWithDisInterval() { for (int i = 1; i < m_timeWithDis.size(); i++) { std::string str1 = m_timeWithDis[i - 1]; std::string str2 = m_timeWithDis[i]; std::chrono::time_point<std::chrono::system_clock> tp1 = std::chrono::system_clock::from_time_t( timestamp2Time(std::stol(m_timeWithDis[i - 1]))); std::chrono::time_point<std::chrono::system_clock> tp2 = std::chrono::system_clock::from_time_t( timestamp2Time(std::stol(m_timeWithDis[i]))); // 计算两个时间点之间的差异 std::chrono::milliseconds diff = std::chrono::duration_cast<std::chrono::milliseconds>(tp1 - tp2); m_timeWithDisIntervalTime.push_back(diff.count() / 1000); // 输出差异毫秒数 std::cout << "The difference in milliseconds is: " << diff.count() << " ms" << std::endl; } } void GetTimeWithDis::ShowTimeWithDisInterval() { printf("m_timeWithDisIntervalTime\n"); for (time_t interval: m_timeWithDisIntervalTime) { std::cout << "The difference in milliseconds is: " << interval << " s" << std::endl; } } int main() { printf("GetTimeWithDis\n"); GetTimeWithDis getTimeWithDis; getTimeWithDis.m_timeWithDis.push_back("1725354889491"); getTimeWithDis.m_timeWithDis.push_back("1725354191449"); getTimeWithDis.m_timeWithDis.push_back("1724750800607"); getTimeWithDis.GetTimeWithDisInterval(); getTimeWithDis.ShowTimeWithDisInterval(); return 0; }