File indexing completed on 2024-06-16 03:42:35

0001 /*
0002     File                 : Double2DateTimeFilter.h
0003     Project              : AbstractColumn
0004     Description          : Conversion filter double -> QDateTime, interpreting
0005     the input numbers as (fractional) Julian days.
0006     --------------------------------------------------------------------
0007     SPDX-FileCopyrightText: 2007 Knut Franke <knut.franke*gmx.de (use @ for *)>
0008     SPDX-FileCopyrightText: 2007 Tilman Benkert <thzs@gmx.net>
0009     SPDX-License-Identifier: GPL-2.0-or-later
0010 */
0011 
0012 #ifndef DOUBLE2DATE_TIME_FILTER_H
0013 #define DOUBLE2DATE_TIME_FILTER_H
0014 
0015 #include "../AbstractSimpleFilter.h"
0016 #include <QDateTime>
0017 #include <cmath>
0018 
0019 //! Conversion filter double -> QDateTime, interpreting the input numbers as (fractional) Julian days.
0020 class Double2DateTimeFilter : public AbstractSimpleFilter {
0021     Q_OBJECT
0022 
0023 public:
0024     QDate dateAt(int row) const override {
0025         if (!m_inputs.value(0))
0026             return QDate();
0027         double inputValue = m_inputs.value(0)->valueAt(row);
0028         if (std::isnan(inputValue))
0029             return QDate();
0030         // QDEBUG(" convert " << inputValue << " to " << QDate(1900, 1, int(inputValue)));
0031         return QDate(1900, 1, 1 + int(inputValue));
0032     }
0033     QTime timeAt(int row) const override {
0034         if (!m_inputs.value(0))
0035             return QTime();
0036         double inputValue = m_inputs.value(0)->valueAt(row);
0037         if (std::isnan(inputValue))
0038             return QTime();
0039         // we only want the digits behind the dot and convert them from fraction of day to milliseconds
0040         // QDEBUG(" convert " << inputValue << " to " << QTime(0,0,0,0).addMSecs(int( (inputValue - int(inputValue)) * 86400000.0 )));
0041         return QTime(0, 0, 0, 0).addMSecs(int((inputValue - int(inputValue)) * 86400000.0));
0042     }
0043     QDateTime dateTimeAt(int row) const override {
0044         return QDateTime(dateAt(row), timeAt(row));
0045     }
0046 
0047     //! Return the data type of the column
0048     AbstractColumn::ColumnMode columnMode() const override {
0049         return AbstractColumn::ColumnMode::DateTime;
0050     }
0051 
0052 protected:
0053     //! Using typed ports: only double inputs are accepted.
0054     bool inputAcceptable(int, const AbstractColumn* source) override {
0055         return source->columnMode() == AbstractColumn::ColumnMode::Double;
0056     }
0057 };
0058 
0059 #endif // ifndef DOUBLE2DATE_TIME_FILTER_H