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

KCalCore Library

  • kcalcore
duration.cpp
Go to the documentation of this file.
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 <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 */
33 #include "duration.h"
34 #include <KDateTime>
35 
36 using namespace KCalCore;
37 
42 //@cond PRIVATE
43 class KCalCore::Duration::Private
44 {
45  public:
46  int seconds() const {
47  return mDaily ? mDuration * 86400 : mDuration;
48  }
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 KCalCore::Duration::Private() )
56 {
57 }
58 
59 Duration::Duration( const KDateTime &start, const KDateTime &end )
60  : d( new KCalCore::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 KCalCore::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 KCalCore::Duration::Private() )
98 {
99  d->mDuration = duration;
100  d->mDaily = ( type == Days );
101 }
102 
103 Duration::Duration( const Duration &duration )
104  : d( new KCalCore::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 d->mDuration == other.d->mDuration &&
144  d->mDaily == other.d->mDaily;
145 }
146 
147 Duration &Duration::operator+=( const Duration &other )
148 {
149  if ( d->mDaily == other.d->mDaily ) {
150  d->mDuration += other.d->mDuration;
151  } else if ( d->mDaily ) {
152  d->mDuration = d->mDuration * 86400 + other.d->mDuration;
153  d->mDaily = false;
154  } else {
155  d->mDuration += other.d->mDuration + 86400;
156  }
157  return *this;
158 }
159 
160 Duration Duration::operator-() const
161 {
162  return Duration( -d->mDuration, ( d->mDaily ? Days : Seconds ) );
163 }
164 
165 Duration &Duration::operator-=( const Duration &duration )
166 {
167  return operator+=( -duration );
168 }
169 
170 Duration &Duration::operator*=( int value )
171 {
172  d->mDuration *= value;
173  return *this;
174 }
175 
176 Duration &Duration::operator/=( int value )
177 {
178  d->mDuration /= value;
179  return *this;
180 }
181 
182 KDateTime Duration::end( const KDateTime &start ) const
183 {
184  return d->mDaily ? start.addDays( d->mDuration )
185  : start.addSecs( d->mDuration );
186 }
187 
188 Duration::Type Duration::type() const
189 {
190  return d->mDaily ? Days : Seconds;
191 }
192 
193 bool Duration::isDaily() const
194 {
195  return d->mDaily;
196 }
197 
198 int Duration::asSeconds() const
199 {
200  return d->seconds();
201 }
202 
203 int Duration::asDays() const
204 {
205  return d->mDaily ? d->mDuration : d->mDuration / 86400;
206 }
207 
208 int Duration::value() const
209 {
210  return d->mDuration;
211 }
This file is part of the KDE documentation.
Documentation copyright © 1996-2013 The KDE developers.
Generated on Sat Jul 13 2013 01:24:51 by doxygen 1.8.3.1 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KCalCore Library

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

kdepimlibs-4.10.5 API Reference

Skip menu "kdepimlibs-4.10.5 API Reference"
  • akonadi
  •   contact
  •   kmime
  •   socialutils
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • microblog
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
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