Latest web development tutorials

C library functions - mktime ()

C standard library - <time.h> C standard library - <time.h>

description

C library functionstime_t mktime (struct tm * timeptr) to timeptrpointed structure into a time_t value based on the local time zone.

statement

Here is the mktime () function's declaration.

time_t mktime(struct tm *timeptr)

parameter

  • timeptr - This is a pointer value pointing time_t calendar time representation of the calendar time is broken down into the following sections.Here are the details timeptr structure:
struct tm {
   int tm_sec;         /* 秒,范围从 0 到 59				*/
   int tm_min;         /* 分,范围从 0 到 59				*/
   int tm_hour;        /* 小时,范围从 0 到 23				*/
   int tm_mday;        /* 一月中的第几天,范围从 1 到 31	                */
   int tm_mon;         /* 月份,范围从 0 到 11				*/
   int tm_year;        /* 自 1900 起的年数				*/
   int tm_wday;        /* 一周中的第几天,范围从 0 到 6		        */
   int tm_yday;        /* 一年中的第几天,范围从 0 到 365	                */
   int tm_isdst;       /* 夏令时						*/	
};

return value

This function returns a time_t value that corresponds to the calendar time parameter passing. If an error occurs, it returns a value of -1.

Examples

The following example demonstrates the mktime () function is used.

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

int main ()
{
   int ret;
   struct tm info;
   char buffer[80];

   info.tm_year = 2001 - 1900;
   info.tm_mon = 7 - 1;
   info.tm_mday = 4;
   info.tm_hour = 0;
   info.tm_min = 0;
   info.tm_sec = 1;
   info.tm_isdst = -1;

   ret = mktime(&info);
   if( ret == -1 )
   {
       printf("错误:不能使用 mktime 转换时间。\n");
   }
   else
   {
      strftime(buffer, sizeof(buffer), "%c", &info );
      print(buffer);
   }

   return(0);
}

Let's compile and run the above program, which will result in the following:

Wed Jul 4 00:00:01 2001

C standard library - <time.h> C standard library - <time.h>