C++新版ctime_s、gmtime_s
之前:
下面展示一些
内联代码片
。
int main( )
{
// 基于当前系统的当前日期/时间
time_t now = time(0);
// 把 now 转换为字符串形式
char* dt = ctime(&now);
cout << "本地日期和时间:" << dt << endl;
// 把 now 转换为 tm 结构
tm *gmtm = gmtime(&now);
dt = asctime(gmtm);
cout << "UTC 日期和时间:"<< dt << endl;
}
更改后:
下面展示一些
内联代码片
。
void time1()//返回当前时间
{
// 基于当前系统的当前日期/时间
time_t now = time(0);
// 把 now 转换为字符串形式 ctime报错,ctime_s使用新方法
char dt[66];//先定义在使用,dt放ctime_s函数内去了
ctime_s(dt,sizeof dt, &now);//duty time
cout << "本地日期和时间:" << dt << endl;
// 把 now 转换为 tm 结构
tm gmtm;
gmtime_s(&gmtm,&now);
asctime_s(dt, sizeof dt, &gmtm);//把tm结构体的转换为字符串
cout << "UTC 日期和时间:" << dt<< "\n" << endl;
}