实现这个功能的方法有很多,有兴趣的朋友可以去自己查找一下。
获取系统的时间 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,后可以获得系统时间。
输出为:
Wed Dec 24 13:51:07 2014
然后通过函数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;
- }
编译后运行,输出:
13:54:31
来源: http://www.phpxs.com/code/1004023/