1/*
2 This file is part of the kcalcore library.
3
4 Copyright (c) 2001 Cornelius Schumacher <schumacher@kde.org>
5 Copyright (c) 2007 David Jarvie <software@astrojar.org.uk>
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Library General Public
9 License as published by the Free Software Foundation; either
10 version 2 of the License, or (at your option) any later version.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Library General Public License for more details.
16
17 You should have received a copy of the GNU Library General Public License
18 along with this library; see the file COPYING.LIB. If not, write to
19 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA.
21*/
22/**
23 @file
24 This file is part of the API for handling calendar data and
25 defines the Period class.
26
27 @brief
28 Represents a period of time.
29
30 @author Cornelius Schumacher \<schumacher@kde.org\>
31*/
32
33#include "period.h"
34
35#include <KDateTime>
36#include <KSystemTimeZone>
37
38#include <QtCore/QHash>
39
40using namespace KCalCore;
41
42//@cond PRIVATE
43class KCalCore::Period::Private
44{
45public:
46 Private() : mHasDuration(false), mDailyDuration(false) {}
47 Private(const KDateTime &start, const KDateTime &end, bool hasDuration)
48 : mStart(start),
49 mEnd(end),
50 mHasDuration(hasDuration),
51 mDailyDuration(false)
52 {}
53 KDateTime mStart; // period starting date/time
54 KDateTime mEnd; // period ending date/time
55 bool mHasDuration; // does period have a duration?
56 bool mDailyDuration; // duration is defined as number of days, not seconds
57};
58//@endcond
59
60Period::Period() : d(new KCalCore::Period::Private())
61{
62}
63
64Period::Period(const KDateTime &start, const KDateTime &end)
65 : d(new KCalCore::Period::Private(start, end, false))
66{
67}
68
69Period::Period(const KDateTime &start, const Duration &duration)
70 : d(new KCalCore::Period::Private(start, duration.end(start), true))
71{
72 d->mDailyDuration = duration.isDaily();
73}
74
75Period::Period(const Period &period)
76 : d(new KCalCore::Period::Private(*period.d))
77{
78}
79
80Period::~Period()
81{
82 delete d;
83}
84
85bool Period::operator<(const Period &other) const
86{
87 return d->mStart < other.d->mStart;
88}
89
90bool Period::operator==(const Period &other) const
91{
92 return
93 ((d->mStart == other.d->mStart) ||
94 (!d->mStart.isValid() && !other.d->mStart.isValid())) &&
95 ((d->mEnd == other.d->mEnd) ||
96 (!d->mEnd.isValid() && !other.d->mEnd.isValid())) &&
97 d->mHasDuration == other.d->mHasDuration;
98}
99
100Period &Period::operator=(const Period &other)
101{
102 // check for self assignment
103 if (&other == this) {
104 return *this;
105 }
106
107 *d = *other.d;
108 return *this;
109}
110
111KDateTime Period::start() const
112{
113 return d->mStart;
114}
115
116KDateTime Period::end() const
117{
118 return d->mEnd;
119}
120
121Duration Period::duration() const
122{
123 if (d->mHasDuration) {
124 return Duration(d->mStart, d->mEnd,
125 d->mDailyDuration ? Duration::Days : Duration::Seconds);
126 } else {
127 return Duration(d->mStart, d->mEnd);
128 }
129}
130
131Duration Period::duration(Duration::Type type) const
132{
133 return Duration(d->mStart, d->mEnd, type);
134}
135
136bool Period::hasDuration() const
137{
138 return d->mHasDuration;
139}
140
141void Period::shiftTimes(const KDateTime::Spec &oldSpec,
142 const KDateTime::Spec &newSpec)
143{
144 if (oldSpec.isValid() && newSpec.isValid() && oldSpec != newSpec) {
145 d->mStart = d->mStart.toTimeSpec(oldSpec);
146 d->mStart.setTimeSpec(newSpec);
147 d->mEnd = d->mEnd.toTimeSpec(oldSpec);
148 d->mEnd.setTimeSpec(newSpec);
149 }
150}
151
152QDataStream &KCalCore::operator<<(QDataStream &stream, const KCalCore::Period &period)
153{
154 return stream << period.d->mStart
155 << period.d->mEnd
156 << period.d->mDailyDuration
157 << period.d->mHasDuration;
158}
159
160QDataStream &KCalCore::operator>>(QDataStream &stream, KCalCore::Period &period)
161{
162 stream >> period.d->mStart
163 >> period.d->mEnd
164 >> period.d->mDailyDuration
165 >> period.d->mHasDuration;
166 return stream;
167}
168
169uint qHash(const KCalCore::Period &key)
170{
171 QString strToHash = key.start().toString();
172 if (key.hasDuration()) {
173 strToHash += key.duration();
174 } else {
175 strToHash += key.end().toString();
176 }
177 return qHash(strToHash);
178}
179
180