将时间表示为 double 类型的最佳方式是什么

Whats the best possible way to represent a time as a double type

我正在尝试使用一个名为 QCustomPlot 的库,并且在 X 轴上我希望能够将股市开放的用户时区设置为从 9:30AM 到 4:00PM但是在设置 x 轴的范围时我需要的参数必须是双倍的,将时间转换为我可以使用的格式的最佳方法是什么?提前致谢!

补充:还有什么简单的方法可以使y轴的范围根据标的股票的当前价格上下波动吗?

代码:

string symbol_std = symbol.toStdString();


    //Retrieves json format of data
    Json::Value chartData = IEX::stocks::chartYtd(symbol_std);

    //Size of json
    int n = chartData.size();

    //Stores x and y values
    QVector<double> time(n), average(n);


    QDateTime start = QDateTime(QDate(2020, 1, 1));
    start.setTimeSpec(Qt::UTC);
    double startTime = start.toTime_t();
    double binSize = 3600*24;
    time[0] = startTime;

    //Reads in data from json(historical data 1 day delayed)
    for(int i = 0; i < chartData.size(); i++)
    {
        time[i + 1] = startTime + 3600*(i+1);
        average[i] = (chartData[i]["close"].asDouble());

        if((average[i] == 0) && (time[i] != chartData.size() - 1))
        {
            average[i] = average[i-1];
        }


    }
    //Initializes graph
    ui->stockGraph->addGraph();
    ui->stockGraph->graph(0)->setData(time, average);

    // give the axes some labels:
    ui->stockGraph->xAxis->setLabel("Time");
    ui->stockGraph->yAxis->setLabel("Price");

    // set axes ranges, so we see all data:
    ui->stockGraph->xAxis->setRange(time[0], time[n]);
    ui->stockGraph->yAxis->setRange(210, 340);
    ui->stockGraph->replot();

图形视觉

what is the best way about getting the time to a format I can use?

您需要 QwtDateScaleEngine along with a QwtDateScaleDraw。这些会将您的 double 值解释为从纪元“1970-01-01T00:00:00 UTC”开始的时间,以毫秒为单位。

ui->stockGraph->setAxisScaleDraw(QwtPlot::xBottom, new QwtDateScaleDraw());
ui->stockGraph->setAxisScaleEngine(QwtPlot::xBottom, new QwtDateScaleEngine());