您的位置:首页 > 编程语言 > Qt开发

Qt设置系统时间

2012-10-27 13:32 337 查看
读取系统时间

[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());

str = "date -s " + month + day +hour+min+"00"+ year;//SBC8100上测试用通过

system(str.toLatin1().data());

//强制写入到CMOS

system("clock -w");
}

同步系统时钟与硬件时钟时间命令:

硬件时钟同步到系统时钟:hwclock --hctosys

系统时钟同步到硬件时钟:hwclock -systohc
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: