mktime

From RAD Studio
Jump to: navigation, search

Go Up to time.h Index


Header File

time.h

Category

Time and Date Routines

Prototype

time_t mktime(struct tm *t);

Description

Converts time to calendar format.

Converts the time in the structure pointed to by t into a calendar time with the same format used by the time function. The original values of the fields tm_sec, tm_min, tm_hour, tm_mday, and tm_mon are not restricted to the ranges described in the tm structure. If the fields are not in their proper ranges, they are adjusted. Values for fields tm_wday and tm_yday are computed after the other fields have been adjusted.

The tm_isdst (Daylight Savings Time) field is adjusted with the correct value after calling the function mktime. Also, tm_isdst is used for adusting the value of tm_hour. For example, if the value of tm_isdst is 1, but the current date is not DST, mktime updates tm_hour (tm_hour = tm_hour - 1) and sets tm_isdst to 0.

The allowable range of calendar times is Jan 1 1970 00:00:00 to Jan 19 2038 03:14:07.

Return Value

On success, mktime returns calendar time as described above.

On error (if the calendar time cannot be represented), mktime returns -1.

Example

#include <stdio.h> 
#include <time.h>
    char *wday[] =   
     {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday",
        "Saturday", "Unknown"}; 
int main(void) 
{ 
  struct tm time_check; 
  int year, month, day; 
/*  Input a year, month and day to find the weekday for */ 
  printf("Year: "); 
  scanf("%d", &year); 
  printf("Month: "); 
  scanf("%d", &month); 
  printf("Day: ");  
  scanf("%d", &day); 
/* load the time_check structure with the data */ 
  time_check.tm_year = year - 1900; 
  time_check.tm_mon = month - 1;  
  time_check.tm_mday = day; 
  time_check.tm_hour = 0;
  time_check.tm_min = 0; 
  time_check.tm_sec = 1; 
  time_check.tm_isdst = -1; 
/* call mktime to fill in the weekday field of the structure */ 
  if (mktime(&time_check) == -1) time_check.tm_wday = 7; 
/* print out the day of the week */ 
  printf("That day is a %s\n", wday[time_check.tm_wday]); 
  return 0; 
} 

Portability

POSIX Win32 ANSI C ANSI C++

+

+

+

+