libkcal

duration.cpp
1/*
2 This file is part of libkcal.
3
4 Copyright (c) 2001 Cornelius Schumacher <schumacher@kde.org>
5
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public
8 License as published by the Free Software Foundation; either
9 version 2 of the License, or (at your option) any later version.
10
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
15
16 You should have received a copy of the GNU Library General Public License
17 along with this library; see the file COPYING.LIB. If not, write to
18 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 Boston, MA 02110-1301, USA.
20*/
21
22#include "duration.h"
23
24using namespace KCal;
25
26Duration::Duration()
27{
28 mDuration = 0;
29}
30
31Duration::Duration( const TQDateTime &start, const TQDateTime &end )
32{
33 if ( start.time() == end.time() ) {
34 mDuration = start.daysTo( end );
35 mDaily = true;
36 } else {
37 mDuration = start.secsTo( end );
38 mDaily = false;
39 }
40}
41
42Duration::Duration( const TQDateTime &start, const TQDateTime &end, Type type )
43{
44 if ( type == Days ) {
45 mDuration = start.daysTo( end );
46 if ( mDuration ) {
47 // Round down to whole number of days if necessary
48 if ( start < end ) {
49 if ( end.time() < start.time() ) {
50 --mDuration;
51 }
52 } else {
53 if ( end.time() > start.time() ) {
54 ++mDuration;
55 }
56 }
57 }
58 mDaily = true;
59 } else {
60 mDuration = start.secsTo( end );
61 mDaily = false;
62 }
63}
64
65Duration::Duration( int duration, Type type )
66{
67 mDuration = duration;
68 mDaily = ( type == Days );
69}
70
71Duration::Duration( const Duration &duration )
72{
73 mDuration = duration.mDuration;
74 mDaily = duration.mDaily;
75}
76
78{
79 // check for self assignment
80 if ( &duration == this ) {
81 return *this;
82 }
83
84 mDuration = duration.mDuration;
85 mDaily = duration.mDaily;
86
87 return *this;
88}
89
90Duration::operator bool() const
91{
92 return mDuration;
93}
94
95bool Duration::operator<( const Duration &other ) const
96{
97 if ( mDaily == other.mDaily ) {
98 // guard against integer overflow for two daily durations
99 return mDuration < other.mDuration;
100 }
101 return seconds() < other.seconds();
102}
103
104bool Duration::operator==( const Duration &other ) const
105{
106 // Note: daily and non-daily durations are always unequal, since a day's
107 // duration may differ from 24 hours if it happens to span a daylight saving
108 // time change.
109 return
110 mDuration == other.mDuration &&
111 mDaily == other.mDaily;
112}
113
114
116{
117 if ( mDaily == other.mDaily ) {
118 mDuration += other.mDuration;
119 } else if ( mDaily ) {
120 mDuration = mDuration * 86400 + other.mDuration;
121 mDaily = false;
122 } else {
123 mDuration += other.mDuration + 86400;
124 }
125 return *this;
126}
127
129{
130 return Duration( -mDuration, ( mDaily ? Days : Seconds ) );
131}
132
134{
135 return operator+=( -duration );
136}
137
139{
140 mDuration *= value;
141 return *this;
142}
143
145{
146 mDuration /= value;
147 return *this;
148}
149
150TQDateTime Duration::end( const TQDateTime &start ) const
151{
152 return mDaily ? start.addDays( mDuration )
153 : start.addSecs( mDuration );
154}
156{
157 return mDaily ? Days : Seconds;
158}
159
161{
162 return mDaily;
163}
164
166{
167 return seconds();
168}
169
171{
172 return mDaily ? mDuration : mDuration / 86400;
173}
174
176{
177 return mDuration;
178}
This class represents a duration.
Definition: duration.h:34
bool operator==(const Duration &other) const
Returns true if this duration is equal to the other.
Definition: duration.cpp:104
Duration & operator=(const Duration &duration)
Sets this duration equal to duration.
Definition: duration.cpp:77
TQDateTime end(const TQDateTime &start) const
Computes a duration end time by adding the number of seconds or days in the duration to the specified...
Definition: duration.cpp:150
Duration()
Constructs a duration of 0 seconds.
Definition: duration.cpp:26
bool isDaily() const
Returns whether the duration is specified in terms of days rather than seconds.
Definition: duration.cpp:160
int asDays() const
Returns the length of the duration in days.
Definition: duration.cpp:170
Duration operator-() const
Returns the negative of this duration.
Definition: duration.cpp:128
int value() const
Returns the length of the duration in seconds or days.
Definition: duration.cpp:175
Type type() const
Returns the time units (seconds or days) used to specify the duration.
Definition: duration.cpp:155
Duration & operator/=(int value)
Divides this duration by a value.
Definition: duration.cpp:144
Duration & operator+=(const Duration &other)
Adds another duration to this one.
Definition: duration.cpp:115
int asSeconds() const
Returns the length of the duration in seconds.
Definition: duration.cpp:165
bool operator<(const Duration &other) const
Returns true if this duration is smaller than the other.
Definition: duration.cpp:95
Type
The unit of time used to define the duration.
Definition: duration.h:39
@ Seconds
duration is a number of seconds
Definition: duration.h:40
@ Days
duration is a number of days
Definition: duration.h:41
Duration & operator-=(const Duration &other)
Subtracts another duration from this one.
Definition: duration.cpp:133
Duration & operator*=(int value)
Multiplies this duration by a value.
Definition: duration.cpp:138
Namespace KCal is for global classes, objects and/or functions in libkcal.
Definition: alarm.h:38