Your Browser is not longer supported

Please use Google Chrome, Mozilla Firefox or Microsoft Edge to view the page correctly
Loading...

{{viewport.spaceProperty.prod}}

mktime, mktime64 - convert local time into time since the Epoch

&pagelevel(4)&pagelevel

Syntax

#include <time.h>

time_t mktime(struct tm *timeptr);
time64_t mktime64(struct tm *timeptr); 

Description

The mktime() and mktime64() functions convert the date and time of the local time which are specified in a structure of the type tm into the number of seconds which have passed since 1.1.1970 00:00:00 hrs UTC (Universal Time Coordinated).

The two functions differ merely in the range of dates which can be displayed:

  • mktime(): 13.12.1901 20:45:52 hrs UTC through 19.1.2038 03:14:07 hrs

  • mktime64(): 1.1.1900 20:45:52 hrs UTC through 31.12.9999 23:59:59 hrs

The tm structure has the following format:

struct tm {
      int     tm_sec;         /* Seconds [0, 61]  */
      int     tm_min;         /* Minutes [0, 59] */
      int     tm_hour;        /* Hours [0, 23] */
      int     tm_mday;        /* Day of the month [1, 31] */
      int     tm_mon;         /* Month [0, 11] */
      int     tm_year;        /* Years since 1900 */
      int     tm_wday;        /* Days since Sunday [0, 6] */
      int     tm_yday;        /* Days since January 1 [0, 365] */
      int     tm_isdst;       /* Flag for daylight saving time */
}; 

Besides computing the calendar time, mktime() normalizes the supplied tm structure. The original values of the tm_wday and tm_yday 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. Upon successful completion, the values of the tm_wday and tm_yday 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 tm_mday is not set until tm_mon and tm_year are determined.

The original values of the components may be either greater than or less than the specified range. For example, a tm_hour of -1 means 1 hour before midnight; a tm_mday of 0 means the day preceding the current month, and a tm_mon of -2 means 2 months before January of the tm_year.

If tm_isdst is > 0, the original values are assumed to be in the alternate timezone, i.e. summer time applies. 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. Conversely, if tm_isdst is zero, the original values are assumed to be in the main timezone, i.e. normal time applies, and are converted to the alternate timezone if the main timezone is invalid. If tm_isdst is negative, mktime() determines the correct timezone.
Local timezone information is set as if mktime() had called the tzset() function.

BS2000
mktime() converts the date and time, which are specified by the user in a structure of type tm, into a time specification of type time_t. This is the number of seconds that have elapsed since 00:00:00, January 1, 1970. (End)

Return val.

Number of seconds 



if successful.


(time_t) - 1 or (time64_t) - 1



if the calendar time cannot be represented. Furthermore, errno is set to  EOVERFLOW.

 

BS2000


For local times as of January 1, 1970, 00.00.00, the number of seconds that have elapsed since then (positive value).

For local times prior to January 1, 1970, 00.00.00, the number of elapsed seconds up to that point (negative value). (End)

Example

Which day of the week was July 4, 2001?

#include <stdio.h>
#include <time.h>
struct tm time_str
char daybuf[20]
int main (void)
{
      time_str.tm_year = 2001 - 1900;
      time_str.tm_mon = 7 - 1;
      time_str.tm_mday = 4;
      time_str.tm_hour = 0;
      time_str.tm_min = 0;
      time_str.tm_sec = 1;
      time_str.tm_isdst = -1;
      if (mktime ( &time_str) == -1)
              (void) puts (" -unknown-“);
      else { 
              (void) strftime (daybuf, sizeof (daybuf), "%A", &time_str);
       }
       return 0;
}

Note

As -1 is a permissible return value in a successful situation, an application wishing to check for error situations should set errno to 0 before calling the function.
If -1 is returned, the application should check if errno is non-zero.

See also

ctime(), getenv(), timezone, time.h.