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



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章