• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdepimlibs API Reference
  • KDE Home
  • Contact Us
 

KCal Library

  • sources
  • kde-4.12
  • kdepimlibs
  • kcal
duration.cpp
Go to the documentation of this file.
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 <djarvie@kde.org>
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 */
34 #include "duration.h"
35 
36 #include <kdatetime.h>
37 
38 using namespace KCal;
39 
44 //@cond PRIVATE
45 class KCal::Duration::Private
46 {
47  public:
48  int seconds() const { return mDaily ? mDuration * 86400 : mDuration; }
49  int mDuration; // number of seconds or days in the duration
50  bool mDaily; // specified in terms of days rather than seconds
51 };
52 //@endcond
53 
54 Duration::Duration()
55  : d( new KCal::Duration::Private() )
56 {
57 }
58 
59 Duration::Duration( const KDateTime &start, const KDateTime &end )
60  : d( new KCal::Duration::Private() )
61 {
62  if ( start.time() == end.time() && start.timeSpec() == end.timeSpec() ) {
63  d->mDuration = start.daysTo( end );
64  d->mDaily = true;
65  } else {
66  d->mDuration = start.secsTo( end );
67  d->mDaily = false;
68  }
69 }
70 
71 Duration::Duration( const KDateTime &start, const KDateTime &end, Type type )
72  : d( new KCal::Duration::Private() )
73 {
74  if ( type == Days ) {
75  KDateTime endSt( end.toTimeSpec( start ) );
76  d->mDuration = start.daysTo( endSt );
77  if ( d->mDuration ) {
78  // Round down to whole number of days if necessary
79  if ( start < endSt ) {
80  if ( endSt.time() < start.time() ) {
81  --d->mDuration;
82  }
83  } else {
84  if ( endSt.time() > start.time() ) {
85  ++d->mDuration;
86  }
87  }
88  }
89  d->mDaily = true;
90  } else {
91  d->mDuration = start.secsTo( end );
92  d->mDaily = false;
93  }
94 }
95 
96 Duration::Duration( int duration, Type type )
97  : d( new KCal::Duration::Private() )
98 {
99  d->mDuration = duration;
100  d->mDaily = ( type == Days );
101 }
102 
103 Duration::Duration( const Duration &duration )
104  : d( new KCal::Duration::Private( *duration.d ) )
105 {
106 }
107 
108 Duration::~Duration()
109 {
110  delete d;
111 }
112 
113 Duration &Duration::operator=( const Duration &duration )
114 {
115  // check for self assignment
116  if ( &duration == this ) {
117  return *this;
118  }
119 
120  *d = *duration.d;
121  return *this;
122 }
123 
124 Duration::operator bool() const
125 {
126  return d->mDuration;
127 }
128 
129 bool Duration::operator<( const Duration &other ) const
130 {
131  if ( d->mDaily == other.d->mDaily ) {
132  // guard against integer overflow for two daily durations
133  return d->mDuration < other.d->mDuration;
134  }
135  return d->seconds() < other.d->seconds();
136 }
137 
138 bool Duration::operator==( const Duration &other ) const
139 {
140  // Note: daily and non-daily durations are always unequal, since a day's
141  // duration may differ from 24 hours if it happens to span a daylight saving
142  // time change.
143  return
144  d->mDuration == other.d->mDuration &&
145  d->mDaily == other.d->mDaily;
146 }
147 
148 Duration &Duration::operator+=( const Duration &other )
149 {
150  if ( d->mDaily == other.d->mDaily ) {
151  d->mDuration += other.d->mDuration;
152  } else if ( d->mDaily ) {
153  d->mDuration = d->mDuration * 86400 + other.d->mDuration;
154  d->mDaily = false;
155  } else {
156  d->mDuration += other.d->mDuration + 86400;
157  }
158  return *this;
159 }
160 
161 Duration Duration::operator-() const
162 {
163  return Duration( -d->mDuration, ( d->mDaily ? Days : Seconds ) );
164 }
165 
166 Duration &Duration::operator-=( const Duration &duration )
167 {
168  return operator+=( -duration );
169 }
170 
171 Duration &Duration::operator*=( int value )
172 {
173  d->mDuration *= value;
174  return *this;
175 }
176 
177 Duration &Duration::operator/=( int value )
178 {
179  d->mDuration /= value;
180  return *this;
181 }
182 
183 KDateTime Duration::end( const KDateTime &start ) const
184 {
185  return d->mDaily ? start.addDays( d->mDuration )
186  : start.addSecs( d->mDuration );
187 }
188 
189 Duration::Type Duration::type() const
190 {
191  return d->mDaily ? Days : Seconds;
192 }
193 
194 bool Duration::isDaily() const
195 {
196  return d->mDaily;
197 }
198 
199 int Duration::asSeconds() const
200 {
201  return d->seconds();
202 }
203 
204 int Duration::asDays() const
205 {
206  return d->mDaily ? d->mDuration : d->mDuration / 86400;
207 }
208 
209 int Duration::value() const
210 {
211  return d->mDuration;
212 }
KCal::Duration::~Duration
~Duration()
Destroys a duration.
Definition: duration.cpp:108
KCal::Duration::operator<
bool operator<(const Duration &other) const
Returns true if this duration is smaller than the other.
Definition: duration.cpp:129
KCal::Duration::type
Type type() const
Returns the time units (seconds or days) used to specify the duration.
Definition: duration.cpp:189
KCal::Duration::asDays
int asDays() const
Returns the length of the duration in days.
Definition: duration.cpp:204
KCal::Duration::asSeconds
int asSeconds() const
Returns the length of the duration in seconds.
Definition: duration.cpp:199
KCal::Duration::Days
duration is a number of days
Definition: duration.h:60
KCal::Duration::isDaily
bool isDaily() const
Returns whether the duration is specified in terms of days rather than seconds.
Definition: duration.cpp:194
KCal::Duration::Duration
Duration()
Constructs a duration of 0 seconds.
Definition: duration.cpp:54
KCal::Duration::operator+=
Duration & operator+=(const Duration &other)
Adds another duration to this one.
Definition: duration.cpp:148
KCal::Duration::Seconds
duration is a number of seconds
Definition: duration.h:59
KCal::Duration::operator*=
Duration & operator*=(int value)
Multiplies this duration by a value.
Definition: duration.cpp:171
KCal::Duration::operator=
Duration & operator=(const Duration &duration)
Sets this duration equal to duration.
Definition: duration.cpp:113
duration.h
This file is part of the API for handling calendar data and defines the Duration class.
KCal::Duration::value
int value() const
Returns the length of the duration in seconds or days.
Definition: duration.cpp:209
KCal::Duration::Type
Type
The unit of time used to define the duration.
Definition: duration.h:58
KCal::Duration
Represents a span of time measured in seconds or days.
Definition: duration.h:52
KCal::Duration::operator==
bool operator==(const Duration &other) const
Returns true if this duration is equal to the other.
Definition: duration.cpp:138
KCal::Duration::operator-
Duration operator-() const
Returns the negative of this duration.
Definition: duration.cpp:161
KCal::Duration::end
KDateTime end(const KDateTime &start) const
Computes a duration end time by adding the number of seconds or days in the duration to the specified...
Definition: duration.cpp:183
KCal::Duration::operator/=
Duration & operator/=(int value)
Divides this duration by a value.
Definition: duration.cpp:177
KCal::Duration::operator-=
Duration & operator-=(const Duration &other)
Subtracts another duration from this one.
Definition: duration.cpp:166
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 23:00:57 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KCal Library

Skip menu "KCal Library"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdepimlibs API Reference

Skip menu "kdepimlibs API Reference"
  • akonadi
  •   contact
  •   kmime
  •   socialutils
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kldap
  • kmbox
  • kmime
  • kpimidentities
  • kpimtextedit
  • kresources
  • ktnef
  • kxmlrpcclient
  • microblog

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal