目录

time_t mktime(struct tm *timeptr)

描述 (Description)

C库函数time_t mktime(struct tm *timeptr)根据本地时区将time_t mktime(struct tm *timeptr)指向的结构转换为time_t值。

声明 (Declaration)

以下是mktime()函数的声明。

time_t mktime(struct tm *timeptr)

参数 (Parameters)

  • timeptr - 这是指向表示日历时间的time_t值的指针,分解为其组件。 下面是timeptr结构的细节

struct tm {
   int tm_sec;         /* seconds,  range 0 to 59          */
   int tm_min;         /* minutes, range 0 to 59           */
   int tm_hour;        /* hours, range 0 to 23             */
   int tm_mday;        /* day of the month, range 1 to 31  */
   int tm_mon;         /* month, range 0 to 11             */
   int tm_year;        /* The number of years since 1900   */
   int tm_wday;        /* day of the week, range 0 to 6    */
   int tm_yday;        /* day in the year, range 0 to 365  */
   int tm_isdst;       /* daylight saving time             */	
};

返回值 (Return Value)

此函数返回与作为参数传递的日历时间对应的time_t值。 出错时,返回-1值。

例子 (Example)

以下示例显示了mktime()函数的用法。

#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("Error: unable to make time using mktime\n");
   } else {
      strftime(buffer, sizeof(buffer), "%c", &info );
      printf(buffer);
   }
   return(0);
}

让我们编译并运行上面的程序,它将产生以下结果 -

Wed Jul 4 00:00:01 2001
↑回到顶部↑
WIKI教程 @2018