简体   繁体   English

使用C ++以秒为单位获取两个日期与时间字符串之间的差异

[英]Getting difference between two date to timestring in seconds using c++

I need to find the difference between two date to time periods in seconds. 我需要找到两个日期到时间段之间的时差(以秒为单位)。 Here is what I did: Assigned the date and time variables using sscanf. 这是我所做的:使用sscanf分配日期和时间变量。 Assigned the variables to struct tm object (timeInfo). 将变量分配给struct tm对象(timeInfo)。

Example of time string: Thu, 29 Mar 2018 11:45:00 时间字符串示例:2018年3月29日,星期四,11:45:00

Everything works fine as expected. 一切正常。 Now comes the problem. 现在出现了问题。 I have obtained the current time as seen in the program. 我已经获得了程序中显示的当前时间。 But when I get currentTime the members such as tm_wday,tm_yday and tm_isdst are assigned to it whereas the string I took doesn't have these details. 但是,当我获得currentTime时,会将诸如tm_wday,tm_yday和tm_isdst之类的成员分配给它,而我获取的字符串没有这些细节。 Should I assign them inorder to obtain the diff seconds right? 我应该分配它们以便获得比较秒吗?

Is there any workaround? 有什么解决方法吗?

int GetDiffTimeInMilliseconds(LPCSTR expireValue)
{
const char monthNames[] =  "JanFebMarAprMayJunJulAugSepOctNovDec";
int hh,mm,ss,day,year,monthNumber;
char weekday[10],month[5],timezone[5];
struct tm timeInfo,currentTime;
time_t now;

sscanf(expireValue,"%[^,], %d %s %d %d:%d:%d %s",weekday,&day,month,&year,&hh,&mm,&ss,timezone);

cout<<weekday<<" "<<day<<" "<<month<<" "<<year<<" "<<hh<<":"<<mm<<":"<<ss;

monthNumber = (strstr(monthNames,month)-monthNames)/3;

timeInfo.tm_mday = day;
timeInfo.tm_mon = monthNumber;
timeInfo.tm_year = year - 1900;
timeInfo.tm_hour = hh;
timeInfo.tm_min = mm;
timeInfo.tm_sec = ss;

time(&now);
currentTime = *localtime(&now);

INT64 seconds= difftime(mktime(&timeInfo),now);

cout<<"The seconds are: "<<seconds;

}

Try this: 尝试这个:

time_t now;

time(&now);
struct tm *currentTime = localtime(&now);

Solution using std::chrono 使用std :: chrono的解决方案

#include <chrono> //std::chrono
#include <iostream> //std::cout
#include <thread> //std::this_thread::sleep_for

auto get_dif_duration(const std::chrono::time_point<std::chrono::system_clock> & time_point)
{
    return std::chrono::system_clock::now() - time_point;
}

int main()
{

    using namespace std::chrono_literals;

    auto start = std::chrono::system_clock::now();

    std::this_thread::sleep_for(2s);

    auto seconds = std::chrono::duration_cast<std::chrono::seconds>(get_dif_duration(start));

    std::cout << "Waited: " << seconds.count() << " seconds\n";

}

I did it. 我做的。 This is the updated code of what I did to find the diff between given time and the present time. 这是我为查找给定时间和当前时间之间的差异所做的更新代码。

Example of the date to time string: Sat, 31 Mar 2018 09:41:28 GMT 日期至时间字符串的示例:2018年3月31日星期六,格林尼治标准时间

INT64 GetDiffTimeInMilliseconds(LPCSTR expireValue)
{
const char monthNames[] =  "JanFebMarAprMayJunJulAugSepOctNovDec";
int hh,mm,ss,day,year,monthNumber;
char weekday[10],month[5],timezone[5];
struct tm timeInfo,currentTime;
time_t now;

time(&now);
currentTime = *localtime(&now);

sscanf(expireValue,"%3[^,], %d %s %d %d:%d:%d %s",weekday,&day,month,&year,&hh,&mm,&ss,timezone);

cout<<weekday<<" "<<day<<" "<<month<<" "<<year<<" "<<hh<<":"<<mm<<":"<<ss;

monthNumber = (strstr(monthNames,month)-monthNames)/3;

timeInfo.tm_mday = day;
timeInfo.tm_mon = monthNumber;
timeInfo.tm_year = year - 1900;
timeInfo.tm_hour = hh;
timeInfo.tm_min = mm;
timeInfo.tm_sec = ss;

INT64 seconds= difftime(mktime(&timeInfo),mktime(&currentTime));

cout<<"The seconds are : "<<seconds;

return seconds;

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM