QT在Linux环境下读取和设置系统时间
本文博客链接:http://blog.csdn.NET/jdh99,作者:jdh,转载请注明.
环境:
主机:Fedora12
开发软件: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"));
- }
读取到分,时间间隔用".",最终读取并显示的效果:2011.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 -systohc
http://blog.csdn.net/jdh99/article/details/7102196