File indexing completed on 2024-04-21 04:40:44

0001 /*
0002     SPDX-FileCopyrightText: 2020 Volker Krause <vkrause@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "interval.h"
0008 
0009 namespace KOpeningHours {
0010 class IntervalPrivate : public QSharedData {
0011 public:
0012     QDateTime begin;
0013     QDateTime end;
0014     Interval::State state = Interval::Invalid;
0015     bool openEndTime = false;
0016     QString comment;
0017     QDateTime estimatedEnd;
0018 };
0019 }
0020 
0021 using namespace KOpeningHours;
0022 
0023 Interval::Interval()
0024     : d(new IntervalPrivate)
0025 {
0026 }
0027 
0028 Interval::Interval(const Interval&) = default;
0029 Interval::Interval(Interval&&) = default;
0030 Interval::~Interval() = default;
0031 Interval& Interval::operator=(const Interval&) = default;
0032 Interval& Interval::operator=(Interval&&) = default;
0033 
0034 bool Interval::operator<(const Interval &other) const
0035 {
0036     if (hasOpenBegin() && !other.hasOpenBegin()) {
0037         return true;
0038     }
0039     if (other.hasOpenBegin() && !hasOpenBegin()) {
0040         return false;
0041     }
0042 
0043     if (d->begin == other.d->begin) {
0044         return d->end < other.d->end;
0045     }
0046     return d->begin < other.d->begin;
0047 }
0048 
0049 bool Interval::intersects(const Interval &other) const
0050 {
0051     if (d->end.isValid() && other.d->begin.isValid() && d->end <= other.d->begin) {
0052         return false;
0053     }
0054     if (other.d->end.isValid() && d->begin.isValid() && other.d->end <= d->begin) {
0055         return false;
0056     }
0057     return true;
0058 }
0059 
0060 bool Interval::isValid() const
0061 {
0062     return d->state != Invalid;
0063 }
0064 
0065 QDateTime Interval::begin() const
0066 {
0067     return d->begin;
0068 }
0069 
0070 void Interval::setBegin(const QDateTime &begin)
0071 {
0072     d.detach();
0073     d->begin = begin;
0074 }
0075 
0076 bool Interval::hasOpenBegin() const
0077 {
0078     return !d->begin.isValid();
0079 }
0080 
0081 QDateTime Interval::end() const
0082 {
0083     return d->end;
0084 }
0085 
0086 void Interval::setEnd(const QDateTime &end)
0087 {
0088     d.detach();
0089     d->end = end;
0090 }
0091 
0092 bool Interval::hasOpenEnd() const
0093 {
0094     return !d->end.isValid();
0095 }
0096 
0097 bool Interval::hasOpenEndTime() const
0098 {
0099     return d->openEndTime;
0100 }
0101 
0102 void Interval::setOpenEndTime(bool openEndTime)
0103 {
0104     d.detach();
0105     d->openEndTime = openEndTime;
0106 }
0107 
0108 bool Interval::contains(const QDateTime &dt) const
0109 {
0110     if (d->openEndTime && d->begin.isValid() && d->begin == d->end) {
0111         return dt == d->begin;
0112     }
0113     return (d->begin.isValid() ? d->begin <= dt : true) && (d->end.isValid() ? dt < d->end : true);
0114 }
0115 
0116 Interval::State Interval::state() const
0117 {
0118     return d->state;
0119 }
0120 
0121 void Interval::setState(Interval::State state)
0122 {
0123     d.detach();
0124     d->state = state;
0125 }
0126 
0127 QString Interval::comment() const
0128 {
0129     return d->comment;
0130 }
0131 
0132 void Interval::setComment(const QString &comment)
0133 {
0134     d.detach();
0135     d->comment = comment;
0136 }
0137 
0138 QDateTime Interval::estimatedEnd() const
0139 {
0140     if (d->openEndTime && d->estimatedEnd.isValid()) {
0141         return d->estimatedEnd;
0142     }
0143     return end();
0144 }
0145 
0146 void Interval::setEstimatedEnd(const QDateTime& estimatedEnd)
0147 {
0148     d.detach();
0149     d->estimatedEnd = estimatedEnd;
0150 }
0151 
0152 int Interval::dstOffset() const
0153 {
0154     if (d->begin.isValid() && estimatedEnd().isValid()) {
0155         return estimatedEnd().offsetFromUtc() - d->begin.offsetFromUtc();
0156     }
0157     return 0;
0158 }
0159 
0160 QDebug operator<<(QDebug debug, const Interval &interval)
0161 {
0162     QDebugStateSaver saver(debug);
0163     debug.nospace().noquote() << '[' << interval.begin().toString(QStringLiteral("yyyy-MM-ddThh:mm")) << " - " << interval.end().toString(QStringLiteral("yyyy-MM-ddThh:mm")) << ' ' << interval.state() << " (\"" << interval.comment() << "\")]";
0164     return debug;
0165 }
0166 
0167 #include "moc_interval.cpp"