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

Qt4 设置系统日期时间

2013-09-02 10:19 471 查看
qt带的类只能读取日期时间,要修改系统的日期时间还需要date和hwclock:
QProcess changedt,syncit;
QStringList cmdlist;

 cmdlist<<"-s" << m_ui->dateTimeEdit->dateTime().toString("yyyy-MM-dd hh:mm:ss");

 changedt.start("date",cmdlist);

 cmdlist.clear();

 cmdlist<<"-w";

 syncit.start("hwclock",cmdlist);

 changedt.waitForFinished(2000);

 syncit.waitForFinished(2000);
以上转自:http://blog.csdn.net/lanmanck
 
 
qt-读取和修改系统时间
 

     QTime ct = QTime::currentTime();

修改系统时间
     // change the system time

    QDateTime dt = QDateTime::currentDateTime();

    dt.setTime(timeEditor->time());

    time_t tt = (time_t)dt.toTime_t();

    int r = stime(&tt);

    if (r)

    {

        if (errno == EPERM)

            QMessageBox::warning(this, "Error", "You don't have permission to                                            change system time.");
     }
/***************/
设置现在时间为2004年8月3日14点55分23秒
 

  adjustdate(2004,8,3,14,55,23);  

  void       adjustdate(int       year,int       mon,int       mday,int       hour,int       min,int       sec       )  

  {  

                          time_t       t;  

                          struct           tm       nowtime       ;   

                          nowtime.tm_sec=sec;/*Seconds.[0-60]   (1   leap    second)*/                                                        

                          nowtime.tm_min=min;/*       Minutes.[0-59]       */                                                                            

                          nowtime.tm_hour=hour;/*       Hours.   [0-23]       */  

                          nowtime.tm_mday=mday;/*       Day.[1-31]       */  

                          nowtime.tm_mon=mon-1;/*       Month.   [0-11]       */  

                          nowtime.tm_year=year-1900       ;/*       Year-       1900.*/  

                          nowtime.tm_isdst=-1       ;/*       DST.[-1/0/1]*/  

                          t=mktime(&nowtime);  

                          stime(&t);  

  }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: