C++封装一个时间显示类并调用执行

#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);
    } 
}

运行效果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值