1mktime(3C)               Standard C Library Functions               mktime(3C)
2
3
4

NAME

6       mktime - converts a tm structure to a calendar time
7

SYNOPSIS

9       #include <time.h>
10
11       time_t mktime(struct tm *timeptr);
12
13

DESCRIPTION

15       The mktime() function converts the time represented by the tm structure
16       pointed to by timeptr into a calendar  time   (the  number  of  seconds
17       since 00:00:00 UTC, January 1, 1970).
18
19
20       The tm structure contains the following members:
21
22         int  tm_sec;     /* seconds after the minute [0, 60]  */
23         int  tm_min;     /* minutes after the hour [0, 59] */
24         int  tm_hour;    /* hour since midnight [0, 23] */
25         int  tm_mday;    /* day of the month [1, 31] */
26         int  tm_mon;     /* months since January [0, 11] */
27         int  tm_year;    /* years since 1900 */
28         int  tm_wday;    /* days since Sunday [0, 6] */
29         int  tm_yday;    /* days since January 1 [0, 365] */
30         int  tm_isdst;   /* flag for daylight savings time */
31
32
33
34       In  addition  to  computing  the calendar time, mktime() normalizes the
35       supplied tm structure. The original values of the tm_wday  and  tm_yday
36       components of the structure are ignored, and the original values of the
37       other components are not restricted to the ranges indicated in the def‐
38       inition  of  the structure. On successful completion, the values of the
39       tm_wday and tm_yday components are set  appropriately,  and  the  other
40       components  are  set to represent the specified calendar time, but with
41       their values forced to be within  the  appropriate  ranges.  The  final
42       value of tm_mday is not set until tm_mon and tm_year are determined.
43
44
45       The  tm_year  member  must  be  for  year 1901 or later. Calendar times
46       before 20:45:52 UTC, December 13, 1901 or after 03:14:07 UTC,   January
47       19, 2038 cannot be represented. Portable applications should not try to
48       create dates before 00:00:00 UTC, January 1,  1970  or  after  00:00:00
49       UTC, January 1, 2038.
50
51
52       The  original  values  of  the components may be either greater than or
53       less than the specified range. For example, a tm_hour  of  −1  means  1
54       hour  before midnight, tm_mday of 0 means the day preceding the current
55       month, and tm_mon of −2 means 2 months before January of tm_year.
56
57
58       If tm_isdst is positive, the original values are assumed to be  in  the
59       alternate  timezone. If it turns out that the alternate timezone is not
60       valid for the computed calendar time, then the components are  adjusted
61       to  the main timezone. Likewise, if tm_isdst is zero, the original val‐
62       ues are assumed to be in the main timezone and  are  converted  to  the
63       alternate  timezone  if the main timezone is not valid. If  tm_isdst is
64       negative, mktime() attempts to determine whether the alternate timezone
65       is in effect for the specified time.
66
67
68       Local  timezone  information is used as if mktime() had called tzset().
69       See ctime(3C).
70

RETURN VALUES

72       If the calendar time can be represented in an object  of  type  time_t,
73       mktime() returns the specified calendar time without changing errno. If
74       the calendar time cannot be represented, the function returns the value
75       (time_t)−1 and sets errno to indicate the error.
76

ERRORS

78       The mktime() function will fail if:
79
80       EOVERFLOW    The date represented by the input tm struct cannot be rep‐
81                    resented in a time_t.  Note that  the  errno  setting  may
82                    change if future revisions to the standards specify a dif‐
83                    ferent value.
84
85

USAGE

87       The  mktime() function is MT-Safe  in  multithreaded  applications,  as
88       long as no user-defined function directly modifies one of the following
89       variables: timezone, altzone, daylight, and tzname. See ctime(3C).
90
91
92       Note that −1 can be a valid return value for the time that is one  sec‐
93       ond  before  the  Epoch.   The  user  should clear errno before calling
94       mktime(). If mktime() then returns −1, the user should check  errno  to
95       determine whether or not an error actually occurred.
96
97
98       The   mktime() function assumes Gregorian dates. Times before the adop‐
99       tion of the Gregorian calendar will not match historial records.
100

EXAMPLES

102       Example 1 Sample code using mktime().
103
104
105       What day of the week is July 4, 2001?
106
107
108         #include <stdio.h>
109         #include <time.h>
110         static char *const wday[] = {
111                 "Sunday", "Monday", "Tuesday", "Wednesday",
112                 "Thursday", "Friday", "Saturday", "-unknown-"
113         };
114         struct tm time_str;
115         /*...*/
116         time_str.tm_year    = 2001 - 1900;
117         time_str.tm_mon = 7 - 1;
118         time_str.tm_mday = 4;
119         time_str.tm_hour = 0;
120         time_str.tm_min = 0;
121         time_str.tm_sec = 1;
122         time_str.tm_isdst = −1;
123         if (mktime(&time_str)== −1)
124                 time_str.tm_wday=7;
125         printf("%s\n", wday[time_str.tm_wday]);
126
127

BUGS

129       The zoneinfo timezone data files do not  transition  past  Tue  Jan  19
130       03:14:07  2038  UTC.   Therefore for 64-bit applications using zoneinfo
131       timezones, calculations beyond this date may not use the correct offset
132       from standard time, and could return incorrect values. This affects the
133       64-bit version of mktime().
134

ATTRIBUTES

136       See attributes(5) for descriptions of the following attributes:
137
138
139
140
141       ┌─────────────────────────────┬─────────────────────────────┐
142       │      ATTRIBUTE TYPE         │      ATTRIBUTE VALUE        │
143       ├─────────────────────────────┼─────────────────────────────┤
144       │Interface Stability          │Standard                     │
145       ├─────────────────────────────┼─────────────────────────────┤
146       │MT-Level                     │MT-Safe with exceptions      │
147       └─────────────────────────────┴─────────────────────────────┘
148

SEE ALSO

150       ctime(3C), getenv(3C), TIMEZONE(4), attributes(5), standards(5)
151
152
153
154SunOS 5.11                        1 Nov 2003                        mktime(3C)
Impressum