QTはlinux環境でシステム時間を読み込み、設定します。

2717 ワード

QTはlinux環境でシステム時間を読み込み、設定します。
このブログのリンク:http://blog.csdn.net/jdh99、著者:jdh、転載は明記してください。
環境:
本体:Fedora 12
開発ソフト:QT
システム時間の読み取り
void moreidDialog::refresh_time()
{
    QDateTime time;
    txt_time->setText(time.currentDateTime().toString("yyyy") + "." + \
                      time.currentDateTime().toString("M") + "." + \
                      time.currentDateTime().toString("d") + "." + \
                      time.currentDateTime().toString("h") + "." + \
                      time.currentDateTime().toString("m"));
}
読み取りポイント、時間間隔は「.」で、最終的に読み取り表示する効果:201.12.27.9.14
システム時間の設定
//     
void moreidDialog::slot_save_time_key()
{
    QDateTime time;
    QString str = txt_time->text();
    //        
    if (str.count(".") != 4)
    {
        txt_time->setText(tr("ge shi cuo wu"));
        return;
    }
    int i = 0,j = 0;
    i = str.indexOf(".");
    QString year = str.mid(0,i);
    j = str.indexOf(".",i + 1);
    QString month = str.mid(i + 1,j - i - 1);
    i = j;
    j = str.indexOf(".",i + 1);
    QString day = str.mid(i + 1,j - i - 1);
    i = j;
    j = str.indexOf(".",i + 1);
    QString hour = str.mid(i + 1,j - i - 1);
    i = j;
    j = str.indexOf(".",i + 1);
    QString min = str.mid(i + 1,j - i - 1);
    bool ok = false;
    year.toInt(&ok);
    if (ok == false)
    {
        txt_time->setText(tr("ge shi cuo wu"));
        return;
    }
    month.toInt(&ok);
    if (ok == false)
    {
        txt_time->setText(tr("ge shi cuo wu"));
        return;
    }
    day.toInt(&ok);
    if (ok == false)
    {
        txt_time->setText(tr("ge shi cuo wu"));
        return;
    }
    hour.toInt(&ok);
    if (ok == false)
    {
        txt_time->setText(tr("ge shi cuo wu"));
        return;
    }
    min.toInt(&ok);
    if (ok == false)
    {
        txt_time->setText(tr("ge shi cuo wu"));
        return;
    }
    str = "date -s " + month + "/" + day + "/" + year;
    system(str.toLatin1().data());
    str = "date -s " + hour + ":" + min + ":" + "00";
    system(str.toLatin1().data());
    //     CMOS
    system("clock -w");
}
同期システムクロックとハードウェアクロック時間コマンド:
ハードウェアクロックはシステムクロックに同期します。hwclock--hctosys
システムクロックはハードウェアクロックに同期します。hwclock-sysstohc