1

I really dont know why this is happening, but here is the abstract of it. The code belows shows my attempt to find the time difference (to check if the given time is in the past or present) using mktime and finally difftime, but what I am finding is that the "hour" part of the output is modified - reduced by one hour Can anyone tell me if I am doing something wrong here?

void find_difference(absolute_time *t)
{
ThreadTime.tm_year  = t->yy - 1900; // thread time
ThreadTime.tm_mon   = t->mm - 1;
ThreadTime.tm_mday  = t->dd;
ThreadTime.tm_hour  = t->hr;
ThreadTime.tm_min   = t->min;
ThreadTime.tm_sec   = t->sec;`

printf("\n thread  : date:%d/%d/%d Time: %d:%d: %d\n", ThreadTime.tm_year,ThreadTime.tm_mon,ThreadTime.tm_mday,ThreadTime.tm_hour,ThreadTime.tm_min,ThreadTime.tm_sec);

ms_t = difftime(mktime(TimeInfo),mktime(&ThreadTime)); // current - thread

printf(" thread  : date:%d/%d/%d Time: %d:%d:%d\n", ThreadTime.tm_year,ThreadTime.tm_mon,ThreadTime.tm_mday,ThreadTime.tm_hour,ThreadTime.tm_min,ThreadTime.tm_sec);
}

The output is as follows:

thread  : date:114/11/11 Time: 16:25:0
thread  : date:114/11/11 Time: 15:25:0
time delay: 2832`

Is there anything that I am doing wrong?

Yashodhar.Rao
  • 107
  • 3
  • 11
  • 2
    Have you correctly set the `tm_isdst` field? Are `TimeInfo->tm_isdst` and `ThreadTime.tm_isdst` equal? – fuz Dec 11 '14 at 15:33

2 Answers2

1

You should read carefully the man page of mktime (if no Unix shell is handy, for example here).

Note that mktime modifies the passed structure (note that it is not const which is important here). It will normalize it correcting some anomalies, in particular here correcting for daylight saving time if needed. The time you pass to it is in the range where daylight saving is in effect, while you seem to be omitting setting it appropriately. The ThreadTime variable seems to be a global, if so, by the C standard it is initialized zero, so the daylight saving field (tm_isdst) indicates daylight saving not being in effect. This is likely corrected by mktime in the example, setting it, and adjusting the hour accordingly.

You might not be using the appropriate time interface for your problem. Maybe look in this question for more appropriate second to nanosecond precision timers independent of calendar time.

Community
  • 1
  • 1
Jubatian
  • 2,171
  • 16
  • 22
0

Check tm_isdst. @FUZxxl

mktime() does not restrict the fields like tm_year, tm_isdst, tm_hour to their primary range. Not only does it return time_t, it updates the fields to their primary range.

In setting up ThreadTime, code did not set the tm_isdst field. The call to mktime() adjusted it and tm_hour.


struct tm could have many fields, best to initialize all of them.

tm_isdt has 3 settings:
-int standard/daylight savings time setting unknown, let system decide
zero standard time
+int daylight savings time

void find_difference(absolute_time *t) {
  memset(&ThreadTime, 0, sizeof ThreadTime);
  ThreadTime.tm_isdst = -1;
  ThreadTime.tm_year  = t->yy - 1900; // thread time
  ThreadTime.tm_mon   = t->mm - 1;
  ...

The unposted code that printed ms_t as "time delay: 2832`" has questionable output. Expected something like:

double diff = difftime(mktime(TimeInfo),mktime(&ThreadTime)); 
printf(" time delay: %0.3f\n", diff);
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256