QT 修改系统时间

guozhixin 2010-04-12 10:30:50
如题,各位,怎么在QT下修改系统时间呀?是linux下
...全文
711 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
钱国正 2011-09-30
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 hugerat 的回复:]
呵呵,这个问题其实很简单,贴一个我写的函数给你。

//传入的时间格式为"yyyy-MM-dd hh:mm:ss"
bool communicate::setRealTime(char *timestring)
{
int fd, retval;
struct rtc_time rtc_tm;

QString temp;
QStringList templist……
[/Quote]楼主你好。最后面的open("/dev/rtc0",O_RDONLY);这个及以下是干嘛用的?设置时间上面已经做到了啊,为何还要再设置一次呢?设置的是硬件时钟?那么这句话之前的设置在系统重启后还会存在吗?
tt2com 2011-08-15
  • 打赏
  • 举报
回复
hugerat 2010-04-19
  • 打赏
  • 举报
回复
呵呵,这个问题其实很简单,贴一个我写的函数给你。

//传入的时间格式为"yyyy-MM-dd hh:mm:ss"
bool communicate::setRealTime(char *timestring)
{
int fd, retval;
struct rtc_time rtc_tm;

QString temp;
QStringList templist;
QStringList templist2;
time_t t;
struct tm nowtime;
int year,month,day,hour,min,sec;
temp = temp.fromAscii(timestring+1,(int)(*timestring));
qDebug()<<"set time is "<<temp;
templist = temp.split(" ");
if(templist.count() < 2) return false;
temp = templist.at(0);
templist2 = temp.split("-");
if(templist2.count()<3) return false;
year = templist2.at(0).toInt();
month = templist2.at(1).toInt();
day = templist2.at(2).toInt();
temp = templist.at(1);
templist2 = temp.split(":");
if(templist2.count() < 3) return false;
hour = templist2.at(0).toInt();
min = templist2.at(1).toInt();
sec = templist2.at(2).toInt();
qDebug()<<year<<" "<<month<<" "<<day<<" "<<hour<<" "<<min<<" "<<sec;
if(year<2000 || year > 2099 || month <1 || month >12 || day <1 || day >31 || hour < 0 || hour > 23 || min < 0 || min > 59 || sec <0 || sec > 60)
{
return false;
}

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=day;// Day.[1-31]
nowtime.tm_mon=month-1;// Month. [0-11]
nowtime.tm_year=year-1900 ;// Year- 1900.
nowtime.tm_isdst=-1 ;//DST.[-1/0/1]
t=mktime(&nowtime);
stime(&t);
qDebug()<<"set time success";

EndDate = EndDate.currentDateTime();
StartDate = EndDate.addMSecs(100);
logEndDate = logEndDate.currentDateTime();
logStartDate = logEndDate.addDays(1);
nouploadDate = nouploadDate.currentDateTime();
//以下为设置硬件时钟
fd = open ("/dev/rtc0", O_RDONLY);
if (fd == -1)
{
close(fd);
qDebug()<<"hw rtc open fail!";
}
rtc_tm.tm_year = year-1900;
rtc_tm.tm_mon = month-1;
rtc_tm.tm_mday = day;
rtc_tm.tm_hour = hour;
rtc_tm.tm_min = min;
rtc_tm.tm_sec = sec;
retval = ioctl(fd, RTC_SET_TIME, &rtc_tm);
if (retval == -1)
{
close(fd);
qDebug()<<"hw rtc set fail!";
return false;
}
close(fd);
msleep(100);
return true;
}
dext 2010-04-18
  • 打赏
  • 举报
回复
设置系统时间 是平台 相关的

linux
stime
SRC: kernel/time.c

Windows
SetSystemTime
HEADER: Winbase.h / Windows.h

mercedes2 2010-04-12
  • 打赏
  • 举报
回复
setDate()?
wesleyluo 2010-04-12
  • 打赏
  • 举报
回复
C++怎么修改,QT就怎么写,一样可用。

16,199

社区成员

发帖
与我相关
我的任务
社区描述
Qt 是一个跨平台应用程序框架。通过使用 Qt,您可以一次性开发应用程序和用户界面,然后将其部署到多个桌面和嵌入式操作系统,而无需重复编写源代码。
社区管理员
  • Qt
  • 亭台六七座
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧