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 
100 {
101  return d->mStart;
102 }
103 
105 {
106  return d->mEnd;
107 }
108 
110 {
111  if (d->mHasDuration) {
112  return Duration(d->mStart, d->mEnd, d->mDailyDuration ? Duration::Days : Duration::Seconds);
113  } else {
114  return Duration(d->mStart, d->mEnd);
115  }
116 }
117 
119 {
120  return Duration(d->mStart, d->mEnd, type);
121 }
122 
124 {
125  return d->mHasDuration;
126 }
127 
128 void Period::shiftTimes(const QTimeZone &oldZone, const QTimeZone &newZone)
129 {
130  if (oldZone.isValid() && newZone.isValid() && oldZone != newZone) {
131  d->mStart = d->mStart.toTimeZone(oldZone);
132  d->mStart.setTimeZone(newZone);
133  d->mEnd = d->mEnd.toTimeZone(oldZone);
134  d->mEnd.setTimeZone(newZone);
135  }
136 }
137 
139 {
140  serializeQDateTimeAsKDateTime(stream, period.d->mStart);
141  serializeQDateTimeAsKDateTime(stream, period.d->mEnd);
142  return stream << period.d->mDailyDuration << period.d->mHasDuration;
143 }
144 
146 {
147  deserializeKDateTimeAsQDateTime(stream, period.d->mStart);
148  deserializeKDateTimeAsQDateTime(stream, period.d->mEnd);
149  stream >> period.d->mDailyDuration >> period.d->mHasDuration;
150  return stream;
151 }
152 
154 {
155  if (key.hasDuration()) {
156  return qHash(key.duration());
157  } else {
158  return qHash(key.start().toString() + key.end().toString());
159  }
160 }
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:123
KCALENDARCORE_EXPORT QDataStream & operator<<(QDataStream &out, const KCalendarCore::Alarm::Ptr &)
Alarm serializer.
Definition: alarm.cpp:821
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:104
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:128
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:834
Duration duration() const
Returns the duration of the period.
Definition: period.cpp:109
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:99
Period & operator=(const Period &other)
Sets this period equal to the other one.
Definition: period.cpp:88
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 Thu Apr 22 2021 22:51:07 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.