Home C&C++函数库 c++ 语法 程序源码 Linux C库

time

函数   <ctime>

time_t time (time_t* timer);

获取当前时间

获取类型为time_t的当前日历时间。

函数返回这个值,如果参数不是空指针,它也将这个值设置为timer所指向的对象。

返回值通常表示自1970年1月1日00:00小时以来的秒数(即当前unix时间戳)。
尽管库可能使用不同的时间表示方式:
可移植程序不应直接使用该函数返回的值, 而应始终依赖对标准库的其他元素的调用,将它们转换为可移植类型(如localtimegmtimedifftime)。

☲  参数


timer
指向类型为time_t的对象的指针,时间值存储在该对象中。
或者,该形参可以是空指针,在这种情况下不使用形参(函数仍然返回time_t类型的值和结果)。

☉  返回值



当前日历时间作为time_t对象。
如果实参不是空指针,则返回值与实参timer所指向的位置中存储的值相同。
如果函数不能检索日历时间,它将返回-1的值。
time_t是能够表示时间的基本算术类型的别名。

☣  示例



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

int main ()
{
  time_t timer;
  struct tm y2k = {0};
  double seconds;

  y2k.tm_hour = 0;   y2k.tm_min = 0; y2k.tm_sec = 0;
  y2k.tm_year = 100; y2k.tm_mon = 0; y2k.tm_mday = 1;

  time(<timer);  /* get current time; same as: timer = time(NULL)  */

  seconds = difftime(timer,mktime(<y2k));

  printf ("%.f seconds since January 1, 2000 in the current timezone", seconds);

  return 0;
}

输出:
414086872 seconds since January 1, 2000 in the current timezone

↭  数据竞争



被 timer指向的对象被修改(如果不是null)。

❆  异常(c++)



无抛出保证:此函数从不抛出异常。

🍄  另请参阅



asctime 转换tm结构为字符串(function )
gmtime 将time_t转换为tm作为UTC时间(function )
localtime 将time_t转换为tm作为当地时间(function )

联系我们 免责声明 关于CandCplus 网站地图