linux 得到系统时间

每天总结一点点,成长一点点~~

1.头文件

#include <ctime>
/* get system current time*/
char* getSysTime(){

    time_t now;
    struct tm *timenow;
    time(&now);
    timenow = localtime(&now);
    return  asctime(timenow);
}

2.QT下 头文件

#include <QDateTime>
QDateTime time;
           timeLabel->setText(time.currentDateTime().toString("yyyy") + "年 " + \
                             time.currentDateTime().toString("M") + "月 " + \
                             time.currentDateTime().toString("d") + "日 " + \
                             time.currentDateTime().toString("h") + "点 " + \
                             time.currentDateTime().toString("m") + "分 " + \
                             time.currentDateTime().toString("s") + "秒"
                              );

3.shell命令
system("while  : ; do clear; date; sleep 1; done");

本想获取该shell命令的输出的,结果没成功~我猜是因为不确定命令没有跑完,是不能捕获到输出的
所以直接用 "date"就输出成功了。。。

参考:http://www.linuxidc.com/Linux/2011-04/34092.htm
用的是:
void executeCMD(const char *cmd, char *result)
{
    char buf_ps[1024];
    char ps[1024]={0};
    FILE *ptr;
    strcpy(ps, cmd);
    if((ptr=popen(ps, "r"))!=NULL)
    {
        while(fgets(buf_ps, 1024, ptr)!=NULL)
        {
           strcat(result, buf_ps);
           if(strlen(result)>1024)
               break;
        }
        pclose(ptr);
        ptr = NULL;
    }
    else
    {
        printf("popen %s error\n", ps);
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值