20#include "KDbDateTime.h"
22#include <QRegularExpression>
24const int UNCACHED_YEAR = -1;
25const int INVALID_YEAR = -2;
29std::function<
QString(
const T&)> byteArrayToString()
34struct KDbDateTimeMetatypeInitializer {
35 KDbDateTimeMetatypeInitializer()
37 using namespace std::placeholders;
46 QMetaType::registerComparators<KDbYear>();
47 QMetaType::registerComparators<KDbDate>();
48 QMetaType::registerComparators<KDbTime>();
49 QMetaType::registerComparators<KDbDateTime>();
53KDbDateTimeMetatypeInitializer s_init;
56bool KDbYear::operator==(
const KDbYear &other)
const
61bool KDbYear::operator<(
const KDbYear &other)
const
68 return std::get<1>(intValue());
73 return m_sign == Sign::None && m_string.
isEmpty();
81 result = QByteArrayLiteral(
"+");
84 result = QByteArrayLiteral(
"-");
96 case KDbYear::Sign::None:
98 case KDbYear::Sign::Plus:
101 case KDbYear::Sign::Minus:
123 result = QByteArrayLiteral(
"<NULL_YEAR>");
125 result = signString() + m_string;
132 return std::get<0>(intValue());
139 std::tie(result, ok) = intValue();
152 const int length =
string.length();
156 }
else if (length > 4) {
157 if (sign == KDbYear::Sign::None) {
170 int result =
string.
toInt(&ok);
171 if (!ok || result < 0) {
177 if (sign != KDbYear::Sign::Plus) {
182 }
else if (sign == KDbYear::Sign::Minus) {
183 qDateYear = - result - 1;
196std::tuple<int, bool> KDbYear::intValue()
const
198 if (m_isoValue == UNCACHED_YEAR) {
199 const_cast<int&
>(m_isoValue) = intValueInternal(m_sign, m_string);
201 if (m_isoValue == INVALID_YEAR) {
202 return std::make_tuple(0,
false);
204 return std::make_tuple(m_sign == Sign::Minus ? -m_isoValue : m_isoValue,
true);
207bool KDbDate::operator==(
const KDbDate &other)
const
213bool KDbDate::operator<(
const KDbDate &other)
const
234int toInt(
const QByteArray &
string,
int min,
int max,
int minLength,
int maxLength)
236 if (
string.length() < minLength ||
string.length() > maxLength) {
241 const int result =
string.isEmpty() ? 0 :
string.toInt(&ok);
242 if (!ok || result < min || result > max) {
252 return toInt(m_monthString, 1, 12, 1, 2);
257 return toInt(m_dayString, 1, 31, 1, 2);
264 result = QByteArrayLiteral(
"<NULL_DATE>");
266 result = m_year.
toString() +
'-' + m_monthString +
'-' + m_dayString;
282bool KDbTime::operator==(
const KDbTime &other)
const
286 && m_period == other.
period();
289bool KDbTime::operator<(
const KDbTime &other)
const
309 const int ms = msec();
314 return { h, m, s, ms };
317 QStringLiteral(
"%1:%2:%3.%4 %5")
323 QStringLiteral(
"h:m:s.z AP"));
330 return toInt(m_hourString, 0, 23, 1, 2);
333 return toInt(m_hourString, 1, 12, 1, 2);
340 return toInt(m_minuteString, 0, 59, 1, 2);
345 return toInt(m_secondString, 0, 59, 0, 2);
348int KDbTime::msec()
const
350 return toInt(m_msecString, 0, 999, 0, 3);
367 result = QByteArrayLiteral(
"<NULL_TIME>");
368 }
else if (m_msecString.
isEmpty()) {
369 if (m_secondString.
isEmpty()) {
370 result = m_hourString +
':' + m_minuteString;
372 result = m_hourString +
':' + m_minuteString +
':' + m_secondString;
375 result = m_hourString +
':' + m_minuteString +
':' + m_secondString +
'.' + m_msecString;
401bool KDbDateTime::operator==(
const KDbDateTime &other)
const
406bool KDbDateTime::operator<(
const KDbDateTime &other)
const
430 result = QByteArrayLiteral(
"<NULL_DATETIME>");
Generic date/time constant.
KDbDate date() const
Returns the date part of the date/time.
bool isNull() const
Returns true if the date/time is null.
QByteArray toString() const
Returns the date/time value converted to string even if it is invalid.
QDateTime toQDateTime() const
Returns the date/time converted to QDateTime value.
KDbTime time() const
Returns the time part of the date/time.
bool isValid() const
Returns true if the date/time is valid.
int month() const
Returns the month part of the date converted to integer.
KDbYear year() const
Returns the year part of the date.
bool isValid() const
Returns true if the date is valid.
int day() const
Returns the day part of the date converted to integer.
QByteArray dayString() const
Returns the day part of the date.
bool isNull() const
Returns true if the date is null.
QByteArray monthString() const
Returns the month part of the date.
QByteArray toString() const
Returns the date value converted to string even if it is invalid.
QDate toQDate() const
Returns the date converted to QDate value.
Period period() const
Specifies hour period.
QByteArray msecString() const
Returns the milliseconds part of the date.
QByteArray minuteString() const
Returns the minute part of the date.
QByteArray secondString() const
Returns the second part of the date.
int minute() const
Returns the minute part of the time converted to integer.
int hour() const
Returns the hour part of the time converted to integer.
@ Pm
PM, after noon, before midnight.
QTime toQTime() const
Returns the time value converted to QTime type.
QByteArray toString() const
Returns the time value converted to string even if it is invalid.
int second() const
Returns the second part of the time converted to integer.
QByteArray hourString() const
Returns the hour part of the date.
bool isValid() const
Returns true if the time is valid.
bool isNull() const
Returns true if the time is null.
Generic year constant based on extended ISO 8601 specification.
QByteArray toString() const
Returns entire year value (with sign) converted to string even if it is invalid.
Sign sign() const
Returns the sign which is used to annotate year.
Sign
Specifies sign which is used to annotate year.
QByteArray yearString() const
Returns the string representation of year value even if it is invalid.
int toIsoValue() const
Returns the integer year value as defined by extended ISO 8601.
bool isValid() const
Returns true if the year is valid.
bool isNull() const
Returns true if the year is null.
int toQDateValue() const
Returns the integer year value as defined by the QDate API.
QDebug operator<<(QDebug dbg, const DcrawInfoContainer &c)
bool isValid(QStringView ifopt)
bool isEmpty() const const
int toInt(bool *ok, int base) const const
bool isValid(int year, int month, int day)
QString fromLatin1(QByteArrayView str)
QTime fromString(QStringView string, QStringView format)
bool isValid(int h, int m, int s, int ms)