竣事这个功能的步骤有许多亚博体育(中国)官方网站,这里咱们看一下最常用的一种形式。
获取系统的工夫 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;
}