diff --git a/src/blackmisc/timestampbased.cpp b/src/blackmisc/timestampbased.cpp index c19956662..ed59351b0 100644 --- a/src/blackmisc/timestampbased.cpp +++ b/src/blackmisc/timestampbased.cpp @@ -12,7 +12,7 @@ namespace BlackMisc { - ITimestampBased::ITimestampBased() + ITimestampBased::ITimestampBased() : m_timestampMSecsSinceEpoch(QDateTime::currentMSecsSinceEpoch()) { } ITimestampBased::ITimestampBased(qint64 msSincePoch) : m_timestampMSecsSinceEpoch(msSincePoch) @@ -26,6 +26,39 @@ namespace BlackMisc return QDateTime::fromMSecsSinceEpoch(this->m_timestampMSecsSinceEpoch, Qt::UTC); } + void ITimestampBased::setByYearMonthDayHourMinute(const QString &yyyyMMddhhmmsszzz) + { + // yyyy MM dd hh mm ss zzz + // 0123 45 67 89 01 23 456 + // 1234 56 78 90 12 34 567 + + QString s(yyyyMMddhhmmsszzz); + s.remove(':').remove(' ').remove('-').remove('.'); // plain vanilla string + int year(s.left(4).toInt()); + int month(s.mid(4, 2).toInt()); + int day(s.mid(6, 2).toInt()); + QDate date; + date.setDate(year, month, day); + QDateTime dt; + dt.setOffsetFromUtc(0); + dt.setDate(date); + if (s.length() < 12) + { + this->setUtcTimestamp(dt); + return; + } + + QTime t; + int hour(s.mid(8, 2).toInt()); + int minute(s.mid(10, 2).toInt()); + int second(s.length() < 14 ? 0 : s.mid(12, 2).toInt()); + int ms(s.length() < 17 ? 0 : s.right(3).toInt()); + + t.setHMS(hour, minute, second, ms); + dt.setTime(t); + this->setUtcTimestamp(dt); + } + void ITimestampBased::setUtcTimestamp(const QDateTime ×tamp) { this->m_timestampMSecsSinceEpoch = timestamp.toMSecsSinceEpoch(); diff --git a/src/blackmisc/timestampbased.h b/src/blackmisc/timestampbased.h index ad9da9cfe..974940b3f 100644 --- a/src/blackmisc/timestampbased.h +++ b/src/blackmisc/timestampbased.h @@ -44,6 +44,9 @@ namespace BlackMisc //! Timestamp as ms value void setMSecsSinceEpoch(qint64 mSecsSinceEpoch) { m_timestampMSecsSinceEpoch = mSecsSinceEpoch; } + //! Set by value such as "20141003231045" + void setByYearMonthDayHourMinute(const QString &yyyyMMddhhmmss); + //! Set timestamp void setUtcTimestamp(const QDateTime ×tamp); @@ -83,10 +86,10 @@ namespace BlackMisc //! As hh:mm QString getFormattedUtcTimestampHm() const; - //! As YYYY mm dd hh ss + //! As yyyy MM dd HH mm ss QString getFormattedUtcTimestampYmdhms() const; - //! As YYYY mm dd hh ss.zzz + //! As yyyy MM dd HH mm ss.zzz QString getFormattedUtcTimestampYmdhmsz() const; //! Can given index be handled @@ -108,7 +111,7 @@ namespace BlackMisc //! \copydoc CValueObject::setPropertyByIndex void setPropertyByIndex(const CVariant &variant, const BlackMisc::CPropertyIndex &index); - qint64 m_timestampMSecsSinceEpoch = QDateTime::currentMSecsSinceEpoch(); //!< timestamp value + qint64 m_timestampMSecsSinceEpoch; //!< timestamp value }; } // namespace