
已毕这个功能的按序有许多尊龙凯时体育,这里咱们看一下最常用的一种状貌。
获取系统的时间 time.cpp:
#include <iostream>
#include <time.h>
#include <string>
int main()
{
std::string s;
char stime[256] = {0}尊龙凯时体育;
time_t now_time;
time(&now_time);
s = ctime(&now_time);
std::cout << s << std::endl;
return 0;
}
通过编译,g++ -time.cpp -o time ,启动./time,后不错取得系统时间。
然后通过函数 strftime() 不错继承我方念念要输出的时局,
如,仅仅输出时,分,秒:
#include <iostream>
#include <time.h>
#include <string>
int main()
{
std::string s;
char stime[256] = {0};
time_t now_time;
time(&now_time);
strftime(stime,sizeof(stime),"%H:%M:%S",localtime(&now_time));
s = stime + '\0';
std::cout << s << std::endl;
return 0;
}
