當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


C語言 gmtime用法及代碼示例

C語言time頭文件(time.h)中gmtime函數的用法及代碼示例。

用法:

struct tm * gmtime (const time_t * timer);
將time_t轉換為tm作為UTC時間
使用由指向的值timer填補tm結構,其值代表相應的時間,表示為UTC時間(即,格林尼治標準時間(GMT)時區的時間)。

有關本地時間的替代方法,請參見localtime

參數

timer
指向類型的對象的指針time_t包含一個時間值。
time_t是基本麵的別名算術類型能夠代表函數返回的時間time

返回值

指向一個的指針tm其成員填充有與UTC時間表示相對應的值的結構timer

返回的值指向一個內部對象,該對象的有效性或值可以通過隨後的任何調用來更改gmtime或者localtime

示例

/* gmtime example */
#include <stdio.h>      /* puts, printf */
#include <time.h>       /* time_t, struct tm, time, gmtime */

#define MST (-7)
#define UTC (0)
#define CCT (+8)

int main ()
{
  time_t rawtime;
  struct tm * ptm;

  time ( &rawtime );

  ptm = gmtime ( &rawtime );

  puts ("Current time around the World:");
  printf ("Phoenix, AZ (U.S.) :  %2d:%02d\n", (ptm->tm_hour+MST)%24, ptm->tm_min);
  printf ("Reykjavik (Iceland) : %2d:%02d\n", (ptm->tm_hour+UTC)%24, ptm->tm_min);
  printf ("Beijing (China) :     %2d:%02d\n", (ptm->tm_hour+CCT)%24, ptm->tm_min);

  return 0;
}


輸出:

Current time around the World:
Phoenix, AZ (U.S.) :    8:22
Reykjavik (Iceland) :  15:22
Beijing (China) :      23:22



相關用法


注:本文由純淨天空篩選整理自C標準庫大神的英文原創作品 C gmtime function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。