Improve the QDate support
This commit is contained in:
@@ -77,7 +77,7 @@ QDateTime datetimeFromNumber(double num, bool is1904)
|
|||||||
if (!is1904 && num > 60)
|
if (!is1904 && num > 60)
|
||||||
num = num - 1;
|
num = num - 1;
|
||||||
|
|
||||||
qint64 msecs = static_cast<qint64>(num * 1000*60*60*24.0);
|
qint64 msecs = static_cast<qint64>(num * 1000*60*60*24.0 + 0.5);
|
||||||
QDateTime epoch(is1904 ? QDate(1904, 1, 1): QDate(1899, 12, 31), QTime(0,0));
|
QDateTime epoch(is1904 ? QDate(1904, 1, 1): QDate(1899, 12, 31), QTime(0,0));
|
||||||
|
|
||||||
return epoch.addMSecs(msecs);
|
return epoch.addMSecs(msecs);
|
||||||
|
|||||||
@@ -53,7 +53,7 @@ WorkbookPrivate::WorkbookPrivate(Workbook *q) :
|
|||||||
|
|
||||||
strings_to_numbers_enabled = false;
|
strings_to_numbers_enabled = false;
|
||||||
date1904 = false;
|
date1904 = false;
|
||||||
defaultDateFormat = QStringLiteral("yyyy-mm-ddThh:mm:ss");
|
defaultDateFormat = QStringLiteral("yyyy-mm-dd");
|
||||||
activesheet = 0;
|
activesheet = 0;
|
||||||
firstsheet = 0;
|
firstsheet = 0;
|
||||||
table_count = 0;
|
table_count = 0;
|
||||||
|
|||||||
@@ -49,6 +49,7 @@
|
|||||||
#include <QXmlStreamReader>
|
#include <QXmlStreamReader>
|
||||||
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
#include <math.h>
|
||||||
|
|
||||||
QT_BEGIN_NAMESPACE_XLSX
|
QT_BEGIN_NAMESPACE_XLSX
|
||||||
|
|
||||||
@@ -516,6 +517,8 @@ QVariant Worksheet::read(int row, int column) const
|
|||||||
QDateTime dt = cell->dateTime();
|
QDateTime dt = cell->dateTime();
|
||||||
if (val < 1)
|
if (val < 1)
|
||||||
return dt.time();
|
return dt.time();
|
||||||
|
if (fmod(val, 1.0) < 1.0/(1000*60*60*24)) //integer
|
||||||
|
return dt.date();
|
||||||
return dt;
|
return dt;
|
||||||
}
|
}
|
||||||
return cell->value();
|
return cell->value();
|
||||||
@@ -848,8 +851,8 @@ int Worksheet::writeDateTime(int row, int column, const QDateTime &dt, const For
|
|||||||
if (d->checkDimensions(row, column))
|
if (d->checkDimensions(row, column))
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
Format fmt = format;
|
Format fmt = format.isValid() ? format : d->cellFormat(row, column);
|
||||||
if (!fmt.isValid())
|
if (!fmt.isValid() || !fmt.isDateTimeFormat())
|
||||||
fmt.setNumberFormat(d->workbook->defaultDateFormat());
|
fmt.setNumberFormat(d->workbook->defaultDateFormat());
|
||||||
d->workbook->styles()->addXfFormat(fmt);
|
d->workbook->styles()->addXfFormat(fmt);
|
||||||
|
|
||||||
@@ -882,8 +885,8 @@ int Worksheet::writeTime(int row, int column, const QTime &t, const Format &form
|
|||||||
if (d->checkDimensions(row, column))
|
if (d->checkDimensions(row, column))
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
Format fmt = format;
|
Format fmt = format.isValid() ? format : d->cellFormat(row, column);
|
||||||
if (!fmt.isValid())
|
if (!fmt.isValid() || !fmt.isDateTimeFormat())
|
||||||
fmt.setNumberFormat(QStringLiteral("hh:mm:ss"));
|
fmt.setNumberFormat(QStringLiteral("hh:mm:ss"));
|
||||||
d->workbook->styles()->addXfFormat(fmt);
|
d->workbook->styles()->addXfFormat(fmt);
|
||||||
|
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ private Q_SLOTS:
|
|||||||
void testReadWriteBlank();
|
void testReadWriteBlank();
|
||||||
void testReadWriteFormula();
|
void testReadWriteFormula();
|
||||||
void testReadWriteDateTime();
|
void testReadWriteDateTime();
|
||||||
|
void testReadWriteDate();
|
||||||
void testReadWriteTime();
|
void testReadWriteTime();
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -193,7 +194,7 @@ void DocumentTest::testReadWriteDateTime()
|
|||||||
format3.setNumberFormat("dd/mm/yyyy");
|
format3.setNumberFormat("dd/mm/yyyy");
|
||||||
xlsx1.write("A3", dt, format3);
|
xlsx1.write("A3", dt, format3);
|
||||||
|
|
||||||
xlsx1.write("A4", "2013-12-14"); //Auto convert to QDateTime, by QVariant
|
xlsx1.write("A4", "2013-12-14T12:30"); //Auto convert to QDateTime, by QVariant
|
||||||
|
|
||||||
xlsx1.saveAs(&device);
|
xlsx1.saveAs(&device);
|
||||||
|
|
||||||
@@ -202,21 +203,72 @@ void DocumentTest::testReadWriteDateTime()
|
|||||||
QCOMPARE(xlsx2.cellAt("A1")->dataType(), Cell::Numeric);
|
QCOMPARE(xlsx2.cellAt("A1")->dataType(), Cell::Numeric);
|
||||||
QCOMPARE(xlsx2.cellAt("A1")->isDateTime(), true);
|
QCOMPARE(xlsx2.cellAt("A1")->isDateTime(), true);
|
||||||
QCOMPARE(xlsx2.cellAt("A1")->dateTime(), dt);
|
QCOMPARE(xlsx2.cellAt("A1")->dateTime(), dt);
|
||||||
|
QVERIFY(xlsx2.read("A1").userType() == QMetaType::QDateTime);
|
||||||
|
|
||||||
QCOMPARE(xlsx2.cellAt("A2")->dataType(), Cell::Numeric);
|
QCOMPARE(xlsx2.cellAt("A2")->dataType(), Cell::Numeric);
|
||||||
// QCOMPARE(xlsx2.cellAt("A2")->isDateTime(), true);
|
QCOMPARE(xlsx2.cellAt("A2")->isDateTime(), true);
|
||||||
// QCOMPARE(xlsx2.cellAt("A2")->dateTime(), dt);
|
QCOMPARE(xlsx2.cellAt("A2")->dateTime(), dt);
|
||||||
|
QVERIFY(xlsx2.read("A2").userType() == QMetaType::QDateTime);
|
||||||
|
|
||||||
QCOMPARE(xlsx2.cellAt("A3")->dataType(), Cell::Numeric);
|
QCOMPARE(xlsx2.cellAt("A3")->dataType(), Cell::Numeric);
|
||||||
QVERIFY(xlsx2.cellAt("A3")->format().isValid());
|
QVERIFY(xlsx2.cellAt("A3")->format().isValid());
|
||||||
qDebug()<<xlsx2.cellAt("A3")->format().numberFormat();
|
|
||||||
QCOMPARE(xlsx2.cellAt("A3")->isDateTime(), true);
|
QCOMPARE(xlsx2.cellAt("A3")->isDateTime(), true);
|
||||||
QCOMPARE(xlsx2.cellAt("A3")->dateTime(), dt);
|
QCOMPARE(xlsx2.cellAt("A3")->dateTime(), dt);
|
||||||
QCOMPARE(xlsx2.cellAt("A3")->format().numberFormat(), QString("dd/mm/yyyy"));
|
QCOMPARE(xlsx2.cellAt("A3")->format().numberFormat(), QString("dd/mm/yyyy"));
|
||||||
|
|
||||||
|
QCOMPARE(xlsx2.cellAt("A4")->dataType(), Cell::Numeric);
|
||||||
|
QCOMPARE(xlsx2.cellAt("A4")->isDateTime(), true);
|
||||||
|
QCOMPARE(xlsx2.cellAt("A4")->dateTime(), QDateTime(QDate(2013,12,14), QTime(12, 30)));
|
||||||
|
QVERIFY(xlsx2.read("A4").userType() == QMetaType::QDateTime);
|
||||||
|
}
|
||||||
|
|
||||||
|
void DocumentTest::testReadWriteDate()
|
||||||
|
{
|
||||||
|
QBuffer device;
|
||||||
|
device.open(QIODevice::WriteOnly);
|
||||||
|
|
||||||
|
Document xlsx1;
|
||||||
|
QDate d(2012, 11, 12);
|
||||||
|
|
||||||
|
xlsx1.write("A1", d);
|
||||||
|
|
||||||
|
Format format;
|
||||||
|
format.setFontColor(Qt::blue);
|
||||||
|
format.setBorderStyle(Format::BorderDashDotDot);
|
||||||
|
format.setFillPattern(Format::PatternSolid);
|
||||||
|
xlsx1.write("A2", d, format);
|
||||||
|
|
||||||
|
Format format3;
|
||||||
|
format3.setNumberFormat("dd/mm/yyyy");
|
||||||
|
xlsx1.write("A3", d, format3);
|
||||||
|
|
||||||
|
xlsx1.write("A4", "2013-12-14"); //Auto convert to QDateTime, by QVariant
|
||||||
|
|
||||||
|
xlsx1.saveAs(&device);
|
||||||
|
|
||||||
|
device.open(QIODevice::ReadOnly);
|
||||||
|
Document xlsx2(&device);
|
||||||
|
QCOMPARE(xlsx2.cellAt("A1")->dataType(), Cell::Numeric);
|
||||||
|
QCOMPARE(xlsx2.cellAt("A1")->isDateTime(), true);
|
||||||
|
QVERIFY(xlsx2.read("A1").userType() == QMetaType::QDate);
|
||||||
|
QCOMPARE(xlsx2.read("A1").toDate(), d);
|
||||||
|
|
||||||
|
QCOMPARE(xlsx2.cellAt("A2")->dataType(), Cell::Numeric);
|
||||||
|
QCOMPARE(xlsx2.cellAt("A2")->isDateTime(), true);
|
||||||
|
QVERIFY(xlsx2.read("A2").userType() == QMetaType::QDate);
|
||||||
|
|
||||||
|
QCOMPARE(xlsx2.cellAt("A3")->dataType(), Cell::Numeric);
|
||||||
|
QVERIFY(xlsx2.cellAt("A3")->format().isValid());
|
||||||
|
QCOMPARE(xlsx2.cellAt("A3")->isDateTime(), true);
|
||||||
|
QCOMPARE(xlsx2.cellAt("A3")->format().numberFormat(), QString("dd/mm/yyyy"));
|
||||||
|
QVERIFY(xlsx2.read("A3").userType() == QMetaType::QDate);
|
||||||
|
QCOMPARE(xlsx2.read("A3").toDate(), d);
|
||||||
|
|
||||||
QCOMPARE(xlsx2.cellAt("A4")->dataType(), Cell::Numeric);
|
QCOMPARE(xlsx2.cellAt("A4")->dataType(), Cell::Numeric);
|
||||||
QCOMPARE(xlsx2.cellAt("A4")->isDateTime(), true);
|
QCOMPARE(xlsx2.cellAt("A4")->isDateTime(), true);
|
||||||
QCOMPARE(xlsx2.cellAt("A4")->dateTime(), QDateTime(QDate(2013,12,14)));
|
QCOMPARE(xlsx2.cellAt("A4")->dateTime(), QDateTime(QDate(2013,12,14)));
|
||||||
|
QVERIFY(xlsx2.read("A4").userType() == QMetaType::QDate);
|
||||||
|
QCOMPARE(xlsx2.read("A4").toDate(), QDate(2013,12,14));
|
||||||
}
|
}
|
||||||
|
|
||||||
void DocumentTest::testReadWriteTime()
|
void DocumentTest::testReadWriteTime()
|
||||||
@@ -238,8 +290,8 @@ void DocumentTest::testReadWriteTime()
|
|||||||
|
|
||||||
QCOMPARE(xlsx2.cellAt("A2")->dataType(), Cell::Numeric);
|
QCOMPARE(xlsx2.cellAt("A2")->dataType(), Cell::Numeric);
|
||||||
QCOMPARE(xlsx2.cellAt("A2")->isDateTime(), true);
|
QCOMPARE(xlsx2.cellAt("A2")->isDateTime(), true);
|
||||||
qDebug()<<xlsx2.cellAt("A2")->value().toDouble();
|
QVERIFY(xlsx2.read("A2").userType() == QMetaType::QTime);
|
||||||
//QCOMPARE(xlsx2.read("A2").toTime(), QTime(1, 22)); //01:21:59.999 ???
|
QCOMPARE(xlsx2.read("A2").toTime(), QTime(1, 22));
|
||||||
}
|
}
|
||||||
|
|
||||||
QTEST_APPLESS_MAIN(DocumentTest)
|
QTEST_APPLESS_MAIN(DocumentTest)
|
||||||
|
|||||||
@@ -119,6 +119,7 @@ void UtilityTest::test_datetimeToNumber_data()
|
|||||||
|
|
||||||
//Note, for number 0, Excel2007 shown as 1900-1-0, which should be 1899-12-31
|
//Note, for number 0, Excel2007 shown as 1900-1-0, which should be 1899-12-31
|
||||||
QTest::newRow("0") << QDateTime(QDate(1899, 12, 31), QTime(0,0)) << false << 0.0;
|
QTest::newRow("0") << QDateTime(QDate(1899, 12, 31), QTime(0,0)) << false << 0.0;
|
||||||
|
QTest::newRow("0.0625") << QDateTime(QDate(1899, 12, 31), QTime(1,30)) << false << 0.0625;
|
||||||
QTest::newRow("1.25") << QDateTime(QDate(1900, 1, 1), QTime(6, 0)) << false << 1.25;
|
QTest::newRow("1.25") << QDateTime(QDate(1900, 1, 1), QTime(6, 0)) << false << 1.25;
|
||||||
QTest::newRow("59") << QDateTime(QDate(1900, 2, 28), QTime(0, 0)) << false << 59.0;
|
QTest::newRow("59") << QDateTime(QDate(1900, 2, 28), QTime(0, 0)) << false << 59.0;
|
||||||
QTest::newRow("61") << QDateTime(QDate(1900, 3, 1), QTime(0, 0)) << false << 61.0;
|
QTest::newRow("61") << QDateTime(QDate(1900, 3, 1), QTime(0, 0)) << false << 61.0;
|
||||||
@@ -162,6 +163,7 @@ void UtilityTest::test_datetimeFromNumber_data()
|
|||||||
QTest::addColumn<double>("num");
|
QTest::addColumn<double>("num");
|
||||||
|
|
||||||
QTest::newRow("0") << QDateTime(QDate(1899, 12, 31), QTime(0,0)) << false << 0.0;
|
QTest::newRow("0") << QDateTime(QDate(1899, 12, 31), QTime(0,0)) << false << 0.0;
|
||||||
|
QTest::newRow("0.0625") << QDateTime(QDate(1899, 12, 31), QTime(1,30)) << false << 0.0625;
|
||||||
QTest::newRow("1.25") << QDateTime(QDate(1900, 1, 1), QTime(6, 0)) << false << 1.25;
|
QTest::newRow("1.25") << QDateTime(QDate(1900, 1, 1), QTime(6, 0)) << false << 1.25;
|
||||||
QTest::newRow("59") << QDateTime(QDate(1900, 2, 28), QTime(0,0)) << false << 59.0;
|
QTest::newRow("59") << QDateTime(QDate(1900, 2, 28), QTime(0,0)) << false << 59.0;
|
||||||
QTest::newRow("61") << QDateTime(QDate(1900, 3, 1), QTime(0,0)) << false << 61.0;
|
QTest::newRow("61") << QDateTime(QDate(1900, 3, 1), QTime(0,0)) << false << 61.0;
|
||||||
|
|||||||
Reference in New Issue
Block a user