Reference
C Library
IOstream Library
Strings library
STL Containers
STL Algorithms
Miscellaneous
C Library
cassert (assert.h)
cctype (ctype.h)
cerrno (errno.h)
cfloat (float.h)
ciso646 (iso646.h)
climits (limits.h)
clocale (locale.h)
cmath (math.h)
csetjmp (setjmp.h)
csignal (signal.h)
cstdarg (stdarg.h)
cstddef (stddef.h)
cstdio (stdio.h)
cstdlib (stdlib.h)
cstring (string.h)
ctime (time.h)
ctime (time.h)
functions:
asctime
clock
ctime
difftime
gmtime
localtime
mktime
strftime
time
macros:
CLOCKS_PER_SEC
NULL
types:
clock_t
size_t
time_t
struct tm


mktime

function
<ctime>
time_t mktime ( struct tm * timeptr );

Convert tm structure to time_t

Interprets the contents of the tm structure pointed by timeptr as a calendar time expressed in local time. This calendar time is used to adjust the values of the members of timeptr accordingly and returned as an object of type time_t.

The original values of the members tm_wday and tm_yday of timeptr are ignored, and the ranges of values for the rest of its members are not restricted to their normal values (like tm_mday being between 1 and 31).

The object pointed by timeptr is modified, setting the tm_wday and tm_yday to their appropiate values, and modifying the other members as necessary to values within the normal range representing the specified time.

Parameters

timeptr
Pointer to a tm structure that contains a calendar time broken down into its components (see tm).

Return Value

A time_t value corresponding to the calendar time passed as argument.
On error, a -1 value is returned.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/* mktime example: weekday calculator */
#include <stdio.h>
#include <time.h>
int main ()
{
  time_t rawtime;
  struct tm * timeinfo;
  int year, month ,day;
  char * weekday[] = { "Sunday", "Monday",
                       "Tuesday", "Wednesday",
                       "Thursday", "Friday", "Saturday"};
  /* prompt user for date */
  printf ("Enter year: "); scanf ("%d",&year);
  printf ("Enter month: "); scanf ("%d",&month);
  printf ("Enter day: "); scanf ("%d",&day);
  /* get current timeinfo and modify it to the user's choice */
  time ( &rawtime );
  timeinfo = localtime ( &rawtime );
  timeinfo->tm_year = year - 1900;
  timeinfo->tm_mon = month - 1;
  timeinfo->tm_mday = day;
  /* call mktime: timeinfo->tm_wday will be set */
  mktime ( timeinfo );
  printf ("That day is a %s.\n", weekday[timeinfo->tm_wday]);
  
  return 0;
}


Output:

Enter year: 2000
Enter month: 5
Enter day: 20
That day is a Saturday.

See also.