1/*
2 This file is part of the kcal 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 <kdebug.h>
36#include <klocalizedstring.h>
37
38using namespace KCal;
39
40//@cond PRIVATE
41class KCal::Period::Private
42{
43 public:
44 Private() : mHasDuration( false ) {}
45 Private( const KDateTime &start, const KDateTime &end, bool hasDuration )
46 : mStart( start ),
47 mEnd( end ),
48 mHasDuration( hasDuration )
49 {}
50 KDateTime mStart; // period starting date/time
51 KDateTime mEnd; // period ending date/time
52 bool mHasDuration; // does period have a duration?
53 bool mDailyDuration; // duration is defined as number of days, not seconds
54};
55//@endcond
56
57Period::Period() : d( new KCal::Period::Private() )
58{
59}
60
61Period::Period( const KDateTime &start, const KDateTime &end )
62 : d( new KCal::Period::Private( start, end, false ) )
63{
64}
65
66Period::Period( const KDateTime &start, const Duration &duration )
67 : d( new KCal::Period::Private( start, duration.end( start ), true ) )
68{
69 d->mDailyDuration = duration.isDaily();
70}
71
72Period::Period( const Period &period )
73 : d( new KCal::Period::Private( *period.d ) )
74{
75}
76
77Period::~Period()
78{
79 delete d;
80}
81
82bool Period::operator<( const Period &other ) const
83{
84 return d->mStart < other.d->mStart;
85}
86
87bool Period::operator==( const Period &other ) const
88{
89 return
90 d->mStart == other.d->mStart &&
91 d->mEnd == other.d->mEnd &&
92 d->mHasDuration == other.d->mHasDuration;
93}
94
95Period &Period::operator=( const Period &other )
96{
97 // check for self assignment
98 if ( &other == this ) {
99 return *this;
100 }
101
102 *d = *other.d;
103 return *this;
104}
105
106KDateTime Period::start() const
107{
108 return d->mStart;
109}
110
111KDateTime Period::end() const
112{
113 return d->mEnd;
114}
115
116Duration Period::duration() const
117{
118 if ( d->mHasDuration ) {
119 return Duration( d->mStart, d->mEnd,
120 d->mDailyDuration ? Duration::Days : Duration::Seconds );
121 } else {
122 return Duration( d->mStart, d->mEnd );
123 }
124}
125
126Duration Period::duration( Duration::Type type ) const
127{
128 return Duration( d->mStart, d->mEnd, type );
129}
130
131bool Period::hasDuration() const
132{
133 return d->mHasDuration;
134}
135
136void Period::shiftTimes( const KDateTime::Spec &oldSpec,
137 const KDateTime::Spec &newSpec )
138{
139 if ( oldSpec.isValid() && newSpec.isValid() && oldSpec != newSpec ) {
140 d->mStart = d->mStart.toTimeSpec( oldSpec );
141 d->mStart.setTimeSpec( newSpec );
142 d->mEnd = d->mEnd.toTimeSpec( oldSpec );
143 d->mEnd.setTimeSpec( newSpec );
144 }
145}
146