InfluxDB 查询 - 时间段 - 数量 - 设定时区 - QT

以下参考:链接

	QDateTime localDate = QDateTime::fromString("2018-12-11 10:40:00", "yyyy-MM-dd hh:mm:ss");//输入的时间
	QDateTime utcDate = localDate.toTimeZone(zone);//拿到的是0时区的当前时间
	QDateTime currentTime = QDateTime::currentDateTime();//当前时区的当前时间
	QDateTime changeLocaldate = QDateTime::fromMSecsSinceEpoch(localDate.toMSecsSinceEpoch(), Qt::UTC);
	QDateTime changeutcDate = changeLocaldate.toTimeZone(zone);
	QTimeZone currentZone = currentTime.timeZone();
	QString currentZoneId = currentZone.id();//当前时间的当前时区
	QDateTime currentUtcTime = QDateTime::currentDateTimeUtc();//当前的UTC时间
	
	ERROR_TRACE("Local Date: %s", localDate.toString().toUtf8().data());
	ERROR_TRACE("utc Date: %s", utcDate.toString().toUtf8().data());
	ERROR_TRACE("Current Date: %s", currentTime.toString().toUtf8().data());
	ERROR_TRACE("change Current Date: %s", changeLocaldate.toString().toUtf8().data());
	ERROR_TRACE("change Current utc Date: %s", changeutcDate.toString().toUtf8().data());
	ERROR_TRACE("Current Zone Id: %s", currentZoneId.toUtf8().data());
	ERROR_TRACE("Current UTC Date: %s", currentUtcTime.toString().toUtf8().data());

  • 上面是demo测试,QTIMEZone和QDateTime在使用对象的时候,都要判断一下 isValid(),因为在Qt源码里面,这两个类的实现,都包含一个私有的d指针,来保证二进制兼容。在某些情况下,这个d指针可能为空,就会导致程序崩溃

以下为 Qt 的实现

QString InfluxdbAPI::showTablePointsTimeRange(const QString &db, const QString &table, const QDateTime &startTime, const QDateTime &endTime)
{
    // 输入查询的时区应为UTC时区,早 北京时区 8小时,所以应该手动减去8 或者 用
    // QDateTime::toTimeZone(zone)

    QString sql = QString("select time,pressure,testBool from %1 ").arg(table);

    // 注意,时间要用单引号括起来
    QString timeRangeStart = QString("where time>='%1' ").arg(startTime.toString("yyyy-MM-ddTHH:mm:ssZ"));
    QString timeRangeEnd   = QString("and time<='%1' ").arg(endTime.toString("yyyy-MM-ddTHH:mm:ssZ"));

    sql.append(timeRangeStart);
    sql.append(timeRangeEnd);

    // 这一句表示返回以上海时区反馈查询的数据
    sql.append("tz('Asia/Shanghai')");

    qDebug() << sql;

    //sql.append("group by xxx ");
    //sql.append("order by yyy ");

    QString url = QString("http://%1/query?pretty=true&db=%2&q=%3").arg(m_host).arg(db).arg(sql);
    qDebug() << url;

    return execSelect(url);
}
//执行查询语句: get
QString InfluxdbAPI::execSelect(const QString & url)
{
    QNetworkRequest req(url);
    req.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");

    QNetworkReply *reply = m_NetManager->get(req);

    QByteArray responseData;
    QEventLoop eventLoop;
    connect(m_NetManager, SIGNAL(finished(QNetworkReply*)), &eventLoop, SLOT(quit()));
    eventLoop.exec();       //block until finish
    responseData = reply->readAll();

    return responseData;
};
select * from measurementName where time >='2018-07-16T10:00:00Z' tz('Asia/Shanghai')

select time,fieldName from "measurementName" where time>= '2021-12-20T2:00:00Z' and time<='2021-12-20T3:00:00Z' tz('Asia/Shanghai')

SELECT * FROM "zhangsan" WHERE time > now() - 5m
SELECT * FROM Analog limit 10;

你可能感兴趣的:(时序/非时序数据库,qt,开发语言)