diff options
Diffstat (limited to 'libkcal/duration.cpp')
-rw-r--r-- | libkcal/duration.cpp | 140 |
1 files changed, 129 insertions, 11 deletions
diff --git a/libkcal/duration.cpp b/libkcal/duration.cpp index 0dfd8781c..fdb5a3701 100644 --- a/libkcal/duration.cpp +++ b/libkcal/duration.cpp @@ -19,42 +19,160 @@ Boston, MA 02110-1301, USA. */ -#include <kdebug.h> -#include <klocale.h> - #include "duration.h" using namespace KCal; Duration::Duration() { - mSeconds = 0; + mDuration = 0; } Duration::Duration( const TQDateTime &start, const TQDateTime &end ) { - mSeconds = start.secsTo( end ); + if ( start.time() == end.time() ) { + mDuration = start.daysTo( end ); + mDaily = true; + } else { + mDuration = start.secsTo( end ); + mDaily = false; + } +} + +Duration::Duration( const TQDateTime &start, const TQDateTime &end, Type type ) +{ + if ( type == Days ) { + mDuration = start.daysTo( end ); + if ( mDuration ) { + // Round down to whole number of days if necessary + if ( start < end ) { + if ( end.time() < start.time() ) { + --mDuration; + } + } else { + if ( end.time() > start.time() ) { + ++mDuration; + } + } + } + mDaily = true; + } else { + mDuration = start.secsTo( end ); + mDaily = false; + } +} + +Duration::Duration( int duration, Type type ) +{ + mDuration = duration; + mDaily = ( type == Days ); +} + +Duration::Duration( const Duration &duration ) +{ + mDuration = duration.mDuration; + mDaily = duration.mDaily; +} + +Duration &Duration::operator=( const Duration &duration ) +{ + // check for self assignment + if ( &duration == this ) { + return *this; + } + + mDuration = duration.mDuration; + mDaily = duration.mDaily; + + return *this; +} + +Duration::operator bool() const +{ + return mDuration; +} + +bool Duration::operator<( const Duration &other ) const +{ + if ( mDaily == other.mDaily ) { + // guard against integer overflow for two daily durations + return mDuration < other.mDuration; + } + return seconds() < other.seconds(); } -Duration::Duration( int seconds ) +bool Duration::operator==( const Duration &other ) const { - mSeconds = seconds; + // Note: daily and non-daily durations are always unequal, since a day's + // duration may differ from 24 hours if it happens to span a daylight saving + // time change. + return + mDuration == other.mDuration && + mDaily == other.mDaily; } -bool KCal::operator==( const Duration& d1, const Duration& d2 ) +Duration &Duration::operator+=( const Duration &other ) { - return ( d1.asSeconds() == d2.asSeconds() ); + if ( mDaily == other.mDaily ) { + mDuration += other.mDuration; + } else if ( mDaily ) { + mDuration = mDuration * 86400 + other.mDuration; + mDaily = false; + } else { + mDuration += other.mDuration + 86400; + } + return *this; } +Duration Duration::operator-() const +{ + return Duration( -mDuration, ( mDaily ? Days : Seconds ) ); +} +Duration &Duration::operator-=( const Duration &duration ) +{ + return operator+=( -duration ); +} + +Duration &Duration::operator*=( int value ) +{ + mDuration *= value; + return *this; +} + +Duration &Duration::operator/=( int value ) +{ + mDuration /= value; + return *this; +} TQDateTime Duration::end( const TQDateTime &start ) const { - return start.addSecs( mSeconds ); + return mDaily ? start.addDays( mDuration ) + : start.addSecs( mDuration ); +} +Duration::Type Duration::type() const +{ + return mDaily ? Days : Seconds; +} + +bool Duration::isDaily() const +{ + return mDaily; } int Duration::asSeconds() const { - return mSeconds; + return seconds(); +} + +int Duration::asDays() const +{ + return mDaily ? mDuration : mDuration / 86400; +} + +int Duration::value() const +{ + return mDuration; } |