【发布时间】:2014-10-02 08:03:31
【问题描述】:
#include <ctime>
#include <iostream>
using namespace std;
int main() {
time_t t = time(0); // get time now
struct tm * now = localtime( & t );
cout << now->tm_mday << '-'//day
<< (now->tm_mon +1 ) << '-'//month
<< (now->tm_year +1900 )//year
<<endl
<<now->tm_hour//hour
<<'-'<<now->tm_min//min
<<'-'<< now->tm_sec//sec
<< endl;
return 0;
}
这段代码给了我系统日期和时间,我唯一的问题是时间没有更新。
例如:= 时间没有向前移动,卡在固定时间,比如 1.33.10
【问题讨论】:
-
请编辑您的问题以更好地缩进您的代码。您在哪个硬件、哪个操作系统、哪个编译器和编译选项上运行它?你知道strftime(3) &
<chrono>吗?您编译时是否包含所有警告和调试信息 (g++ -Wall -g)?您是否使用过调试器 (gdb)? -
您可能缺少一些
#include指令。 time(2)你需要的文件#include <time.h> -
这对我来说很好用(在固定大括号之后)。
-
我编辑了代码,我知道 strftime 和
但我想它们都不会帮助我更新时间。 -
我正在使用带有代码块编译器的 Windows 7。