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 "incidencebase.h"
21 #include "period.h"
22 #include "utils_p.h"
23 
24 #include <QHash>
25 #include <QTimeZone>
26 
27 using namespace KCalendarCore;
28 
29 //@cond PRIVATE
30 class Q_DECL_HIDDEN KCalendarCore::Period::Private
31 {
32 public:
33  Private()
34  : mHasDuration(false)
35  , mDailyDuration(false)
36  {
37  }
38  Private(const QDateTime &start, const QDateTime &end, bool hasDuration)
39  : mStart(start)
40  , mEnd(end)
41  , mHasDuration(hasDuration)
42  , mDailyDuration(false)
43  {
44  }
45  QDateTime mStart; // period starting date/time
46  QDateTime mEnd; // period ending date/time
47  bool mHasDuration = false; // does period have a duration?
48  bool mDailyDuration = false; // duration is defined as number of days, not seconds
49 };
50 //@endcond
51 
53  : d(new KCalendarCore::Period::Private())
54 {
55 }
56 
58  : d(new KCalendarCore::Period::Private(start, end, false))
59 {
60 }
61 
62 Period::Period(const QDateTime &start, const Duration &duration)
63  : d(new KCalendarCore::Period::Private(start, duration.end(start), true))
64 {
65  d->mDailyDuration = duration.isDaily();
66 }
67 
68 Period::Period(const Period &period)
69  : d(new KCalendarCore::Period::Private(*period.d))
70 {
71 }
72 
74 {
75  delete d;
76 }
77 
78 bool Period::operator<(const Period &other) const
79 {
80  return d->mStart < other.d->mStart;
81 }
82 
83 bool Period::operator==(const Period &other) const
84 {
85  return identical(d->mStart, other.d->mStart) && identical(d->mEnd, other.d->mEnd)
86  && d->mHasDuration == other.d->mHasDuration;
87 }
88 
90 {
91  // check for self assignment
92  if (&other == this) {
93  return *this;
94  }
95 
96  *d = *other.d;
97  return *this;
98 }
99 
100 bool Period::isValid() const
101 {
102  return d->mStart.isValid();
103 }
104 
106 {
107  return d->mStart;
108 }
109 
111 {
112  return d->mEnd;
113 }
114 
116 {
117  if (d->mHasDuration) {
118  return Duration(d->mStart, d->mEnd, d->mDailyDuration ? Duration::Days : Duration::Seconds);
119  } else {
120  return Duration(d->mStart, d->mEnd);
121  }
122 }
123 
125 {
126  return Duration(d->mStart, d->mEnd, type);
127 }
128 
130 {
131  return d->mHasDuration;
132 }
133 
134 void Period::shiftTimes(const QTimeZone &oldZone, const QTimeZone &newZone)
135 {
136  if (oldZone.isValid() && newZone.isValid() && oldZone != newZone) {
137  d->mStart = d->mStart.toTimeZone(oldZone);
138  d->mStart.setTimeZone(newZone);
139  d->mEnd = d->mEnd.toTimeZone(oldZone);
140  d->mEnd.setTimeZone(newZone);
141  }
142 }
143 
145 {
146  serializeQDateTimeAsKDateTime(stream, period.d->mStart);
147  serializeQDateTimeAsKDateTime(stream, period.d->mEnd);
148  return stream << period.d->mDailyDuration << period.d->mHasDuration;
149 }
150 
152 {
153  deserializeKDateTimeAsQDateTime(stream, period.d->mStart);
154  deserializeKDateTimeAsQDateTime(stream, period.d->mEnd);
155  stream >> period.d->mDailyDuration >> period.d->mHasDuration;
156  return stream;
157 }
158 
160 {
161  if (key.hasDuration()) {
162  return qHash(key.duration());
163  } else {
164  return qHash(key.start().toString() + key.end().toString());
165  }
166 }
Duration duration() const
Returns the duration of the period.
Definition: period.cpp:115
KCALENDARCORE_EXPORT QDataStream & operator<<(QDataStream &out, const KCalendarCore::Alarm::Ptr &)
Alarm serializer.
Definition: alarm.cpp:820
Represents a span of time measured in seconds or days.
Definition: duration.h:43
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:129
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
@ Seconds
duration is a number of seconds
Definition: duration.h:50
Namespace for all KCalendarCore types.
Definition: alarm.h:36
QDateTime end() const
Returns when this period ends.
Definition: period.cpp:110
bool isValid() const
Returns true if the Period is not empty.
Definition: period.cpp:100
KCALENDARCORE_EXPORT bool identical(QDateTime, QDateTime)
Compare two QDateTimes for extended equality.
KCALENDARCORE_EXPORT QDataStream & operator>>(QDataStream &in, const KCalendarCore::Alarm::Ptr &)
Alarm deserializer.
Definition: alarm.cpp:833
@ Days
duration is a number of days
Definition: duration.h:51
Q_SCRIPTABLE Q_NOREPLY void start()
bool isValid() const const
Period & operator=(const Period &other)
Sets this period equal to the other one.
Definition: period.cpp:89
bool operator==(const Period &other) const
Returns true if this period is equal to the other one.
Definition: period.cpp:83
~Period()
Destroys a period.
Definition: period.cpp:73
Type
The unit of time used to define the duration.
Definition: duration.h:49
Period()
Constructs a period without a duration.
Definition: period.cpp:52
KCALENDARCORE_EXPORT uint qHash(const KCalendarCore::Person &key)
Return a hash value for a Person argument.
Definition: person.cpp:147
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:78
QString toString(Qt::DateFormat format) const const
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:105
Represents a period of time.
const QList< QKeySequence > & end()
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:134
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Thu Sep 28 2023 03:53:12 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.