目录

struct tm *gmtime(const time_t *timer)

描述 (Description)

C库函数struct tm *gmtime(const time_t *timer)使用struct tm *gmtime(const time_t *timer)指向的值来填充tm结构,其中的值表示相应的时间,以协调世界时(UTC)或GMT时区表示。

声明 (Declaration)

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

struct tm *gmtime(const time_t *timer)

参数 (Parameters)

  • timeptr - 这是指向表示日历时间的time_t值的指针。

返回值 (Return Value)

此函数返回指向填充了时间信息的tm结构的指针。以下是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             */	
};

例子 (Example)

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

#include <stdio.h>
#include <time.h>
#define BST (+1)
#define CCT (+8)
int main () {
   time_t rawtime;
   struct tm *info;
   time(&rawtime);
   /* Get GMT time */
   info = gmtime(&rawtime );
   printf("Current world clock:\n");
   printf("London : %2d:%02d\n", (info->tm_hour+BST)%24, info->tm_min);
   printf("China  : %2d:%02d\n", (info->tm_hour+CCT)%24, info->tm_min);
   return(0);
}

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

Current world clock:
London : 14:10
China : 21:10
↑回到顶部↑
WIKI教程 @2018