mktime(3C) (C Programming Language Utilities) mktime(3C)
NAME
mktime - converts a tm structure to a calendar time
SYNOPSIS
#include <time.h>
timet mktime (struct tm *timeptr);
DESCRIPTION
mktime converts the time represented by the tm structure pointed to
by timeptr into a calendar time (the number of seconds since 00:00:00
UTC, January 1, 1970).
The tm structure has the following format.
struct tm {
int tmsec; /* seconds after the minute - [0, 61] */
int tmmin; /* minutes after the hour - [0, 59] */
int tmhour; /* hour since midnight - [0, 23] */
int tmmday; /* day of the month - [1, 31] */
int tmmon; /* months since January - [0, 11] */
int tmyear; /* years since 1900 */
int tmwday; /* days since Sunday - [0, 6] */
int tmyday; /* days since January 1 - [0, 365] */
int tmisdst; /* flag for daylight savings time */
};
In addition to computing the calendar time, mktime normalizes the
supplied tm structure. The original values of the tmwday and
tmyday components of the structure are ignored, and the original
values of the other components are not restricted to the ranges
indicated in the definition of the structure. On successful
completion, the values of the tmwday and tmyday components are set
appropriately, and the other components are set to represent the
specified calendar time, but with their values forced to be within
the appropriate ranges. The final value of tmmday is not set until
tmmon and tmyear are determined.
The original values of the components may be either greater than or
less than the specified range. For example, a tmhour of -1 means 1
hour before midnight, tmmday of 0 means the day preceding the
current month, and tmmon of -2 means 2 months before January of
tmyear.
If tmisdst is positive, the original values are assumed to be in the
alternate timezone. If it turns out that the alternate timezone is
not valid for the computed calendar time, then the components are
adjusted to the main timezone. Likewise, if tmisdst is zero, the
original values are assumed to be in the main timezone and are
converted to the alternate timezone if the main timezone is not
valid. If tmisdst is negative, the correct timezone is determined
8/91 Page 1
mktime(3C) (C Programming Language Utilities) mktime(3C)
and the components are not adjusted.
Local timezone information is used as if mktime had called tzset.
mktime returns the specified calendar time. If the calendar time
cannot be represented, the function returns the value (timet) -1.
EXAMPLE
What day of the week is July 4, 2001?
#include <stdio.h>
#include <time.h>
static char *const wday[] = {
"Sunday", "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday", "-unknown-"
};
struct tm timestr;
/*...*/
timestr.tmyear = 2001 - 1900;
timestr.tmmon = 7 - 1;
timestr.tmmday = 4;
timestr.tmhour = 0;
timestr.tmmin = 0;
timestr.tmsec = 1;
timestr.tmisdst = -1;
if (mktime(×tr)== -1)
timestr.tmwday=7;
printf("%s\n", wday[timestr.tmwday]);
NOTES
tmyear of the tm structure must be for year 1970 or later. Calendar
times before 00:00:00 UTC, January 1, 1970 or after 03:14:07 UTC,
January 19, 2038 cannot be represented.
SEE ALSO
ctime(3C), getenv(3C), timezone(4).
Page 2 8/91