This blog is under construction

Saturday, 5 May 2012

mktime example in C

Header file:
    time.h

Synopsis:
     time_t mktime(struct tm *tp);

Description:
     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:


  #include<stdio.h>
  #include<time.h>
  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;
  }



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

1 comment:

  1. This explanation of mktime is really helpful, especially for understanding how PHP handles timestamps and date calculations. It makes it clearer how useful it is when working with dynamic dates in real projects. I can see how this would be important in programming practice and even in building simple tools or experiments like a big daddy game win project. Thanks for sharing this useful guide.

    ReplyDelete