QT技巧系列(2)QT中数据库保存的以秒为单位的时间秒转换成日期格式示例及代码

         QT中将数据库中保存的以秒为单位的时间

                                                        转换成日期格式QDateTime显示示例及代码

 

问题:数据库用UINT整数保存时间秒,需要显示为正常的日期时间格式。

 

解决方法: QDateTime类提供了uint toTime_t() const函数 这个函数返回当前自1970-01-01 00:00:00经过了多少秒,返回值为一个UINT类型。

函数fromTime_t(uint seconds)函数则可以将距1970-01-01 00:00:00 的秒数转换为QDateTime,,seconds 表示的是距 1970-01-01 00:00:00 的秒数。

示例如下:

    QDateTime timeT = QDateTime::currentDateTime();   //获取当前时间

    uint secondsT = timeT.toTime_t();

    qDebug() << "秒数="+QString::number(secondsT);



    //uint secondsIn =1582591473;

    qDebug() << "秒数转换为日期="+QDateTime::fromTime_t(secondsT).toString("yyyy-MM-dd hh:mm:ss");

测试输出结果如下:

注意:上面时间都是以1970-01-01 00:00:00为基准,这个其实是UTC时间,又称世界统一时间、世界标准时间、国际协调时间,简称UTC,是以原子时秒长为基础,在时刻上尽量接近于世界时的一种时间计量系统。1979年12月3日在内瓦举行的世界无线电行政大会通过决议,确定用“世界协调时间”取代“格林威治时间”,作为无线电通信领域内的国际标准时间,应用于天文学及天体,是测绘学上的一类用语。

     UTC时间同本地时间关系:

           UTC +时间差=本地时间

     时间差根据时区定,东边为正,西边为负,如北京东八区,时间差0800,8小时。如果UTC时间是 2020-02-25 00:00:00 那么北京时间就是 20202-02-25 08:00:00。

 示例如下:

    QDateTime dateTime = QDateTime::fromString("1970-01-01 08:00:00","yyyy-MM-dd hh:mm:ss");

    dateTime.setTimeSpec(Qt::LocalTime);

    uint secondsLocal= dateTime.toTime_t();  //secondsLocal =0;

    qDebug() << "local秒数="+QString::number(secondsLocal);

    dateTime.setTimeSpec(Qt::UTC);

    uint secondsUTC = dateTime.toTime_t();   //secondsUTC = 28800;8小时

    qDebug() << "UTC秒数="+QString::number(secondsUTC);

 

测试输出结果如下:

 

由于fromTime_t()是静态函数,返回QDateTime,缺省直接转换成Qt::LocalTime,即以下操作,不管如何,返回时间值都不变。示例如下:

    //反向转换

     QDateTime dateTime0 = QDateTime::fromTime_t(0);

     dateTime0.setTimeSpec(Qt::LocalTime);

     qDebug() <<"local日期:"+dateTime0.toString("yyyy-MM-dd hh:mm:ss");

     dateTime0.setTimeSpec(Qt::UTC);

     qDebug() <<"UTC日期:"+dateTime0.toString("yyyy-MM-dd hh:mm:ss");

测试输出结果如下:

 

 

注:实战示例,解疑答惑。

           --不间端地思考,实时地批判你的工作!

  • 7
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的示例代码,演示如何使用Qt的tableView显示数据库的数据,并且可以添加、更新和删除数据。 ```cpp #include <QtSql> #include <QtWidgets> int main(int argc, char *argv[]) { QApplication app(argc, argv); // 连接数据库 QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE"); db.setDatabaseName("mydatabase.db"); if (!db.open()) { QMessageBox::critical(nullptr, "Error", "Failed to connect to database!"); return 1; } // 创建表格 QSqlQuery query; query.exec("CREATE TABLE IF NOT EXISTS students (" "id INTEGER PRIMARY KEY AUTOINCREMENT," "name TEXT," "age INTEGER)"); // 查询数据 QSqlQueryModel *model = new QSqlQueryModel; model->setQuery("SELECT * FROM students"); // 创建tableView并设置数据模型 QTableView *tableView = new QTableView; tableView->setModel(model); // 创建添加按钮 QPushButton *addButton = new QPushButton("Add"); QObject::connect(addButton, &QPushButton::clicked, [&](){ // 弹出对话框,获取用户输入 QString name = QInputDialog::getText(nullptr, "Add student", "Name:"); bool ok; int age = QInputDialog::getInt(nullptr, "Add student", "Age:", 0, 0, 100, 1, &ok); if (!ok) { return; } // 插入数据 QSqlQuery query; query.prepare("INSERT INTO students (name, age) VALUES (:name, :age)"); query.bindValue(":name", name); query.bindValue(":age", age); if (!query.exec()) { QMessageBox::critical(nullptr, "Error", "Failed to insert data!"); return; } // 刷新数据 model->setQuery("SELECT * FROM students"); }); // 创建更新按钮 QPushButton *updateButton = new QPushButton("Update"); QObject::connect(updateButton, &QPushButton::clicked, [&](){ // 获取当前选的行 QModelIndexList indexes = tableView->selectionModel()->selectedIndexes(); if (indexes.isEmpty()) { QMessageBox::information(nullptr, "Information", "Please select a row to update!"); return; } int row = indexes.first().row(); // 获取用户输入 QString name = QInputDialog::getText(nullptr, "Update student", "Name:", QLineEdit::Normal, model->index(row, 1).data().toString()); bool ok; int age = QInputDialog::getInt(nullptr, "Update student", "Age:", model->index(row, 2).data().toInt(), 0, 100, 1, &ok); if (!ok) { return; } // 更新数据 QSqlQuery query; query.prepare("UPDATE students SET name=:name, age=:age WHERE id=:id"); query.bindValue(":name", name); query.bindValue(":age", age); query.bindValue(":id", model->index(row, 0).data().toInt()); if (!query.exec()) { QMessageBox::critical(nullptr, "Error", "Failed to update data!"); return; } // 刷新数据 model->setQuery("SELECT * FROM students"); }); // 创建删除按钮 QPushButton *deleteButton = new QPushButton("Delete"); QObject::connect(deleteButton, &QPushButton::clicked, [&](){ // 获取当前选的行 QModelIndexList indexes = tableView->selectionModel()->selectedIndexes(); if (indexes.isEmpty()) { QMessageBox::information(nullptr, "Information", "Please select a row to delete!"); return; } int row = indexes.first().row(); // 删除数据 QSqlQuery query; query.prepare("DELETE FROM students WHERE id=:id"); query.bindValue(":id", model->index(row, 0).data().toInt()); if (!query.exec()) { QMessageBox::critical(nullptr, "Error", "Failed to delete data!"); return; } // 刷新数据 model->setQuery("SELECT * FROM students"); }); // 创建布局 QVBoxLayout *layout = new QVBoxLayout; layout->addWidget(tableView); QHBoxLayout *buttonLayout = new QHBoxLayout; buttonLayout->addWidget(addButton); buttonLayout->addWidget(updateButton); buttonLayout->addWidget(deleteButton); layout->addLayout(buttonLayout); // 创建窗口 QWidget *widget = new QWidget; widget->setLayout(layout); widget->show(); return app.exec(); } ``` 这个示例程序创建了一个名为"students"的表格,包含"id"、"name"和"age"三个字段。程序使用了QSqlQueryModel类将查询结果换为tableView的数据模型,使用了QPushButton类实现了添加、更新和删除数据的功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值