KCal Library
duration.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00034 #include "duration.h"
00035
00036 #include <kdatetime.h>
00037
00038 using namespace KCal;
00039
00044
00045 class KCal::Duration::Private
00046 {
00047 public:
00048 int seconds() const { return mDaily ? mDuration * 86400 : mDuration; }
00049 int mDuration;
00050 bool mDaily;
00051 };
00052
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
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
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
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 return
00141 d->mDuration == other.d->mDuration &&
00142 d->mDaily == other.d->mDaily;
00143 }
00144
00145 Duration &Duration::operator+=( const Duration &other )
00146 {
00147 if ( d->mDaily == other.d->mDaily ) {
00148 d->mDuration += other.d->mDuration;
00149 } else if ( d->mDaily ) {
00150 d->mDuration = d->mDuration * 86400 + other.d->mDuration;
00151 d->mDaily = false;
00152 } else {
00153 d->mDuration += other.d->mDuration + 86400;
00154 }
00155 return *this;
00156 }
00157
00158 Duration Duration::operator-() const
00159 {
00160 return Duration( -d->mDuration, ( d->mDaily ? Days : Seconds ) );
00161 }
00162
00163 Duration &Duration::operator-=( const Duration &duration )
00164 {
00165 return operator+=( -duration );
00166 }
00167
00168 Duration &Duration::operator*=( int value )
00169 {
00170 d->mDuration *= value;
00171 return *this;
00172 }
00173
00174 Duration &Duration::operator/=( int value )
00175 {
00176 d->mDuration /= value;
00177 return *this;
00178 }
00179
00180 KDateTime Duration::end( const KDateTime &start ) const
00181 {
00182 return d->mDaily ? start.addDays( d->mDuration )
00183 : start.addSecs( d->mDuration );
00184 }
00185
00186 Duration::Type Duration::type() const
00187 {
00188 return d->mDaily ? Days : Seconds;
00189 }
00190
00191 bool Duration::isDaily() const
00192 {
00193 return d->mDaily;
00194 }
00195
00196 int Duration::asSeconds() const
00197 {
00198 return d->seconds();
00199 }
00200
00201 int Duration::asDays() const
00202 {
00203 return d->mDaily ? d->mDuration : d->mDuration / 86400;
00204 }
00205
00206 int Duration::value() const
00207 {
00208 return d->mDuration;
00209 }