DateTime2BigIntFilter.h 2.87 KB
Newer Older
Stefan Gerlach's avatar
Stefan Gerlach committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
/***************************************************************************
    File                 : DateTime2BigIntFilter.h
    Project              : AbstractColumn
    --------------------------------------------------------------------
    Copyright            : (C) 2020 Stefan Gerlach (stefan.gerlach@uni.kn)
    Description          : Conversion filter QDateTime -> bigint (using Julian day).

 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *  This program is free software; you can redistribute it and/or modify   *
 *  it under the terms of the GNU General Public License as published by   *
 *  the Free Software Foundation; either version 2 of the License, or      *
 *  (at your option) any later version.                                    *
 *                                                                         *
 *  This program is distributed in the hope that it will be useful,        *
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of         *
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
 *  GNU General Public License for more details.                           *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the Free Software           *
 *   Foundation, Inc., 51 Franklin Street, Fifth Floor,                    *
 *   Boston, MA  02110-1301  USA                                           *
 *                                                                         *
 ***************************************************************************/
#ifndef DATE_TIME2BIGINT_FILTER_H
#define DATE_TIME2BIGINT_FILTER_H

#include "../AbstractSimpleFilter.h"
#include <QDateTime>

//! Conversion filter QDateTime -> bigint (using Julian day).
class DateTime2BigIntFilter : public AbstractSimpleFilter {
	Q_OBJECT

public:
	qint64 bigIntAt(int row) const override {
		//DEBUG("bigIntAt()");
		if (!m_inputs.value(0)) return 0;
		QDateTime inputDate = m_inputs.value(0)->dateTimeAt(row);
		if (!inputDate.isValid()) return 0;
44
#if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0))
45 46
		QDateTime start(QDate(1900, 1, 1).startOfDay());
#else
Stefan Gerlach's avatar
Stefan Gerlach committed
47
		QDateTime start(QDate(1900, 1, 1));
48
#endif
Stefan Gerlach's avatar
Stefan Gerlach committed
49 50 51 52
		return qint64(start.daysTo(inputDate));
	}

	//! Return the data type of the column
53
	AbstractColumn::ColumnMode columnMode() const override { return AbstractColumn::ColumnMode::BigInt; }
Stefan Gerlach's avatar
Stefan Gerlach committed
54 55 56 57

protected:
	//! Using typed ports: only DateTime inputs are accepted.
	bool inputAcceptable(int, const AbstractColumn *source) override {
58
		return source->columnMode() == AbstractColumn::ColumnMode::DateTime;
Stefan Gerlach's avatar
Stefan Gerlach committed
59 60 61 62 63
	}
};

#endif // ifndef DATE_TIME2BIGINT_FILTER_H