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 */
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() : mHasDuration(false), mDailyDuration(false) {}
33  Private(const QDateTime &start, const QDateTime &end, bool hasDuration)
34  : mStart(start),
35  mEnd(end),
36  mHasDuration(hasDuration),
37  mDailyDuration(false)
38  {}
39  QDateTime mStart; // period starting date/time
40  QDateTime mEnd; // period ending date/time
41  bool mHasDuration = false; // does period have a duration?
42  bool mDailyDuration = false; // duration is defined as number of days, not seconds
43 };
44 //@endcond
45 
46 Period::Period() : d(new KCalendarCore::Period::Private())
47 {
48 }
49 
51  : d(new KCalendarCore::Period::Private(start, end, false))
52 {
53 }
54 
56  : d(new KCalendarCore::Period::Private(start, duration.end(start), true))
57 {
58  d->mDailyDuration = duration.isDaily();
59 }
60 
61 Period::Period(const Period &period)
62  : d(new KCalendarCore::Period::Private(*period.d))
63 {
64 }
65 
67 {
68  delete d;
69 }
70 
71 bool Period::operator<(const Period &other) const
72 {
73  return d->mStart < other.d->mStart;
74 }
75 
76 bool Period::operator==(const Period &other) const
77 {
78  return
79  ((d->mStart == other.d->mStart) ||
80  (!d->mStart.isValid() && !other.d->mStart.isValid())) &&
81  ((d->mEnd == other.d->mEnd) ||
82  (!d->mEnd.isValid() && !other.d->mEnd.isValid())) &&
83  d->mHasDuration == other.d->mHasDuration;
84 }
85 
87 {
88  // check for self assignment
89  if (&other == this) {
90  return *this;
91  }
92 
93  *d = *other.d;
94  return *this;
95 }
96 
98 {
99  return d->mStart;
100 }
101 
103 {
104  return d->mEnd;
105 }
106 
108 {
109  if (d->mHasDuration) {
110  return Duration(d->mStart, d->mEnd,
111  d->mDailyDuration ? Duration::Days : Duration::Seconds);
112  } else {
113  return Duration(d->mStart, d->mEnd);
114  }
115 }
116 
118 {
119  return Duration(d->mStart, d->mEnd, type);
120 }
121 
123 {
124  return d->mHasDuration;
125 }
126 
127 void Period::shiftTimes(const QTimeZone &oldZone, const QTimeZone &newZone)
128 {
129  if (oldZone.isValid() && newZone.isValid() && oldZone != newZone) {
130  d->mStart = d->mStart.toTimeZone(oldZone);
131  d->mStart.setTimeZone(newZone);
132  d->mEnd = d->mEnd.toTimeZone(oldZone);
133  d->mEnd.setTimeZone(newZone);
134  }
135 }
136 
138 {
139  serializeQDateTimeAsKDateTime(stream, period.d->mStart);
140  serializeQDateTimeAsKDateTime(stream, period.d->mEnd);
141  return stream << period.d->mDailyDuration
142  << period.d->mHasDuration;
143 }
144 
146 {
147  deserializeKDateTimeAsQDateTime(stream, period.d->mStart);
148  deserializeKDateTimeAsQDateTime(stream, period.d->mEnd);
149  stream >> period.d->mDailyDuration
150  >> period.d->mHasDuration;
151  return stream;
152 }
153 
155 {
156  if (key.hasDuration()) {
157  return qHash(key.duration());
158  } else {
159  return qHash(key.start().toString() + key.end().toString());
160  }
161 }
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:122
KCALENDARCORE_EXPORT QDataStream & operator<<(QDataStream &out, const KCalendarCore::Alarm::Ptr &)
Alarm serializer.
Definition: alarm.cpp:825
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:102
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:127
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:71
bool operator==(const Period &other) const
Returns true if this period is equal to the other one.
Definition: period.cpp:76
KCALENDARCORE_EXPORT QDataStream & operator>>(QDataStream &in, const KCalendarCore::Alarm::Ptr &)
Alarm deserializer.
Definition: alarm.cpp:849
Duration duration() const
Returns the duration of the period.
Definition: period.cpp:107
Represents a span of time measured in seconds or days.
Definition: duration.h:44
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:38
bool isDaily() const
Returns whether the duration is specified in terms of days rather than seconds.
Definition: duration.cpp:183
QDateTime start() const
Returns when this period starts.
Definition: period.cpp:97
Period & operator=(const Period &other)
Sets this period equal to the other one.
Definition: period.cpp:86
Period()
Constructs a period without a duration.
Definition: period.cpp:46
duration is a number of seconds
Definition: duration.h:51
bool isValid() const const
duration is a number of days
Definition: duration.h:52
Type
The unit of time used to define the duration.
Definition: duration.h:50
~Period()
Destroys a period.
Definition: period.cpp:66
KCALENDARCORE_EXPORT uint qHash(const KCalendarCore::Person &key)
Return a hash value for a Person argument.
Definition: person.cpp:148
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 Tue Jan 26 2021 22:51:02 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.