#include <iostream>
#include <string>
#include <vector>
using namespace std;
extern "C"
{
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <string.h>
}
//获取系统时间
class Time
{
public:
Time()
{
wday.push_back("一");
wday.push_back("二");
wday.push_back("三");
wday.push_back("四");
wday.push_back("五");
wday.push_back("六");
wday.push_back("日");
memset(buf,0,sizeof(buf));
}
const char *showTime()
{
//获取当地时间
time_space = time(NULL);
time_data = localtime(&time_space);
sprintf(buf,"%d年%d月%d日 星期%s %02d:%02d:%02d",1900+time_data->tm_year,1+time_data->tm_mon,time_data->tm_mday,wday.at(time_data->tm_wday-1).c_str(),time_data->tm_hour,time_data->tm_min,time_data->tm_sec);
return buf;
}
private:
time_t time_space;
struct tm *time_data;
char buf[4096];
vector<string> wday;
};
int main()
{
Time time_Show;
while(1)
{
cout << time_Show.showTime() << endl;
sleep(1);
}
}
运行效果