QT QDateTime

  intmain(intargc,char *argv[])

{
    QApplication a(argc, argv);
    Widget w;
    if(w.m_display)
        w.show();
    else
        QTimer::singleShot(0,qApp,SLOT(quit()));
    return a.exec();
}


void Widget::checkDate()
{
    QFile file("./base.inf");
    QString format("yyyy-MM-dd hh:mm:ss");
    //当前时间
    QDateTime currentTime=QDateTime::currentDateTime();
    QString time=currentTime.toString(format);
    qDebug()<<"currentTime:"<<time;
    //参考时间文件不存在则设置当前时间为参考时间
    if(!file.exists()){
        if(!file.open(QIODevice::WriteOnly))
            qDebug("base.inf open::WriteOnly failed");
        //base64格式存储文本
        QByteArray a=currentTime.toString(format).toUtf8().toBase64();
        file.write(a.data(),a.size());
        file.close();
    }
 
  
    if(!file.open(QIODevice::ReadOnly)){
        qDebug("base.inf open::ReadOnly failed");
        m_display=false;
        return;
    }
    //解析base64
    QByteArray timeArray=QByteArray::fromBase64(file.readAll());
    qDebug("baseTime:"+timeArray);
    file.close();
    QDateTime baseTime=QDateTime::fromString(QString(timeArray),format);
    //截止日期设置成参考日期之后一个月
    QDateTime deadLine=baseTime.addMonths(1);
    qDebug()<<"deadLine:"<<deadLine.toString(format);
    //时间天数差值
    int days=currentTime.daysTo(deadLine);
    if(days<0/*currenTime>deadLine*/){
        m_display=false;
        QMessageBox::information(0,"提示","软件试用已结束,请购买正式产品",QMessageBox::Ok);
    }
    else{
        QMessageBox::information(0,"提示",QString("还有%1天试用时间").arg(days),QMessageBox::Ok);
    }
}
 
 

猜你喜欢

转载自blog.csdn.net/jianzemax/article/details/73916995
QT