This blog is under construction

Saturday, 5 May 2012

mktime example in C

Header file:

     time_t mktime(struct tm *tp);

     It takes the broken time (local time in the structure *tp) as parameter and returns us the calendar time in the same representation of time().

           struct tm {
               int tm_sec;         /* seconds */
               int tm_min;         /* minutes */
               int tm_hour;        /* hours */
               int tm_mday;        /* day of the month */
               int tm_mon;         /* month */
               int tm_year;        /* year */
               int tm_wday;        /* day of the week */
               int tm_yday;        /* day in the year */
               int tm_isdst;       /* daylight saving time */

mktime function C example:

  int main() {
        struct tm *tp;
        time_t t;
        char str[100];
        t = time(NULL);
        tp = localtime(&t);
        t = mktime(tp);
        printf("Return value of mktime:%ld\n", t);
        sprintf(str, "%d/%d/%d   %d:%d:%d\n", tp->tm_mday, tp->tm_mon,
                1900+tp->tm_year, tp->tm_hour, tp->tm_min, tp->tm_sec);
        printf("Output:%s\n", str);
        return 0;

  jp@jp-VirtualBox:~/cpgms/time$ ./a.out
  Return value of mktime:1336237515
  Output:5/4/2012   22:35:15

No comments:

Post a Comment