DateTime2DoubleFilter.h 2.99 KB
Newer Older
1 2
/***************************************************************************
    File                 : DateTime2DoubleFilter.h
3
    Project              : LabPlot
4
    --------------------------------------------------------------------
5 6
    Copyright            : (C) 2007 by Tilman Benkert (thzs@gmx.net)
    Copyright            : (C) 2007 by Knut Franke (knut.franke@gmx.de)
7
    Description          : Conversion filter QDateTime -> double (using Julian day).
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
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *  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_TIME2DOUBLE_FILTER_H
#define DATE_TIME2DOUBLE_FILTER_H

#include "../AbstractSimpleFilter.h"
#include <QDateTime>
Stefan Gerlach's avatar
Stefan Gerlach committed
34
#include <cmath>
35 36

//! Conversion filter QDateTime -> double (using Julian day).
Stefan Gerlach's avatar
Stefan Gerlach committed
37
class DateTime2DoubleFilter : public AbstractSimpleFilter {
38 39
	Q_OBJECT

Stefan Gerlach's avatar
Stefan Gerlach committed
40
public:
41
	double valueAt(int row) const override {
Stefan Gerlach's avatar
Stefan Gerlach committed
42
		if (!m_inputs.value(0)) return NAN;
43 44
		QDateTime inputDate = m_inputs.value(0)->dateTimeAt(row);
		if (!inputDate.isValid()) return NAN;
45
#if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0))
46 47
		QDateTime start(QDate(1900, 1, 1).startOfDay());
#else
48
		QDateTime start(QDate(1900, 1, 1));
49
#endif
50 51
		return double(start.daysTo(inputDate)) +
			double( -inputDate.time().msecsTo(QTime(0,0,0,0)) ) / 86400000.0;
Stefan Gerlach's avatar
Stefan Gerlach committed
52 53 54
	}

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

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

#endif // ifndef DATE_TIME2DOUBLE_FILTER_H