Linux时间设置函数化
linuxTime.h
1 #ifndef LinuxTime_H 2 #define LinuxTime_H 3 #include <QtCore/QCoreApplication> 4 5 class LinuxTime 6 { 7 public: 8 void setTime(int m_nhour,int m_nminute,int m_nsecond); 9 void setDate(int m_nyear,int m_nmonth,int m_nday); 10 11 private: 12 int m_nyear; 13 int m_nmonth; 14 int m_nday; 15 int m_nhour; 16 int m_nminute; 17 int m_nsecond; 18 }; 19 20 #endif
linuxTime.cpp
1 #include "linuxTime.h" 2 void LinuxTime::setTime(int m_nhour,int m_nminute,int m_nsecond) 3 { 4 QString strTime; 5 strTime = QString("date -s %1:%2:%3") .arg(m_nhour).arg(m_nminute).arg(m_nsecond); 6 system(strTime.toLatin1().data()); 7 system("clock -w"); 8 9 } 10 11 void LinuxTime::setDate(int m_nmonth,int m_nday,int m_nyear) 12 { 13 QString strDate; 14 strDate = QString("date -s %1/%2/%3") .arg(m_nmonth).arg(m_nday).arg(m_nyear); 15 system(strDate.toLatin1().data()); 16 system("clock -w"); 17 // system("find . -type f -exec touch {} \; "); 18 }
main.cpp
1 #include <QtCore/QCoreApplication> 2 #include <QTextStream> 3 #include <QDate> 4 #include <QTime> 5 #include <QString> 6 #include "linuxTime.h" 7 int main(int argc,char *argv[]) 8 { 9 QCoreApplication app(argc, argv); 10 QTextStream out(stdout); 11 if (argc < 6) 12 { 13 out<<"Usage : linuxTime_set setdate -s mm dd yy or linuxTime_set settime -s hh mm ss"<<endl; 14 exit(0); 15 } 16 const char * date = "setdate"; 17 const char * time = "settime"; 18 const char * in_date =argv[1]; 19 20 if(QString::compare(date,in_date)==0) 21 { 22 bool ok; 23 QString mm = argv[3]; 24 QString dd = argv[4]; 25 QString yy = argv[5]; 26 27 int in_nmm = mm.toInt(&ok, 10); 28 int in_ndd = dd.toInt(&ok, 10); 29 int in_nyy = yy.toInt(&ok, 10); 30 31 LinuxTime setdate; 32 setdate.setDate(in_nmm,in_ndd,in_nyy); 33 exit(0); 34 } 35 36 if(QString::compare(time,in_date)==0) 37 { 38 bool ok; 39 QString hh = argv[3]; 40 QString mm = argv[4]; 41 QString ss = argv[5]; 42 43 int in_nhh = hh.toInt(&ok, 10); 44 int in_nmm = mm.toInt(&ok, 10); 45 int in_nss = ss.toInt(&ok, 10); 46 47 LinuxTime settime; 48 settime.setTime(in_nhh,in_nmm,in_nss); 49 exit(0); 50 } 51 return app.exec(); 52 }
运行结果: