KCal Library
period.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 <software@astrojar.org.uk> 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 */ 00033 #include "period.h" 00034 00035 #include <kdebug.h> 00036 #include <klocale.h> 00037 00038 using namespace KCal; 00039 00040 //@cond PRIVATE 00041 class KCal::Period::Private 00042 { 00043 public: 00044 Private() : mHasDuration( false ) {} 00045 Private( const KDateTime &start, const KDateTime &end, bool hasDuration ) 00046 : mStart( start ), 00047 mEnd( end ), 00048 mHasDuration( hasDuration ) 00049 {} 00050 KDateTime mStart; // period starting date/time 00051 KDateTime mEnd; // period ending date/time 00052 bool mHasDuration; // does period have a duration? 00053 bool mDailyDuration; // duration is defined as number of days, not seconds 00054 }; 00055 //@endcond 00056 00057 Period::Period() : d( new KCal::Period::Private() ) 00058 { 00059 } 00060 00061 Period::Period( const KDateTime &start, const KDateTime &end ) 00062 : d( new KCal::Period::Private( start, end, false ) ) 00063 { 00064 } 00065 00066 Period::Period( const KDateTime &start, const Duration &duration ) 00067 : d( new KCal::Period::Private( start, duration.end( start ), true ) ) 00068 { 00069 d->mDailyDuration = duration.isDaily(); 00070 } 00071 00072 Period::Period( const Period &period ) 00073 : d( new KCal::Period::Private( *period.d ) ) 00074 { 00075 } 00076 00077 Period::~Period() 00078 { 00079 delete d; 00080 } 00081 00082 bool Period::operator<( const Period &other ) const 00083 { 00084 return d->mStart < other.d->mStart; 00085 } 00086 00087 bool Period::operator==( const Period &other ) const 00088 { 00089 return 00090 d->mStart == other.d->mStart && 00091 d->mEnd == other.d->mEnd && 00092 d->mHasDuration == other.d->mHasDuration; 00093 } 00094 00095 Period &Period::operator=( const Period &other ) 00096 { 00097 // check for self assignment 00098 if ( &other == this ) { 00099 return *this; 00100 } 00101 00102 *d = *other.d; 00103 return *this; 00104 } 00105 00106 KDateTime Period::start() const 00107 { 00108 return d->mStart; 00109 } 00110 00111 KDateTime Period::end() const 00112 { 00113 return d->mEnd; 00114 } 00115 00116 Duration Period::duration() const 00117 { 00118 if ( d->mHasDuration ) { 00119 return Duration( d->mStart, d->mEnd, 00120 d->mDailyDuration ? Duration::Days : Duration::Seconds ); 00121 } else { 00122 return Duration( d->mStart, d->mEnd ); 00123 } 00124 } 00125 00126 Duration Period::duration( Duration::Type type ) const 00127 { 00128 return Duration( d->mStart, d->mEnd, type ); 00129 } 00130 00131 bool Period::hasDuration() const 00132 { 00133 return d->mHasDuration; 00134 } 00135 00136 void Period::shiftTimes( const KDateTime::Spec &oldSpec, 00137 const KDateTime::Spec &newSpec ) 00138 { 00139 if ( oldSpec.isValid() && newSpec.isValid() && oldSpec != newSpec ) { 00140 d->mStart = d->mStart.toTimeSpec( oldSpec ); 00141 d->mStart.setTimeSpec( newSpec ); 00142 d->mEnd = d->mEnd.toTimeSpec( oldSpec ); 00143 d->mEnd.setTimeSpec( newSpec ); 00144 } 00145 }