環境:
主機:Fedora 12
開發軟件:QT
讀取系統時間
[cpp]
- 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"));
- }
讀取到分,時間間隔用".",最終讀取並顯示的效果:2011.12.27.9.14
設置系統時間
[cpp]
- //保存時間鍵
- 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 -systohc