File indexing completed on 2024-11-24 04:42:19

0001 /*
0002  *  datetime.cpp  -  date/time with start-of-day time for date-only values
0003  *  This file is part of kalarmcalendar library, which provides access to KAlarm
0004  *  calendar data.
0005  *  Program:  kalarm
0006  *  SPDX-FileCopyrightText: 2003-2022 David Jarvie <djarvie@kde.org>
0007  *
0008  *  SPDX-License-Identifier: LGPL-2.0-or-later
0009  */
0010 
0011 #include "datetime.h"
0012 
0013 #include <QTimeZone>
0014 #include <QLocale>
0015 
0016 namespace KAlarmCal
0017 {
0018 
0019 class Q_DECL_HIDDEN DateTime::Private
0020 {
0021 public:
0022     Private() = default;
0023     Private(const QDate& d, const KADateTime::Spec& spec) : mDateTime(d, spec) {}
0024     Private(const QDate& d, const QTime& t, const KADateTime::Spec& spec)
0025         : mDateTime(d, t, spec) {}
0026     Private(const QDateTime& dt, const KADateTime::Spec& spec)
0027         : mDateTime(dt, spec) {}
0028     Private(const QDateTime& dt) : mDateTime(dt) {}
0029     Private(const KADateTime& dt) : mDateTime(dt) {}
0030 
0031     static QTime mStartOfDay;
0032     KADateTime mDateTime;
0033 };
0034 
0035 QTime DateTime::Private::mStartOfDay;
0036 
0037 DateTime::DateTime()
0038     : d(new Private)
0039 {
0040 }
0041 
0042 DateTime::DateTime(const QDate& d, const KADateTime::Spec& spec)
0043     : d(new Private(d, spec))
0044 {
0045 }
0046 
0047 DateTime::DateTime(const QDate& d, const QTime& t, const KADateTime::Spec& spec)
0048     : d(new Private(d, t, spec))
0049 {
0050 }
0051 
0052 DateTime::DateTime(const QDateTime& dt, const KADateTime::Spec& spec)
0053     : d(new Private(dt, spec))
0054 {
0055 }
0056 
0057 DateTime::DateTime(const QDateTime& dt)
0058     : d(new Private(dt))
0059 {
0060 }
0061 
0062 DateTime::DateTime(const KADateTime& dt)
0063     : d(new Private(dt))
0064 {
0065 }
0066 
0067 DateTime::DateTime(const DateTime& dt)
0068     : d(new Private(*dt.d))
0069 {
0070 }
0071 
0072 DateTime::~DateTime()
0073 {
0074     delete d;
0075 }
0076 
0077 DateTime& DateTime::operator=(const DateTime& dt)
0078 {
0079     if (&dt != this)
0080         *d = *dt.d;
0081     return *this;
0082 }
0083 
0084 DateTime& DateTime::operator=(const KADateTime& dt)
0085 {
0086     d->mDateTime = dt;
0087     return *this;
0088 }
0089 
0090 bool DateTime::isNull() const
0091 {
0092     return d->mDateTime.isNull();
0093 }
0094 
0095 bool DateTime::isValid() const
0096 {
0097     return d->mDateTime.isValid();
0098 }
0099 
0100 bool DateTime::isDateOnly() const
0101 {
0102     return d->mDateTime.isDateOnly();
0103 }
0104 
0105 void DateTime::setDateOnly(bool dateOnly)
0106 {
0107     d->mDateTime.setDateOnly(dateOnly);
0108 }
0109 
0110 QDate DateTime::date() const
0111 {
0112     return d->mDateTime.date();
0113 }
0114 
0115 void DateTime::setDate(const QDate& date)
0116 {
0117     d->mDateTime.setDate(date);
0118 }
0119 
0120 QDateTime DateTime::qDateTime() const
0121 {
0122     return d->mDateTime.qDateTime();
0123 }
0124 
0125 KADateTime DateTime::kDateTime() const
0126 {
0127     return d->mDateTime;
0128 }
0129 
0130 QTime DateTime::effectiveTime() const
0131 {
0132     return d->mDateTime.isDateOnly() ? d->mStartOfDay : d->mDateTime.time();
0133 }
0134 
0135 void DateTime::setTime(const QTime& t)
0136 {
0137     d->mDateTime.setTime(t);
0138 }
0139 
0140 QDateTime DateTime::effectiveDateTime() const
0141 {
0142     if (d->mDateTime.isDateOnly())
0143     {
0144         QDateTime dt = d->mDateTime.qDateTime();    // preserve Qt::UTC or Qt::LocalTime
0145         dt.setTime(d->mStartOfDay);
0146         return dt;
0147     }
0148     return d->mDateTime.qDateTime();
0149 }
0150 
0151 KADateTime DateTime::effectiveKDateTime() const
0152 {
0153     if (d->mDateTime.isDateOnly())
0154     {
0155         KADateTime dt = d->mDateTime;
0156         dt.setTime(d->mStartOfDay);
0157         return dt;
0158     }
0159     return d->mDateTime;
0160 }
0161 
0162 QDateTime DateTime::calendarDateTime() const
0163 {
0164     if (d->mDateTime.isDateOnly())
0165     {
0166         QDateTime dt = d->mDateTime.qDateTime();
0167         dt.setTime(QTime(0, 0));
0168         return dt;
0169     }
0170     return d->mDateTime.qDateTime();
0171 }
0172 
0173 KADateTime DateTime::calendarKDateTime() const
0174 {
0175     if (d->mDateTime.isDateOnly())
0176     {
0177         KADateTime dt = d->mDateTime;
0178         dt.setTime(QTime(0, 0));
0179         return dt;
0180     }
0181     return d->mDateTime;
0182 }
0183 
0184 QTimeZone DateTime::timeZone() const
0185 {
0186     return d->mDateTime.timeZone();
0187 }
0188 
0189 KADateTime::Spec DateTime::timeSpec() const
0190 {
0191     return d->mDateTime.timeSpec();
0192 }
0193 
0194 void DateTime::setTimeSpec(const KADateTime::Spec& spec)
0195 {
0196     d->mDateTime.setTimeSpec(spec);
0197 }
0198 
0199 KADateTime::SpecType DateTime::timeType() const
0200 {
0201     return d->mDateTime.timeType();
0202 }
0203 
0204 bool DateTime::isLocalZone() const
0205 {
0206     return d->mDateTime.isLocalZone();
0207 }
0208 
0209 bool DateTime::isUtc() const
0210 {
0211     return d->mDateTime.isUtc();
0212 }
0213 
0214 bool DateTime::isOffsetFromUtc() const
0215 {
0216     return d->mDateTime.isOffsetFromUtc();
0217 }
0218 
0219 int DateTime::utcOffset() const
0220 {
0221     return d->mDateTime.utcOffset();
0222 }
0223 
0224 bool DateTime::isSecondOccurrence() const
0225 {
0226     return d->mDateTime.isSecondOccurrence();
0227 }
0228 
0229 void DateTime::setSecondOccurrence(bool second)
0230 {
0231     d->mDateTime.setSecondOccurrence(second);
0232 }
0233 
0234 DateTime DateTime::toUtc() const
0235 {
0236     return DateTime(d->mDateTime.toUtc());
0237 }
0238 
0239 DateTime DateTime::toOffsetFromUtc() const
0240 {
0241     return DateTime(d->mDateTime.toOffsetFromUtc());
0242 }
0243 
0244 DateTime DateTime::toOffsetFromUtc(int utcOffset) const
0245 {
0246     return DateTime(d->mDateTime.toOffsetFromUtc(utcOffset));
0247 }
0248 
0249 DateTime DateTime::toLocalZone() const
0250 {
0251     return DateTime(d->mDateTime.toLocalZone());
0252 }
0253 
0254 DateTime DateTime::toZone(const QTimeZone& zone) const
0255 {
0256     return DateTime(d->mDateTime.toZone(zone));
0257 }
0258 
0259 DateTime DateTime::toTimeSpec(const KADateTime::Spec& spec) const
0260 {
0261     return DateTime(d->mDateTime.toTimeSpec(spec));
0262 }
0263 
0264 qint64 DateTime::toSecsSinceEpoch() const
0265 {
0266     return d->mDateTime.toSecsSinceEpoch();
0267 }
0268 
0269 void DateTime::setSecsSinceEpoch(qint64 secs)
0270 {
0271     d->mDateTime.setSecsSinceEpoch(secs);
0272 }
0273 
0274 DateTime DateTime::addSecs(qint64 n) const
0275 {
0276     return DateTime(d->mDateTime.addSecs(n));
0277 }
0278 
0279 DateTime DateTime::addMins(qint64 n) const
0280 {
0281     return DateTime(d->mDateTime.addSecs(n * 60));
0282 }
0283 
0284 DateTime DateTime::addDays(int n) const
0285 {
0286     return DateTime(d->mDateTime.addDays(n));
0287 }
0288 
0289 DateTime DateTime::addMonths(int n) const
0290 {
0291     return DateTime(d->mDateTime.addMonths(n));
0292 }
0293 
0294 DateTime DateTime::addYears(int n) const
0295 {
0296     return DateTime(d->mDateTime.addYears(n));
0297 }
0298 
0299 int DateTime::daysTo(const DateTime& dt) const
0300 {
0301     return d->mDateTime.daysTo(dt.d->mDateTime);
0302 }
0303 
0304 int DateTime::minsTo(const DateTime& dt) const
0305 {
0306     return d->mDateTime.secsTo(dt.d->mDateTime) / 60;
0307 }
0308 
0309 int DateTime::secsTo(const DateTime& dt) const
0310 {
0311     return d->mDateTime.secsTo(dt.d->mDateTime);
0312 }
0313 
0314 qint64 DateTime::secsTo_long(const DateTime& dt) const
0315 {
0316     return d->mDateTime.secsTo(dt.d->mDateTime);
0317 }
0318 
0319 QString DateTime::toString(KADateTime::TimeFormat f) const
0320 {
0321     return d->mDateTime.toString(f);
0322 }
0323 
0324 QString DateTime::toString(const QString& format) const
0325 {
0326     return d->mDateTime.toString(format);
0327 }
0328 
0329 QString DateTime::formatLocale(bool shortFormat) const
0330 {
0331     QLocale::FormatType format = shortFormat ? QLocale::ShortFormat : QLocale::LongFormat;
0332     return d->mDateTime.isDateOnly() ? QLocale().toString(d->mDateTime.date(), format)
0333                                      : QLocale().toString(d->mDateTime.qDateTime(), format);
0334 }
0335 
0336 void DateTime::setStartOfDay(const QTime& sod)
0337 {
0338     Private::mStartOfDay = sod;
0339 }
0340 
0341 KADateTime::Comparison DateTime::compare(const DateTime& other) const
0342 {
0343     return d->mDateTime.compare(other.d->mDateTime);
0344 }
0345 
0346 QTime DateTime::startOfDay()
0347 {
0348     return Private::mStartOfDay;
0349 }
0350 
0351 bool operator==(const DateTime& dt1, const DateTime& dt2)
0352 {
0353     return dt1.d->mDateTime == dt2.d->mDateTime;
0354 }
0355 
0356 bool operator==(const KADateTime& dt1, const DateTime& dt2)
0357 {
0358     return dt1 == dt2.d->mDateTime;
0359 }
0360 
0361 bool operator<(const DateTime& dt1, const DateTime& dt2)
0362 {
0363     if (dt1.d->mDateTime.isDateOnly()  &&  !dt2.d->mDateTime.isDateOnly())
0364     {
0365         KADateTime dt = dt1.d->mDateTime.addDays(1);
0366         dt.setTime(DateTime::Private::mStartOfDay);
0367         return dt <= dt2.d->mDateTime;
0368     }
0369     if (!dt1.d->mDateTime.isDateOnly()  &&  dt2.d->mDateTime.isDateOnly())
0370     {
0371         KADateTime dt = dt2.d->mDateTime;
0372         dt.setTime(DateTime::Private::mStartOfDay);
0373         return dt1.d->mDateTime < dt;
0374     }
0375     return dt1.d->mDateTime < dt2.d->mDateTime;
0376 }
0377 
0378 } // namespace KAlarmCal
0379 
0380 // vim: et sw=4: