6.22.11 mktime Function

Converts local time to a calendar value.

Include

<time.h>

Prototype

time_t mktime(struct tm *tptr);

Argument

tptr
a pointer to the time structure

Return Value

Returns the calendar time encoded as a value of time_t.

Remarks

If the calendar time cannot be represented, the function returns -1 cast as a time_t (i.e. (time_t) -1).

Example

See the notes at the beginning of this chapter or section for information on using printf() or scanf() (and other functions reading and writing the stdin or stdout streams) in the example code.

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

int main(void)
{
  time_t timer, whattime;
  struct tm *newtime;

  timer = 1066668182; /* Mon Oct 20 16:43:02 2003 */
  /* localtime allocates space for struct tm */
  newtime = localtime(&timer);
  printf("Local time = %s", asctime(newtime));

  whattime = mktime(newtime);
  printf("Calendar time as time_t = %ld\n", 
          whattime);
}

Example Output

Local time = Mon Oct 20 16:43:02 2003
Calendar time as time_t = 1066668182