Previous Page
Next Page

mktime

Determines the time represented by a struct tm value

#include <time.h>
time_t mktime ( struct tm *timeptr  );

The mktime( ) function calculates the local calendar time represented by the member values in the object referenced by the pointer argument.

The type struct tm is defined in time.h as follows:

struct tm {
  int tm_sec;         /* Seconds (0-60; 1 leap second) */
  int tm_min;         /* Minutes (0-59) */
  int tm_hour;        /* Hours   (0-23) */
  int tm_mday;        /* Day     (1-31) */
  int tm_mon;         /* Month   (0-11) */
  int tm_year;        /* Year    (difference from 1900) */
  int tm_wday;        /* Day of week (0-6)   */
  int tm_yday;        /* Day of year (0-365) */
  int tm_isdst;       /* Daylight saving time (-1, 0, 1) */
};

The member tm_isdst is equal to 0 if daylight saving time is not in effect, or 1 if it is. A negative value indicates that the information is not available, in which case mktime( ) attempts to calculate whether daylight saving time is applicable at the time represented by the other members.

The mktime( ) function ignores the tm_wday and tm_yday members in determining the time, but does use tm_isdst. The other members may contain values outside their normal ranges. Once it has calculated the time represented, mktime( ) adjusts the struct tm members so that each one is within its normal range, and also sets tm_wday and tm_yday accordingly. The return value is the number of seconds from the epoch (usually midnight on January 1, 1970, UTC) to the time represented in the structure, or -1 to indicate an error.

Example

  time_t seconds;
  struct tm sometime;

  sometime.tm_sec   = 10;
  sometime.tm_min   = 80;
  sometime.tm_hour  = 40;
  sometime.tm_mday  = 23;
  sometime.tm_mon   = 1;
  sometime.tm_year  = 105;
  sometime.tm_wday  = 11;
  sometime.tm_yday  = 111;
  sometime.tm_isdst = -1;

  seconds = mktime( &sometime );

  if ( seconds == -1 )
  {
    printf( "mktime( ) couldn't make sense of its input.\n" );
    return -1;
  }

  printf( "The return value, %ld, represents %s",
          (long)seconds, ctime(&seconds) );

  printf( "The structure has been adjusted as follows:\n"
          "tm_sec   == %d\n"
          "tm_min   == %d\n"
          "tm_hour  == %d\n"
          "tm_mday  == %d\n"
          "tm_mon   == %d\n"
          "tm_year  == %d\n"
          "tm_wday  == %d\n"
          "tm_yday  == %d\n"
          "tm_isdst == %d\n",

          sometime.tm_sec,
          sometime.tm_min,
          sometime.tm_hour,
          sometime.tm_mday,
          sometime.tm_mon,
          sometime.tm_year,
          sometime.tm_wday,
          sometime.tm_yday,
          sometime.tm_isdst );

  printf( "The structure now represents %s", asctime( &sometime ));
}

This program produces the following output:

The return value, 1109262010, represents Thu Feb 24 17:20:10 2005
The structure has been adjusted as follows:
tm_sec   == 10
tm_min   == 20
tm_hour  == 17
tm_mday  == 24
tm_mon   == 1
tm_year  == 105
tm_wday  == 4
tm_yday  == 54
tm_isdst == 0
The structure now represents Thu Feb 24 17:20:10 2005

See Also

asctime( ), ctime( ), localtime( ), gmtime( ), strftime( )


Previous Page
Next Page