mktime -- converts local time to calendar time


cc . . . -lc

#include  <time.h>

time_t mktime (timeptr) struct tm *timeptr; /* Local time structure */


The mktime function converts the local time into a calendar value. The timeptr argument points to a structure that contains the local time. The structure is described in the reference page for asctime. The converted time has the same encoding as the values returned by the time function. The original values of the tm_wday and tm_yday components of the timeptr structure are ignored, and the original values of the other components are not restricted to their normal ranges.

If successful, mktime sets the values of tm_wday and tm_yday appropriately, and sets the other components to represent the specified calendar time, but with their values forced to the normal ranges; the final value of tm_mday is not set until tm_mon and tm_year are determined.


Note that the gmtime, mktime, and localtime functions use a single statically allocated buffer for the conversion. Each call to one of these routines destroys the result of the previous call.

Return value

The mktime function returns the specified calendar time encoded as a value of type time_t. If the calendar time cannot be represented, the function returns the value -1 (time_t).

See also

ctime(S), difftime(S), nl_cxtime(S), stime(S), time(S)

Standards conformance

mktime is conformant with:

ANSI X3.159-1989 Programming Language -- C .


   #include <time.h>
   #include <stdio.h>

struct tm when; time_t now; time_t result; int days;
main() { printf("How many days to look ahead? "); scanf("%d", &days);

time(&now); when = *localtime(&now); when.tm_mday = when.tm_mday + days; if ((result = mktime(&when)) != (time_t)-1) printf("\n%d days from now the time will be %s", days, asctime(&when)); else perror("mktime failed"); }

The example above takes a number of days as input and returns the future time and date on the specified number of days ahead.
© 2003 Caldera International, Inc. All rights reserved.
SCO OpenServer Release 5.0.7 -- 11 February 2003