KOpeningHours

interval.cpp
1 /*
2  SPDX-FileCopyrightText: 2020 Volker Krause <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 
7 #include "interval.h"
8 
9 namespace KOpeningHours {
10 class IntervalPrivate : public QSharedData {
11 public:
13  QDateTime end;
14  Interval::State state = Interval::Invalid;
15  bool openEndTime = false;
16  QString comment;
17  QDateTime estimatedEnd;
18 };
19 }
20 
21 using namespace KOpeningHours;
22 
23 Interval::Interval()
24  : d(new IntervalPrivate)
25 {
26 }
27 
28 Interval::Interval(const Interval&) = default;
29 Interval::Interval(Interval&&) = default;
30 Interval::~Interval() = default;
31 Interval& Interval::operator=(const Interval&) = default;
32 Interval& Interval::operator=(Interval&&) = default;
33 
34 bool 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 
49 bool 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 
60 bool Interval::isValid() const
61 {
62  return d->state != Invalid;
63 }
64 
65 QDateTime Interval::begin() const
66 {
67  return d->begin;
68 }
69 
70 void Interval::setBegin(const QDateTime &begin)
71 {
72  d.detach();
73  d->begin = begin;
74 }
75 
76 bool Interval::hasOpenBegin() const
77 {
78  return !d->begin.isValid();
79 }
80 
81 QDateTime Interval::end() const
82 {
83  return d->end;
84 }
85 
86 void Interval::setEnd(const QDateTime &end)
87 {
88  d.detach();
89  d->end = end;
90 }
91 
92 bool Interval::hasOpenEnd() const
93 {
94  return !d->end.isValid();
95 }
96 
97 bool Interval::hasOpenEndTime() const
98 {
99  return d->openEndTime;
100 }
101 
102 void Interval::setOpenEndTime(bool openEndTime)
103 {
104  d.detach();
105  d->openEndTime = openEndTime;
106 }
107 
108 bool 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 
116 Interval::State Interval::state() const
117 {
118  return d->state;
119 }
120 
121 void Interval::setState(Interval::State state)
122 {
123  d.detach();
124  d->state = state;
125 }
126 
127 QString Interval::comment() const
128 {
129  return d->comment;
130 }
131 
132 void Interval::setComment(const QString &comment)
133 {
134  d.detach();
135  d->comment = comment;
136 }
137 
138 QDateTime Interval::estimatedEnd() const
139 {
140  if (d->openEndTime && d->estimatedEnd.isValid()) {
141  return d->estimatedEnd;
142  }
143  return end();
144 }
145 
146 void Interval::setEstimatedEnd(const QDateTime& estimatedEnd)
147 {
148  d.detach();
149  d->estimatedEnd = estimatedEnd;
150 }
151 
152 QDebug operator<<(QDebug debug, const Interval &interval)
153 {
154  QDebugStateSaver saver(debug);
155  debug.nospace().noquote() << '[' << interval.begin().toString(QStringLiteral("yyyy-MM-ddThh:mm")) << " - " << interval.end().toString(QStringLiteral("yyyy-MM-ddThh:mm")) << ' ' << interval.state() << " (\"" << interval.comment() << "\")]";
156  return debug;
157 }
State
Opening state during a time interval.
Definition: interval.h:90
KCALENDARCORE_EXPORT QDataStream & operator<<(QDataStream &out, const KCalendarCore::Alarm::Ptr &)
QDebug & nospace()
const QList< QKeySequence > & begin()
QDebug & noquote()
bool contains(const QDateTime &dt) const
Check if this interval contains dt.
Definition: interval.cpp:108
bool operator<(const Interval &other) const
Check whether this interval starts before other.
Definition: interval.cpp:34
bool isValid() const
Default constructed empty/invalid interval.
Definition: interval.cpp:60
QString toString(Qt::DateFormat format) const const
OSM opening hours parsing and evaluation.
Definition: display.h:14
const QList< QKeySequence > & end()
bool intersects(const Interval &other) const
Checks whether this interval overlaps with other.
Definition: interval.cpp:49
A time interval for which an opening hours expression has been evaluated.
Definition: interval.h:24
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Tue Aug 9 2022 04:07:20 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.