mktime

Description

Converts the local time to a calendar value.

#include <time.h>

time_t mktime( struct tm *timeptr );

timeptr Pointer to time structure  

Remarks

The mktime function converts the supplied time structure (possibly incomplete) pointed to by timeptr into a fully defined structure with “normalized” values and then converts it to a time_t calendar time value. The structure for the tm 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.

If timeptr references a date before midnight, December 31, 1899, mktime returns –1.

Note that the gmtime and localtime functions use a single statically allocated buffer for the conversion. If you supply this buffer to mktime, the previous contents will be destroyed.

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 cast to type time_t.

Compatibility

Standards:ANSI

16-Bit:DOS, QWIN, WIN, WIN DLL

32-Bit:DOS32X

See Also

asctime, gmtime, localtime, time

Example

/* MKTIME.C: The example takes a number of days as input and returns

* the time, the current date, and the specified number of days.

*/

#include <time.h>

#include <stdio.h>

void main( void )

{

struct tm when;

time_t now, result;

int days;

time( &now );

when = *localtime( &now );

printf( "Current time is %s\n", asctime( &when ) );

printf( "How many days to look ahead: " );

scanf( "%d", &days );

when.tm_mday = when.tm_mday + days;

if( (result = mktime( &when )) != (time_t)-1 )

printf( "In %d days the time will be %s\n",

days, asctime( &when ) );

else

perror( "mktime failed" );

}

Output

Current time is Sat Jun 19 11:45:20 1999

How many days to look ahead: 23

In 23 days the time will be Mon Jul 12 11:45:20 1999