• Skip to content
  • Skip to link menu
KDE 4.6 API Reference
  • KDE API Reference
  • KDE-PIM Libraries
  • KDE Home
  • Contact Us
 

KCal Library

duration.cpp
Go to the documentation of this file.
00001 /*
00002     This file is part of the kcal library.
00003 
00004     Copyright (c) 2001 Cornelius Schumacher <schumacher@kde.org>
00005     Copyright (c) 2007 David Jarvie <djarvie@kde.org>
00006 
00007     This library is free software; you can redistribute it and/or
00008     modify it under the terms of the GNU Library General Public
00009     License as published by the Free Software Foundation; either
00010     version 2 of the License, or (at your option) any later version.
00011 
00012     This library is distributed in the hope that it will be useful,
00013     but WITHOUT ANY WARRANTY; without even the implied warranty of
00014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015     Library General Public License for more details.
00016 
00017     You should have received a copy of the GNU Library General Public License
00018     along with this library; see the file COPYING.LIB.  If not, write to
00019     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00020     Boston, MA 02110-1301, USA.
00021 */
00034 #include "duration.h"
00035 
00036 #include <kdatetime.h>
00037 
00038 using namespace KCal;
00039 
00044 //@cond PRIVATE
00045 class KCal::Duration::Private
00046 {
00047   public:
00048     int seconds() const { return mDaily ? mDuration * 86400 : mDuration; }
00049     int mDuration; // number of seconds or days in the duration
00050     bool mDaily;   // specified in terms of days rather than seconds
00051 };
00052 //@endcond
00053 
00054 Duration::Duration()
00055   : d( new KCal::Duration::Private() )
00056 {
00057 }
00058 
00059 Duration::Duration( const KDateTime &start, const KDateTime &end )
00060   : d( new KCal::Duration::Private() )
00061 {
00062   if ( start.time() == end.time() && start.timeSpec() == end.timeSpec() ) {
00063     d->mDuration = start.daysTo( end );
00064     d->mDaily = true;
00065   } else {
00066     d->mDuration = start.secsTo( end );
00067     d->mDaily = false;
00068   }
00069 }
00070 
00071 Duration::Duration( const KDateTime &start, const KDateTime &end, Type type )
00072   : d( new KCal::Duration::Private() )
00073 {
00074   if ( type == Days ) {
00075     KDateTime endSt( end.toTimeSpec( start ) );
00076     d->mDuration = start.daysTo( endSt );
00077     if ( d->mDuration ) {
00078       // Round down to whole number of days if necessary
00079       if ( start < endSt ) {
00080         if ( endSt.time() < start.time() ) {
00081           --d->mDuration;
00082         }
00083       } else {
00084         if ( endSt.time() > start.time() ) {
00085           ++d->mDuration;
00086         }
00087       }
00088     }
00089     d->mDaily = true;
00090   } else {
00091     d->mDuration = start.secsTo( end );
00092     d->mDaily = false;
00093   }
00094 }
00095 
00096 Duration::Duration( int duration, Type type )
00097   : d( new KCal::Duration::Private() )
00098 {
00099   d->mDuration = duration;
00100   d->mDaily = ( type == Days );
00101 }
00102 
00103 Duration::Duration( const Duration &duration )
00104   : d( new KCal::Duration::Private( *duration.d ) )
00105 {
00106 }
00107 
00108 Duration::~Duration()
00109 {
00110   delete d;
00111 }
00112 
00113 Duration &Duration::operator=( const Duration &duration )
00114 {
00115   // check for self assignment
00116   if ( &duration == this ) {
00117     return *this;
00118   }
00119 
00120   *d = *duration.d;
00121   return *this;
00122 }
00123 
00124 Duration::operator bool() const
00125 {
00126   return d->mDuration;
00127 }
00128 
00129 bool Duration::operator<( const Duration &other ) const
00130 {
00131   if ( d->mDaily == other.d->mDaily ) {
00132     // guard against integer overflow for two daily durations
00133     return d->mDuration < other.d->mDuration;
00134   }
00135   return d->seconds() < other.d->seconds();
00136 }
00137 
00138 bool Duration::operator==( const Duration &other ) const
00139 {
00140   // Note: daily and non-daily durations are always unequal, since a day's
00141   // duration may differ from 24 hours if it happens to span a daylight saving
00142   // time change.
00143   return
00144     d->mDuration == other.d->mDuration &&
00145     d->mDaily == other.d->mDaily;
00146 }
00147 
00148 Duration &Duration::operator+=( const Duration &other )
00149 {
00150   if ( d->mDaily == other.d->mDaily ) {
00151     d->mDuration += other.d->mDuration;
00152   } else if ( d->mDaily ) {
00153     d->mDuration = d->mDuration * 86400 + other.d->mDuration;
00154     d->mDaily = false;
00155   } else {
00156     d->mDuration += other.d->mDuration + 86400;
00157   }
00158   return *this;
00159 }
00160 
00161 Duration Duration::operator-() const
00162 {
00163   return Duration( -d->mDuration, ( d->mDaily ? Days : Seconds ) );
00164 }
00165 
00166 Duration &Duration::operator-=( const Duration &duration )
00167 {
00168   return operator+=( -duration );
00169 }
00170 
00171 Duration &Duration::operator*=( int value )
00172 {
00173   d->mDuration *= value;
00174   return *this;
00175 }
00176 
00177 Duration &Duration::operator/=( int value )
00178 {
00179   d->mDuration /= value;
00180   return *this;
00181 }
00182 
00183 KDateTime Duration::end( const KDateTime &start ) const
00184 {
00185   return d->mDaily ? start.addDays( d->mDuration )
00186                    : start.addSecs( d->mDuration );
00187 }
00188 
00189 Duration::Type Duration::type() const
00190 {
00191   return d->mDaily ? Days : Seconds;
00192 }
00193 
00194 bool Duration::isDaily() const
00195 {
00196   return d->mDaily;
00197 }
00198 
00199 int Duration::asSeconds() const
00200 {
00201   return d->seconds();
00202 }
00203 
00204 int Duration::asDays() const
00205 {
00206   return d->mDaily ? d->mDuration : d->mDuration / 86400;
00207 }
00208 
00209 int Duration::value() const
00210 {
00211   return d->mDuration;
00212 }

KCal Library

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

KDE-PIM Libraries

Skip menu "KDE-PIM Libraries"
  • akonadi
  •   contact
  •   kmime
  • kabc
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  •   richtextbuilders
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • microblog
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
Generated for KDE-PIM Libraries by doxygen 1.7.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal