Home > Electronics Tutorials > C Language Tutorial > time.h - mktime

C Language Programming Library Reference Guide

time.h - mktime

Declaration: time_t mktime(struct tm *timeptr); Converts the structure pointed to by timeptr into a time_t value according to the local time zone. The values in the structure are not limited to their constraints. If they exceed their bounds, then they are adjusted accordingly so that they fit within their bounds. The original values of tm_wday (day of the week) and tm_yday (day of the year) are ignored, but are set correctly after the other values have been constrained. tm_mday (day of the month) is not corrected until after tm_mon and tm_year are corrected.

After adjustment the structure still represents the same time.

The encoded time_t value is returned. If the calendar time cannot be represented, then -1 is returned.

Example:

#include<time.h>
#include<stdio.h>

/* find out what day of the week is January 1, 2001 
  (first day of the 21st century) */

int main(void)
{
  struct tm time_struct;
  char days[7][4]={"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};

  time_struct.tm_year=2001-1900;
  time_struct.tm_mon=0;
  time_struct.tm_mday=1;
  time_struct.tm_sec=0;
  time_struct.tm_min=0;
  time_struct.tm_hour=0;
  time_struct.tm_isdst=-1;

  if(mktime(&time_struct)==-1)
   {
    printf("Error getting time.\n");
    exit(0);
   }

  printf("January 1, 2001 is a %s.\n",days[time_struct.tm_wday]);
  return 0;
}
Note: To report broken links or to submit your projects, tutorials please email to Webmaster

Discover

     more......