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 <schumacher@kde.org>
5 SPDX-FileCopyrightText: 2007 David Jarvie <djarvie@kde.org>
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 <schumacher@kde.org>
18*/
19
20#include "incidencebase.h"
21#include "period.h"
22#include "utils_p.h"
23
24#include <QHash>
25#include <QTimeZone>
26
27using namespace KCalendarCore;
28
29//@cond PRIVATE
30class Q_DECL_HIDDEN KCalendarCore::Period::Private
31{
32public:
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
62Period::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
68Period::Period(const Period &period)
69 : d(new KCalendarCore::Period::Private(*period.d))
70{
71}
72
74{
75 delete d;
76}
77
78bool Period::operator<(const Period &other) const
79{
80 return d->mStart < other.d->mStart;
81}
82
83bool 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
100bool 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
134void 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
159size_t KCalendarCore::qHash(const KCalendarCore::Period &key, size_t seed)
160{
161 if (key.hasDuration()) {
162 return qHash(key.duration(), seed);
163 } else {
164 return qHashMulti(seed, key.start(), key.end());
165 }
166}
Represents a span of time measured in seconds or days.
Definition duration.h:44
Type
The unit of time used to define the duration.
Definition duration.h:49
@ Days
duration is a number of days
Definition duration.h:51
@ Seconds
duration is a number of seconds
Definition duration.h:50
bool isDaily() const
Returns whether the duration is specified in terms of days rather than seconds.
Definition duration.cpp:181
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 operator==(const Period &other) const
Returns true if this period is equal to the other one.
Definition period.cpp:83
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
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
Period & operator=(const Period &other)
Sets this period equal to the other one.
Definition period.cpp:89
~Period()
Destroys a period.
Definition period.cpp:73
QDateTime end() const
Returns when this period ends.
Definition period.cpp:110
Duration duration() const
Returns the duration of the period.
Definition period.cpp:115
Period()
Constructs a period without a duration.
Definition period.cpp:52
QDateTime start() const
Returns when this period starts.
Definition period.cpp:105
bool isValid() const
Returns true if the Period is not empty.
Definition period.cpp:100
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
Q_SCRIPTABLE Q_NOREPLY void start()
This file is part of the API for handling calendar data and defines the IncidenceBase class.
Namespace for all KCalendarCore types.
Definition alarm.h:37
KCALENDARCORE_EXPORT QDataStream & operator>>(QDataStream &in, const KCalendarCore::Alarm::Ptr &)
Alarm deserializer.
Definition alarm.cpp:833
KCALENDARCORE_EXPORT QDataStream & operator<<(QDataStream &out, const KCalendarCore::Alarm::Ptr &)
Alarm serializer.
Definition alarm.cpp:820
KCALENDARCORE_EXPORT bool identical(const QDateTime &dt1, const QDateTime &dt2)
Compare two QDateTimes for extended equality.
const QList< QKeySequence > & end()
This file is part of the API for handling calendar data and defines the Period class.
bool isValid() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Dec 6 2024 12:01:23 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.