Export (0) Print
Expand All
Expand Minimize
1 out of 2 rated this helpful - Rate this topic

mktime, _mktime64

Convert the local time to a calendar value.

time_t mktime(
   struct tm *timeptr 
);
__time64_t _mktime64(
   struct tm *timeptr 
);

Parameter

timeptr
Pointer to time structure; see asctime.

Return Value

mktime returns the specified calendar time encoded as a value of type time_t. If timeptr references a date before midnight, January 1, 1970, or if the calendar time cannot be represented, mktime returns –1 cast to type time_t. When using mktime and if timeptr references a date after 3:14:07 January 19, 2038, UTC, it will return –1 cast to type time_t.

_mktime64 will return –1 cast to type __time64_t if timeptr references a date after 23:59:59, December 31, 3000, UTC.

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 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.

After an adjustment to Greenwich Mean Time (GMT), mktime handles dates from midnight, January 1, 1970, to January 19, 3:14:07, 2038. This adjustment may cause mktime to return -1 (cast to time_t) even though the date you specify is within range. For example, if you are in Cairo, Egypt, which is two hours ahead of GMT, two hours will first be subtracted from the date you specify in timeptr; this may now put your date out of range.

If successful, mktime sets the values of tm_wday and tm_yday as appropriate 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. When specifying a tm structure time, set the tm_isdst field to:

  • Zero (0) to indicate that standard time is in effect.
  • A value greater than 0 to indicate that daylight savings time is in effect.
  • A value less than zero to have the C run-time library code compute whether standard time or daylight savings time is in effect.

(The C run-time library assumes the United States' rules for implementing the calculation of Daylight Saving Time.) tm_isdst is a required field. If not set, its value is undefined and the return value from mktime is unpredictable. If timeptr points to a tm structure returned by a previous call to asctime, gmtime, or localtime, the tm_isdst field contains the correct value.

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

Requirements

Routine Required header Compatibility
mktime <time.h> ANSI, Win 98, Win Me, Win NT, Win 2000, Win XP
_mktime64 <time.h> Win 98, Win Me, Win NT, Win 2000, Win XP

For additional compatibility information, see Compatibility in the Introduction.

Libraries

All versions of the C run-time libraries.

Example

// crt_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>

int main( void )
{
   struct tm when;
   __time64_t now, result;
   int    days;

   _time64( &now );
   when = *_localtime64( &now );
   printf( "Current time is %s\n", asctime( &when ) );
   days = 20;
   when.tm_mday = when.tm_mday + days;
   if( (result = _mktime64( &when )) != (time_t)-1 )
      printf( "In %d days the time will be %s\n", 
              days, asctime( &when ) );
   else
      perror( "_mktime64 failed" );
}

Sample Output

Current time is Tue Feb 12 09:57:44 2002

In 20 days the time will be Mon Mar 04 09:57:44 2002

See Also

Time Management Routines | asctime | gmtime | localtime | time | Run-Time Routines and .NET Framework Equivalents

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.