KCalendarCore

period.cpp
Go to the documentation of this file.
1 /*
2  This file is part of the kcalcore library.
3 
4  SPDX-FileCopyrightText: 2001 Cornelius Schumacher <[email protected]>
5  SPDX-FileCopyrightText: 2007 David Jarvie <[email protected]>
6 
7  SPDX-License-Identifier: LGPL-2.0-or-later
8 */
9 /**
10  @file
11  This file is part of the API for handling calendar data and
12  defines the Period class.
13 
14  @brief
15  Represents a period of time.
16 
17  @author Cornelius Schumacher <[email protected]>
18 */
19 
20 #include "period.h"
21 #include "utils_p.h"
22 
23 #include <QHash>
24 #include <QTimeZone>
25 
26 using namespace KCalendarCore;
27 
28 //@cond PRIVATE
29 class Q_DECL_HIDDEN KCalendarCore::Period::Private
30 {
31 public:
32  Private()
33  : mHasDuration(false)
34  , mDailyDuration(false)
35  {
36  }
37  Private(const QDateTime &start, const QDateTime &end, bool hasDuration)
38  : mStart(start)
39  , mEnd(end)
40  , mHasDuration(hasDuration)
41  , mDailyDuration(false)
42  {
43  }
44  QDateTime mStart; // period starting date/time
45  QDateTime mEnd; // period ending date/time
46  bool mHasDuration = false; // does period have a duration?
47  bool mDailyDuration = false; // duration is defined as number of days, not seconds
48 };
49 //@endcond
50 
52  : d(new KCalendarCore::Period::Private())
53 {
54 }
55 
57  : d(new KCalendarCore::Period::Private(start, end, false))
58 {
59 }
60 
62  : d(new KCalendarCore::Period::Private(start, duration.end(start), true))
63 {
64  d->mDailyDuration = duration.isDaily();
65 }
66 
67 Period::Period(const Period &period)
68  : d(new KCalendarCore::Period::Private(*period.d))
69 {
70 }
71 
73 {
74  delete d;
75 }
76 
77 bool Period::operator<(const Period &other) const
78 {
79  return d->mStart < other.d->mStart;
80 }
81 
82 bool Period::operator==(const Period &other) const
83 {
84  return ((d->mStart == other.d->mStart) || (!d->mStart.isValid() && !other.d->mStart.isValid()))
85  && ((d->mEnd == other.d->mEnd) || (!d->mEnd.isValid() && !other.d->mEnd.isValid())) && d->mHasDuration == other.d->mHasDuration;
86 }
87 
89 {
90  // check for self assignment
91  if (&other == this) {
92  return *this;
93  }
94 
95  *d = *other.d;
96  return *this;
97 }
98 
99 bool Period::isValid() const
100 {
101  return d->mStart.isValid();
102 }
103 
105 {
106  return d->mStart;
107 }
108 
110 {
111  return d->mEnd;
112 }
113 
115 {
116  if (d->mHasDuration) {
117  return Duration(d->mStart, d->mEnd, d->mDailyDuration ? Duration::Days : Duration::Seconds);
118  } else {
119  return Duration(d->mStart, d->mEnd);
120  }
121 }
122 
124 {
125  return Duration(d->mStart, d->mEnd, type);
126 }
127 
129 {
130  return d->mHasDuration;
131 }
132 
133 void Period::shiftTimes(const QTimeZone &oldZone, const QTimeZone &newZone)
134 {
135  if (oldZone.isValid() && newZone.isValid() && oldZone != newZone) {
136  d->mStart = d->mStart.toTimeZone(oldZone);
137  d->mStart.setTimeZone(newZone);
138  d->mEnd = d->mEnd.toTimeZone(oldZone);
139  d->mEnd.setTimeZone(newZone);
140  }
141 }
142 
144 {
145  serializeQDateTimeAsKDateTime(stream, period.d->mStart);
146  serializeQDateTimeAsKDateTime(stream, period.d->mEnd);
147  return stream << period.d->mDailyDuration << period.d->mHasDuration;
148 }
149 
151 {
152  deserializeKDateTimeAsQDateTime(stream, period.d->mStart);
153  deserializeKDateTimeAsQDateTime(stream, period.d->mEnd);
154  stream >> period.d->mDailyDuration >> period.d->mHasDuration;
155  return stream;
156 }
157 
159 {
160  if (key.hasDuration()) {
161  return qHash(key.duration());
162  } else {
163  return qHash(key.start().toString() + key.end().toString());
164  }
165 }
bool hasDuration() const
Returns true if this period has a set duration, false if it just has a start and an end...
Definition: period.cpp:128
KCALENDARCORE_EXPORT QDataStream & operator<<(QDataStream &out, const KCalendarCore::Alarm::Ptr &)
Alarm serializer.
Definition: alarm.cpp:820
This file is part of the API for handling calendar data and defines the Period class.
QString toString(Qt::DateFormat format) const const
QDateTime end() const
Returns when this period ends.
Definition: period.cpp:109
void shiftTimes(const QTimeZone &oldZone, const QTimeZone &newZone)
Shift the times of the period so that they appear at the same clock time as before but in a new time ...
Definition: period.cpp:133
bool operator<(const Period &other) const
Returns true if the start of this period is earlier than the start of the other one.
Definition: period.cpp:77
bool operator==(const Period &other) const
Returns true if this period is equal to the other one.
Definition: period.cpp:82
KCALENDARCORE_EXPORT QDataStream & operator>>(QDataStream &in, const KCalendarCore::Alarm::Ptr &)
Alarm deserializer.
Definition: alarm.cpp:833
Duration duration() const
Returns the duration of the period.
Definition: period.cpp:114
Represents a span of time measured in seconds or days.
Definition: duration.h:43
The period can be defined by either a start time and an end time or by a start time and a duration...
Definition: period.h:37
bool isDaily() const
Returns whether the duration is specified in terms of days rather than seconds.
Definition: duration.cpp:181
QDateTime start() const
Returns when this period starts.
Definition: period.cpp:104
Period & operator=(const Period &other)
Sets this period equal to the other one.
Definition: period.cpp:88
bool isValid() const
Returns true if the Period is not empty.
Definition: period.cpp:99
Period()
Constructs a period without a duration.
Definition: period.cpp:51
duration is a number of seconds
Definition: duration.h:50
bool isValid() const const
duration is a number of days
Definition: duration.h:51
Type
The unit of time used to define the duration.
Definition: duration.h:49
~Period()
Destroys a period.
Definition: period.cpp:72
KCALENDARCORE_EXPORT uint qHash(const KCalendarCore::Person &key)
Return a hash value for a Person argument.
Definition: person.cpp:147
Namespace for all KCalendarCore types.
Definition: alarm.h:36
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Sat Sep 18 2021 22:51:44 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.