libkcal
duration.cpp00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "duration.h"
00023
00024 using namespace KCal;
00025
00026 Duration::Duration()
00027 {
00028 mDuration = 0;
00029 }
00030
00031 Duration::Duration( const TQDateTime &start, const TQDateTime &end )
00032 {
00033 if ( start.time() == end.time() ) {
00034 mDuration = start.daysTo( end );
00035 mDaily = true;
00036 } else {
00037 mDuration = start.secsTo( end );
00038 mDaily = false;
00039 }
00040 }
00041
00042 Duration::Duration( const TQDateTime &start, const TQDateTime &end, Type type )
00043 {
00044 if ( type == Days ) {
00045 mDuration = start.daysTo( end );
00046 if ( mDuration ) {
00047
00048 if ( start < end ) {
00049 if ( end.time() < start.time() ) {
00050 --mDuration;
00051 }
00052 } else {
00053 if ( end.time() > start.time() ) {
00054 ++mDuration;
00055 }
00056 }
00057 }
00058 mDaily = true;
00059 } else {
00060 mDuration = start.secsTo( end );
00061 mDaily = false;
00062 }
00063 }
00064
00065 Duration::Duration( int duration, Type type )
00066 {
00067 mDuration = duration;
00068 mDaily = ( type == Days );
00069 }
00070
00071 Duration::Duration( const Duration &duration )
00072 {
00073 mDuration = duration.mDuration;
00074 mDaily = duration.mDaily;
00075 }
00076
00077 Duration &Duration::operator=( const Duration &duration )
00078 {
00079
00080 if ( &duration == this ) {
00081 return *this;
00082 }
00083
00084 mDuration = duration.mDuration;
00085 mDaily = duration.mDaily;
00086
00087 return *this;
00088 }
00089
00090 Duration::operator bool() const
00091 {
00092 return mDuration;
00093 }
00094
00095 bool Duration::operator<( const Duration &other ) const
00096 {
00097 if ( mDaily == other.mDaily ) {
00098
00099 return mDuration < other.mDuration;
00100 }
00101 return seconds() < other.seconds();
00102 }
00103
00104 bool Duration::operator==( const Duration &other ) const
00105 {
00106
00107
00108
00109 return
00110 mDuration == other.mDuration &&
00111 mDaily == other.mDaily;
00112 }
00113
00114
00115 Duration &Duration::operator+=( const Duration &other )
00116 {
00117 if ( mDaily == other.mDaily ) {
00118 mDuration += other.mDuration;
00119 } else if ( mDaily ) {
00120 mDuration = mDuration * 86400 + other.mDuration;
00121 mDaily = false;
00122 } else {
00123 mDuration += other.mDuration + 86400;
00124 }
00125 return *this;
00126 }
00127
00128 Duration Duration::operator-() const
00129 {
00130 return Duration( -mDuration, ( mDaily ? Days : Seconds ) );
00131 }
00132
00133 Duration &Duration::operator-=( const Duration &duration )
00134 {
00135 return operator+=( -duration );
00136 }
00137
00138 Duration &Duration::operator*=( int value )
00139 {
00140 mDuration *= value;
00141 return *this;
00142 }
00143
00144 Duration &Duration::operator/=( int value )
00145 {
00146 mDuration /= value;
00147 return *this;
00148 }
00149
00150 TQDateTime Duration::end( const TQDateTime &start ) const
00151 {
00152 return mDaily ? start.addDays( mDuration )
00153 : start.addSecs( mDuration );
00154 }
00155 Duration::Type Duration::type() const
00156 {
00157 return mDaily ? Days : Seconds;
00158 }
00159
00160 bool Duration::isDaily() const
00161 {
00162 return mDaily;
00163 }
00164
00165 int Duration::asSeconds() const
00166 {
00167 return seconds();
00168 }
00169
00170 int Duration::asDays() const
00171 {
00172 return mDaily ? mDuration : mDuration / 86400;
00173 }
00174
00175 int Duration::value() const
00176 {
00177 return mDuration;
00178 }
|