C Time Functions - C mktime






Converts time to a time value.

Prototype

time_t mktime (struct tm * timeptr);

Parameter

This function has the following parameter.

timeptr
Pointer to a tm structure for a calendar time broken down into its components.

struct tm is a structure used to hold the time and date.

Its members are as follows:

int tm_sec;    /* seconds after the minute (0 to 61) */ 
int tm_min;    /* minutes after the hour (0 to 59) */ 
int tm_hour;   /* hours since midnight (0 to 23) */ 
int tm_mday;   /* day of the month (1 to 31) */ 
int tm_mon;    /* months since January (0 to 11) */ 
int tm_year;   /* years since 1900 */ 
int tm_wday;   /* days since Sunday (0 to 6 Sunday=0)  */ 
int tm_yday;   /* days since January 1 (0 to 365) */ 
int tm_isdst;  /* Daylight Savings Time */ 




Return

A time_t value containing calendar time passed as argument.

Example


#include <stdio.h>
#include <time.h>
 /*from w  ww .  ja  va2s  .  c  o m*/
int main(void)
{
    struct tm tm = *localtime(&(time_t){time(NULL)});
    printf("Today is           %s", asctime(&tm));
    tm.tm_mon -= 100;  // tm_mon is now outside its normal range
    mktime(&tm); // recalculate tm
    printf("100 months ago was %s", asctime(&tm));
}

The code above generates the following result.





Example 2


#include <stdio.h> /* printf, scanf */
#include <time.h>  /* time_t, struct tm, time, mktime */
/*from   w  w  w .  j  av  a  2s. c o  m*/
int main (){
  time_t rawtime;
  struct tm * timeinfo;
  int year, month ,day;
  const char * weekday[] = { "Sunday", "Monday",
                             "Tuesday", "Wednesday",
                             "Thursday", "Friday", "Saturday"};

  /* prompt user for date */
  printf ("Enter year: "); fflush(stdout); scanf ("%d",&year);
  printf ("Enter month: "); fflush(stdout); scanf ("%d",&month);
  printf ("Enter day: "); fflush(stdout); scanf ("%d",&day);

  /* get current timeinfo and modify it to the user's choice */
  time ( &rawtime );
  timeinfo = localtime ( &rawtime );
  timeinfo->tm_year = year - 1900;
  timeinfo->tm_mon = month - 1;
  timeinfo->tm_mday = day;

  /* call mktime: timeinfo->tm_wday will be set */
  mktime ( timeinfo );

  printf ("That day is a %s.\n", weekday[timeinfo->tm_wday]);

  return 0;
} 

       

The code above generates the following result.

Example 3


#include<time.h> 
#include<stdio.h> 
//from  w  w w . ja  v a  2 s . co m
/* 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; 
 } 

The code above generates the following result.