summaryrefslogtreecommitdiffstats
path: root/libkcal/duration.h
blob: 9cddf6958993ab705209ab963b66c8dbf0509559 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/*
    This file is part of libkcal.

    Copyright (c) 2001-2003 Cornelius Schumacher <schumacher@kde.org>

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301, USA.
*/
#ifndef KCAL_DURATION_H
#define KCAL_DURATION_H

#include <tqdatetime.h>

#include "libkcal_export.h"

namespace KCal {

/**
  This class represents a duration.
*/
class LIBKCAL_EXPORT Duration
{
  public:
    /**
      The unit of time used to define the duration.
    */
    enum Type {
      Seconds,   /**< duration is a number of seconds */
      Days       /**< duration is a number of days */
    };

      /**
      Constructs a duration of 0 seconds.
    */
    Duration();

    /**
      Constructs a duration from @p start to @p end.

      If the time of day in @p start and @p end is equal, and their time
      specifications (i.e. time zone etc.) are the same, the duration will be
      set in terms of days. Otherwise, the duration will be set in terms of
      seconds.

      @param start is the time the duration begins.
      @param end is the time the duration ends.
    */
    Duration( const TQDateTime &start, const TQDateTime &end );

    /**
      Constructs a duration from @p start to @p end.

      If @p type is Days, and the time of day in @p start's time zone differs
      between @p start and @p end, the duration will be rounded down to the
      nearest whole number of days.

      @param start is the time the duration begins.
      @param end is the time the duration ends.
      @param type the unit of time to use (seconds or days)
    */
    Duration( const TQDateTime &start, const TQDateTime &end, Type type );

    /**
      Constructs a duration with a number of seconds or days.

      @param duration the number of seconds or days in the duration
      @param type the unit of time to use (seconds or days)
    */
    Duration( int duration, Type type = Seconds ); //krazy:exclude=explicit

    /**
      Constructs a duration by copying another duration object.

      @param duration is the duration to copy.
    */
    Duration( const Duration &duration );

    /**
      Sets this duration equal to @p duration.

      @param duration is the duration to copy.
    */
    Duration &operator=( const Duration &duration );

    /**
      Returns true if this duration is non-zero.
    */
    operator bool() const;

    /**
      Returns true if this duration is zero.
    */
    bool operator!() const  { return !operator bool(); }

    /**
      Returns true if this duration is smaller than the @p other.
      @param other is the other duration to compare.
    */
    bool operator<( const Duration &other ) const;

    /**
      Returns true if this duration is smaller than or equal to the @p other.
      @param other is the other duration to compare.
    */
    bool operator<=( const Duration &other ) const
    { return !other.operator<( *this ); }


    /**
      Returns true if this duration is greater than the @p other.
      @param other is the other duration to compare.
    */
    bool operator>( const Duration &other ) const
    { return other.operator<( *this ); }

    /**
      Returns true if this duration is greater than or equal to the @p other.
      @param other is the other duration to compare.
    */
    bool operator>=( const Duration &other ) const
    { return !operator<( other ); }

    /**
      Returns true if this duration is equal to the @p other.
      Daily and non-daily durations are always considered unequal, since a
      day's duration may differ from 24 hours if it happens to span a daylight
      saving time change.
      @param other the other duration to compare
    */
    bool operator==( const Duration &other ) const;

    /**
      Returns true if this duration is not equal to the @p other.
      Daily and non-daily durations are always considered unequal, since a
      day's duration may differ from 24 hours if it happens to span a daylight
      saving time change.
      @param other is the other duration to compare.
    */
    bool operator!=( const Duration &other ) const
    { return !operator==( other ); }

    /**
      Adds another duration to this one.
      If one is in terms of days and the other in terms of seconds,
      the result is in terms of seconds.
      @param other the other duration to add
    */
    Duration &operator+=( const Duration &other );

    /**
      Adds two durations.
      If one is in terms of days and the other in terms of seconds,
      the result is in terms of seconds.

      @param other the other duration to add
      @return combined duration
    */
    Duration operator+( const Duration &other ) const
    { return Duration( *this ) += other; }

    /**
      Returns the negative of this duration.
    */
    Duration operator-() const;

    /**
      Subtracts another duration from this one.
      If one is in terms of days and the other in terms of seconds,
      the result is in terms of seconds.

      @param other the other duration to subtract
    */
    Duration &operator-=( const Duration &other );

    /**
      Returns the difference between another duration and this.
      If one is in terms of days and the other in terms of seconds,
      the result is in terms of seconds.

      @param other the other duration to subtract
      @return difference in durations
    */
    Duration operator-( const Duration &other ) const
    { return Duration( *this ) += other; }

    /**
      Multiplies this duration by a value.
      @param value value to multiply by
    */
    Duration &operator*=( int value );

    /**
      Multiplies a duration by a value.

      @param value value to multiply by
      @return resultant duration
    */
    Duration operator*( int value ) const
    { return Duration( *this ) *= value; }

    /**
      Divides this duration by a value.
      @param value value to divide by
    */
    Duration &operator/=( int value );

    /**
      Divides a duration by a value.

      @param value value to divide by
      @return resultant duration
    */
    Duration operator/( int value ) const
    { return Duration( *this ) /= value; }

    /**
      Computes a duration end time by adding the number of seconds or
      days in the duration to the specified @p start time.

      @param start is the start time.
      @return end time.
    */
    TQDateTime end( const TQDateTime &start ) const;

    /**
      Returns the time units (seconds or days) used to specify the duration.
    */
    Type type() const;

    /**
      Returns whether the duration is specified in terms of days rather
      than seconds.
    */
    bool isDaily() const;

    /**
      Returns the length of the duration in seconds.
    */
    int asSeconds() const;

    /**
      Returns the length of the duration in days. If the duration is
      not an exact number of days, it is rounded down to return the
      number of whole days.
    */
    int asDays() const;

    /**
      Returns the length of the duration in seconds or days.

      @return if isDaily(), duration in days, else duration in seconds
    */
    int value() const;

  private:
    int seconds() const { return mDaily ? mDuration * 86400 : mDuration; }
    int mDuration;
    bool mDaily;

    class Private;
    Private *d;
};

}

#endif