Compare tm’s
struct tm Tm1 = {0};
struct tm Tm2 = {0};
strptime("2020-08-29 11:05:13", "%Y-%m-%d %H:%M:%S", &Tm1);
strptime("2020-08-29 11:05:16", "%Y-%m-%d %H:%M:%S", &Tm2);
if (mktime(&Tm1) >= mktime(&Tm2)) //Larger values of mktime() are more recent in time
{
printf("Tm1 is more recent than Tm2\n");
}
else
{
printf("Tm1 is older than Tm2\n");
}
or with strptime() check
struct tm Tm1 = {0};
struct tm Tm2 = {0};
if (strptime("2020-08-29 11:05:13", "%Y-%m-%d %H:%M:%S", &Tm1) == NULL)
printf("strptime Failed\n");
if (strptime("2020-08-29 11:05:16", "%Y-%m-%d %H:%M:%S", &Tm2) == NULL)
printf("strptime Failed\n");
if (mktime(&Tm1) >= mktime(&Tm2)) //Larger values of mktime() are more recent in time
{
printf("Tm1 is more recent than Tm2\n");
}
else
{
printf("Tm1 is older than Tm2\n");
}
Get time difference in seconds
double TimeDifferenceSeconds;
TimeDifferenceSeconds = difftime(mktime(&Tm1), mktime(&Tm2)); //Value1 - Value2
USEFUL?
We benefit hugely from resources on the web so we decided we should try and give back some of our knowledge and resources to the community by opening up many of our company’s internal notes and libraries through resources like this. We hope you find it helpful.
Please feel free to comment if you can add help to this page or point out issues and solutions you have found, but please note that we do not provide support here. If you need help with a problem please use one of the many online forums.