Вопрос

I wanted to calculate the time difference for a GMT time and current time. For this I am converting tm time (which is in GMT) to time_t format using mktime. and current time using time() api.

struct tm = x;  time_t t1, t2; 
time(&t1);
/* here x  will get in GMT format */
t2 = mktime(&x);
sec = difftime(t2 , t1);

In this for making the same time zone, is mktime() will take care of converting to local time ? or shall I need to explicitly add sec = difftime(t2 , gmtime(&t1); Thanks

Это было полезно?

Решение

Yes mktime convert to local time, please read the man:

http://www.mkssoftware.com/docs/man3/mktime.3.asp

mktime() : convert local time to seconds since the Epoch

EDIT : To calculate the difference time between two dates, you can use this

    time_t t1, t2;
    struct tm my_target_date;

    /* Construct your date */
    my_target_date.tm_sec = 0;
    my_target_date.tm_min = 0;
    my_target_date.tm_hour = 0;
    my_target_date.tm_mday = 20;
    my_target_date.tm_mon = 7;
    my_target_date.tm_year = 112; /* Date today */
    t1 = mktime (&my_target_date);
    t2 = time (NULL);
    printf ("Number of days since target date : %ld\n", (t2 - t1) / 86400); /* 1 day = 86400 sec, use 3600 if you want hours */
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top