KOpeningHours

interval.cpp
1/*
2 SPDX-FileCopyrightText: 2020 Volker Krause <vkrause@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "interval.h"
8
9namespace KOpeningHours {
10class IntervalPrivate : public QSharedData {
11public:
12 QDateTime begin;
13 QDateTime end;
14 Interval::State state = Interval::Invalid;
15 bool openEndTime = false;
16 QString comment;
17 QDateTime estimatedEnd;
18};
19}
20
21using namespace KOpeningHours;
22
23Interval::Interval()
24 : d(new IntervalPrivate)
25{
26}
27
28Interval::Interval(const Interval&) = default;
29Interval::Interval(Interval&&) = default;
30Interval::~Interval() = default;
31Interval& Interval::operator=(const Interval&) = default;
32Interval& Interval::operator=(Interval&&) = default;
33
34bool Interval::operator<(const Interval &other) const
35{
36 if (hasOpenBegin() && !other.hasOpenBegin()) {
37 return true;
38 }
39 if (other.hasOpenBegin() && !hasOpenBegin()) {
40 return false;
41 }
42
43 if (d->begin == other.d->begin) {
44 return d->end < other.d->end;
45 }
46 return d->begin < other.d->begin;
47}
48
49bool Interval::intersects(const Interval &other) const
50{
51 if (d->end.isValid() && other.d->begin.isValid() && d->end <= other.d->begin) {
52 return false;
53 }
54 if (other.d->end.isValid() && d->begin.isValid() && other.d->end <= d->begin) {
55 return false;
56 }
57 return true;
58}
59
61{
62 return d->state != Invalid;
63}
64
65QDateTime Interval::begin() const
66{
67 return d->begin;
68}
69
70void Interval::setBegin(const QDateTime &begin)
71{
72 d.detach();
73 d->begin = begin;
74}
75
76bool Interval::hasOpenBegin() const
77{
78 return !d->begin.isValid();
79}
80
81QDateTime Interval::end() const
82{
83 return d->end;
84}
85
86void Interval::setEnd(const QDateTime &end)
87{
88 d.detach();
89 d->end = end;
90}
91
92bool Interval::hasOpenEnd() const
93{
94 return !d->end.isValid();
95}
96
97bool Interval::hasOpenEndTime() const
98{
99 return d->openEndTime;
100}
101
102void Interval::setOpenEndTime(bool openEndTime)
103{
104 d.detach();
105 d->openEndTime = openEndTime;
106}
107
108bool Interval::contains(const QDateTime &dt) const
109{
110 if (d->openEndTime && d->begin.isValid() && d->begin == d->end) {
111 return dt == d->begin;
112 }
113 return (d->begin.isValid() ? d->begin <= dt : true) && (d->end.isValid() ? dt < d->end : true);
114}
115
116Interval::State Interval::state() const
117{
118 return d->state;
119}
120
121void Interval::setState(Interval::State state)
122{
123 d.detach();
124 d->state = state;
125}
126
127QString Interval::comment() const
128{
129 return d->comment;
130}
131
132void Interval::setComment(const QString &comment)
133{
134 d.detach();
135 d->comment = comment;
136}
137
138QDateTime Interval::estimatedEnd() const
139{
140 if (d->openEndTime && d->estimatedEnd.isValid()) {
141 return d->estimatedEnd;
142 }
143 return end();
144}
145
146void Interval::setEstimatedEnd(const QDateTime& estimatedEnd)
147{
148 d.detach();
149 d->estimatedEnd = estimatedEnd;
150}
151
152int Interval::dstOffset() const
153{
154 if (d->begin.isValid() && estimatedEnd().isValid()) {
155 return estimatedEnd().offsetFromUtc() - d->begin.offsetFromUtc();
156 }
157 return 0;
158}
159
160QDebug operator<<(QDebug debug, const Interval &interval)
161{
162 QDebugStateSaver saver(debug);
163 debug.nospace().noquote() << '[' << interval.begin().toString(QStringLiteral("yyyy-MM-ddThh:mm")) << " - " << interval.end().toString(QStringLiteral("yyyy-MM-ddThh:mm")) << ' ' << interval.state() << " (\"" << interval.comment() << "\")]";
164 return debug;
165}
166
167#include "moc_interval.cpp"
A time interval for which an opening hours expression has been evaluated.
Definition interval.h:25
bool isValid() const
Default constructed empty/invalid interval.
Definition interval.cpp:60
bool operator<(const Interval &other) const
Check whether this interval starts before other.
Definition interval.cpp:34
State
Opening state during a time interval.
Definition interval.h:97
bool contains(const QDateTime &dt) const
Check if this interval contains dt.
Definition interval.cpp:108
bool intersects(const Interval &other) const
Checks whether this interval overlaps with other.
Definition interval.cpp:49
OSM opening hours parsing and evaluation.
Definition display.h:16
int offsetFromUtc() const const
QString toString(QStringView format, QCalendar cal) const const
QDebug & noquote()
QDebug & nospace()
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Sep 27 2024 11:55:00 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.