summaryrefslogtreecommitdiff
path: root/kcalcore/duration.cpp
blob: 0267db93664549fa9bf9c9268e49238b8fa66538 (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
/*
  This file is part of the kcalcore library.

  Copyright (c) 2001 Cornelius Schumacher <schumacher@kde.org>
  Copyright (c) 2007 David Jarvie <djarvie@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.
*/
/**
  @file
  This file is part of the API for handling calendar data and
  defines the Duration class.

  @brief
  Represents a span of time measured in seconds.

  @author Cornelius Schumacher \<schumacher@kde.org\>
  @author David Jarvie \<software@astrojar.org.uk\>
*/
#include "duration.h"
#include <KDateTime>

#include <QTime>

using namespace KCalCore;

/**
  Private class that helps to provide binary compatibility between releases.
  @internal
*/
//@cond PRIVATE
class KCalCore::Duration::Private
{
public:
    int seconds() const {
        return mDaily ? mDuration * 86400 : mDuration;
    }
    int mDuration; // number of seconds or days in the duration
    bool mDaily;   // specified in terms of days rather than seconds
};
//@endcond

Duration::Duration()
    : d(new KCalCore::Duration::Private())
{
}

Duration::Duration(const KDateTime &start, const KDateTime &end)
    : d(new KCalCore::Duration::Private())
{
    if (start.time() == end.time() && start.timeSpec() == end.timeSpec()) {
        d->mDuration = start.daysTo(end);
        d->mDaily = true;
    } else {
        d->mDuration = start.secsTo(end);
        d->mDaily = false;
    }
}

Duration::Duration(const KDateTime &start, const KDateTime &end, Type type)
    : d(new KCalCore::Duration::Private())
{
    if (type == Days) {
        KDateTime endSt(end.toTimeSpec(start));
        d->mDuration = start.daysTo(endSt);
        if (d->mDuration) {
            // Round down to whole number of days if necessary
            if (start < endSt) {
                if (endSt.time() < start.time()) {
                    --d->mDuration;
                }
            } else {
                if (endSt.time() > start.time()) {
                    ++d->mDuration;
                }
            }
        }
        d->mDaily = true;
    } else {
        d->mDuration = start.secsTo(end);
        d->mDaily = false;
    }
}

Duration::Duration(int duration, Type type)
    : d(new KCalCore::Duration::Private())
{
    d->mDuration = duration;
    d->mDaily = (type == Days);
}

Duration::Duration(const Duration &duration)
    : d(new KCalCore::Duration::Private(*duration.d))
{
}

Duration::~Duration()
{
    delete d;
}

Duration &Duration::operator=(const Duration &duration)
{
    // check for self assignment
    if (&duration == this) {
        return *this;
    }

    *d = *duration.d;
    return *this;
}

Duration::operator bool() const
{
    return d->mDuration;
}

bool Duration::operator<(const Duration &other) const
{
    if (d->mDaily == other.d->mDaily) {
        // guard against integer overflow for two daily durations
        return d->mDuration < other.d->mDuration;
    }
    return d->seconds() < other.d->seconds();
}

bool Duration::operator==(const Duration &other) const
{
    // 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 d->mDuration == other.d->mDuration &&
           d->mDaily == other.d->mDaily;
}

Duration &Duration::operator+=(const Duration &other)
{
    if (d->mDaily == other.d->mDaily) {
        d->mDuration += other.d->mDuration;
    } else if (d->mDaily) {
        d->mDuration = d->mDuration * 86400 + other.d->mDuration;
        d->mDaily = false;
    } else {
        d->mDuration += other.d->mDuration + 86400;
    }
    return *this;
}

Duration Duration::operator-() const
{
    return Duration(-d->mDuration, (d->mDaily ? Days : Seconds));
}

Duration &Duration::operator-=(const Duration &duration)
{
    return operator+=(-duration);
}

Duration &Duration::operator*=(int value)
{
    d->mDuration *= value;
    return *this;
}

Duration &Duration::operator/=(int value)
{
    d->mDuration /= value;
    return *this;
}

KDateTime Duration::end(const KDateTime &start) const
{
    return d->mDaily ? start.addDays(d->mDuration)
           : start.addSecs(d->mDuration);
}

Duration::Type Duration::type() const
{
    return d->mDaily ? Days : Seconds;
}

bool Duration::isDaily() const
{
    return d->mDaily;
}

int Duration::asSeconds() const
{
    return d->seconds();
}

int Duration::asDays() const
{
    return d->mDaily ? d->mDuration : d->mDuration / 86400;
}

int Duration::value() const
{
    return d->mDuration;
}

QDataStream &KCalCore::operator<<(QDataStream &out, const KCalCore::Duration &duration)
{
    out << duration.d->mDuration << duration.d->mDaily;
    return out;
}

QDataStream &KCalCore::operator>>(QDataStream &in, KCalCore::Duration &duration)
{
    in >> duration.d->mDuration >> duration.d->mDaily;
    return in;
}