【C/C++】获取当前系统时间

 1 #include<iostream>
 2 #include<Ctime>
 3 using namespace std;
 4 
 5 int main()
 6 {
 7     time_t t;
 8     time(&t); 
 9     cout<<"now is: "<<ctime(&t)<<endl; 
10     return 0;
11 }

【NOTE】

关于time_t,<time.h>中有如下定义:

------------------------------------------------------------------------------
#ifndef _TIME_T_DEFINED
typedef long time_t;        /* time value */
#define _TIME_T_DEFINED     /* avoid multiple def's of time_t */
#endif

------------------------------------------------------------------------------

也就是说,time_t类型实际就是long int类型。

time函数原型:

time_t time(time_t * timer);

//返回值与timer的值相同,都是表示距离 1970年1月1日0时0分0秒 的秒数,其实从32位长度表示的long int只能表示到 2038年1月18日19时14分07秒。

ctime函数原型:

char *ctime(const time_t *timer);

//把timer转换为比较直观的时间表示方式[星期 月 日 时:分:秒 年],并以字符串形式返回。

原文地址:https://www.cnblogs.com/wxiaoli/p/5554334.html