C++ Reference: Standard C++ Library reference: C Library: ctime: gmtime

发布于:2022-10-31 ⋅ 阅读:(340) ⋅ 点赞:(0)

C++官网参考链接:https://cplusplus.com/reference/ctime/gmtime/

函数 
<ctime>
gmtime
struct tm * gmtime (const time_t * timer);
将time_t转换为tm作为UTC时间
使用timer指向的值用表示相应时间的值填充tm结构,表示为UTC时间(即GMT时区的时间)。
有关本地时间替代方案,请参见localtime

形参
timer
指向time_t类型对象的指针,该对象包含时间值。
time_t是一种基本算术类型(arithmetic type)的别名,它能够表示函数time返回的时间。

返回值
一个指向tm结构的指针,其成员用与timer的UTC时间表示相对应的值填充。
返回值指向一个内部对象,其有效性或值可能被后续对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;
}
输出:

数据竞争
该函数访问由timer指向的对象。
该函数还访问和修改一个共享内部对象,这可能会在对gmtime和localtime的并发调用中引入数据竞争。有些库提供了避免这种数据竞争的替代函数:gmtime_r(不可移植)。

异常(C++) 
无抛出保证:此函数从不抛出异常。 

另请参考
asctime    Convert tm structure to string (function)
ctime    Convert time_t value to string (function)
localtime    Convert time_t to tm as local time (function)
mktime    Convert tm structure to time_t (function)
time    Get current time (function) 


网站公告

今日签到

点亮在社区的每一天
去签到