libkcal
duration.cpp
00001 /* 00002 This file is part of libkcal. 00003 00004 Copyright (c) 2001 Cornelius Schumacher <schumacher@kde.org> 00005 00006 This library is free software; you can redistribute it and/or 00007 modify it under the terms of the GNU Library General Public 00008 License as published by the Free Software Foundation; either 00009 version 2 of the License, or (at your option) any later version. 00010 00011 This library is distributed in the hope that it will be useful, 00012 but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00014 Library General Public License for more details. 00015 00016 You should have received a copy of the GNU Library General Public License 00017 along with this library; see the file COPYING.LIB. If not, write to 00018 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00019 Boston, MA 02110-1301, USA. 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 QDateTime &start, const QDateTime &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 QDateTime &start, const QDateTime &end, Type type ) 00043 { 00044 if ( type == Days ) { 00045 mDuration = start.daysTo( end ); 00046 if ( mDuration ) { 00047 // Round down to whole number of days if necessary 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 // check for self assignment 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 // guard against integer overflow for two daily durations 00099 return mDuration < other.mDuration; 00100 } 00101 return seconds() < other.seconds(); 00102 } 00103 00104 bool Duration::operator==( const Duration &other ) const 00105 { 00106 // Note: daily and non-daily durations are always unequal, since a day's 00107 // duration may differ from 24 hours if it happens to span a daylight saving 00108 // time change. 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 QDateTime Duration::end( const QDateTime &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 }