zoukankan      html  css  js  c++  java
  • QT在linux环境下读取和设置系统时间(通过system来直接调用Linux命令,注意权限问题)

    QT在Linux环境下读取和设置系统时间

    本文博客链接:http://blog.csdn.NET/jdh99,作者:jdh,转载请注明.

    环境

    主机:Fedora12

    开发软件:QT

    读取系统时间

    [cpp] view plain copy
     
    1. void moreidDialog::refresh_time()  
    2. {  
    3.     QDateTime time;  
    4.     txt_time->setText(time.currentDateTime().toString("yyyy") + "." +   
    5.                       time.currentDateTime().toString("M") + "." +   
    6.                       time.currentDateTime().toString("d") + "." +   
    7.                       time.currentDateTime().toString("h") + "." +   
    8.                       time.currentDateTime().toString("m"));  
    9. }  
    读取到分,时间间隔用".",最终读取并显示的效果:2011.12.27.9.14
     
    设置系统时间
    [cpp] view plain copy
     
    1. //保存时间键  
    2. void moreidDialog::slot_save_time_key()  
    3. {  
    4.     QDateTime time;  
    5.     QString str = txt_time->text();  
    6.     //判断格式是否正确  
    7.     if (str.count(".") != 4)  
    8.     {  
    9.         txt_time->setText(tr("ge shi cuo wu"));  
    10.         return;  
    11.     }  
    12.     int i = 0,j = 0;  
    13.     i = str.indexOf(".");  
    14.     QString year = str.mid(0,i);  
    15.     j = str.indexOf(".",i + 1);  
    16.     QString month = str.mid(i + 1,j - i - 1);  
    17.     i = j;  
    18.     j = str.indexOf(".",i + 1);  
    19.     QString day = str.mid(i + 1,j - i - 1);  
    20.     i = j;  
    21.     j = str.indexOf(".",i + 1);  
    22.     QString hour = str.mid(i + 1,j - i - 1);  
    23.     i = j;  
    24.     j = str.indexOf(".",i + 1);  
    25.     QString min = str.mid(i + 1,j - i - 1);  
    26.     bool ok = false;  
    27.     year.toInt(&ok);  
    28.     if (ok == false)  
    29.     {  
    30.         txt_time->setText(tr("ge shi cuo wu"));  
    31.         return;  
    32.     }  
    33.     month.toInt(&ok);  
    34.     if (ok == false)  
    35.     {  
    36.         txt_time->setText(tr("ge shi cuo wu"));  
    37.         return;  
    38.     }  
    39.     day.toInt(&ok);  
    40.     if (ok == false)  
    41.     {  
    42.         txt_time->setText(tr("ge shi cuo wu"));  
    43.         return;  
    44.     }  
    45.     hour.toInt(&ok);  
    46.     if (ok == false)  
    47.     {  
    48.         txt_time->setText(tr("ge shi cuo wu"));  
    49.         return;  
    50.     }  
    51.     min.toInt(&ok);  
    52.     if (ok == false)  
    53.     {  
    54.         txt_time->setText(tr("ge shi cuo wu"));  
    55.         return;  
    56.     }  
    57.     str = "date -s " + month + "/" + day + "/" + year;  
    58.     system(str.toLatin1().data());  
    59.     str = "date -s " + hour + ":" + min + ":" + "00";  
    60.     system(str.toLatin1().data());  
    61.     //强制写入到CMOS  
    62.     system("clock -w");  
    63. }  
     
    同步系统时钟与硬件时钟时间命令:
    硬件时钟同步到系统时钟:hwclock --hctosys
    系统时钟同步到硬件时钟:hwclock -systohc

    http://blog.csdn.net/jdh99/article/details/7102196

  • 相关阅读:
    用队列打印杨辉三角
    mysql允许远程连接
    window文件恢复工具
    android 虚拟机没有sd卡
    StringUtils 的填充方法
    plsql 中出现 Dynamic Performance Tables not accessible 问题解决
    oracle数据库服务介绍
    遮罩的使用
    <pre>标签
    总结五个小技巧定位数据库性能问题
  • 原文地址:https://www.cnblogs.com/findumars/p/6152855.html
Copyright © 2011-2022 走看看