mktime() — Convert Local Time

Format

#include <time.h>
time_t mktime(struct tm *time);

Language Level: ANSI

Threadsafe: Yes.

Locale Sensitive: The behavior of this function might be affected by the LC_TOD category of the current locale.

Description

The mktime() function converts a stored tm structure (assume to be in job local time) pointed to by time, into a time_t structure suitable for use with other time functions. After the conversion, the time_t structure will be considered Universal Coordinate Time (UTC). For this conversion, mktime() checks the current locale setting for local time zone and daylight saving time (DST). If these values are not set in the current locale, mktime() gets the local time zone and daylight saving time settings from the current job. If the DST is set in the locale but the time zone information is not, the DST information in the locale is ignored. mktime() then uses the current time zone information to determine UTC.

The values of some structure elements pointed to by time are not restricted to the ranges shown for gmtime().

The values of tm_wday and tm_yday passed to mktime() are ignored and are assigned their correct values on return.

A positive or 0 value for tm_isdst causes mktime() to presume initially that DST, respectively, is or is not in effect for the specified time. A negative value for tm_isdst causes mktime() to attempt to determine whether DST is in effect for the specified time.

Return Value

The mktime() function returns Universal Coordinate Time (UTC) having type time_t. The value (time_t)(-1) is returned if the Universal Coordinate Time cannot be represented.

Example that uses mktime()

This example prints the day of the week that is 40 days and 16 hours from the current date.

#include <stdio.h>
#include <time.h>
 
char *wday[] = { "Sunday", "Monday", "Tuesday", "Wednesday",
                 "Thursday", "Friday", "Saturday" };
 
int main(void)
{
  time_t t1, t3;
  struct tm *t2;
 
  t1 = time(NULL);
  t2 = localtime(&t1);
  t2 -> tm_mday += 40;
  t2 -> tm_hour += 16;
  t3 = mktime(t2);
 
  printf("40 days and 16 hours from now, it will be a %s \n",
          wday[t2 -> tm_wday]);
}
 
/*******************  Output should be similar to:  ***************
 
40 days and 16 hours from now, it will be a Sunday
*/

Related Information



[ Top of Page | Previous Page | Next Page | Contents | Index ]