直接上菜:
#include <iostream>
#include <string>
using namespace std;
static string getCurrentTimeStr()
{
time_t t = time(NULL);
char ch[64] = {0};
strftime(ch, sizeof(ch) - 1, "%Y-%m-%d %H:%M:%S", localtime(&t)); //年-月-日 时-分-秒
return ch;
}
int main()
{
cout << getCurrentTimeStr() << endl;
return 0;
}
结果:
ubuntu@VM-0-13-ubuntu:~$ ./a.out
2017-12-24 16:31:17
ubuntu@VM-0-13-ubuntu:~$ date
Sun Dec 24 16:31:19 CST 2017
ubuntu@VM-0-13-ubuntu:~$
good