KCalendarCore

duration.cpp
Go to the documentation of this file.
1/*
2 This file is part of the kcalcore library.
3
4 SPDX-FileCopyrightText: 2001 Cornelius Schumacher <schumacher@kde.org>
5 SPDX-FileCopyrightText: 2007 David Jarvie <djarvie@kde.org>
6
7 SPDX-License-Identifier: LGPL-2.0-or-later
8*/
9/**
10 @file
11 This file is part of the API for handling calendar data and
12 defines the Duration class.
13
14 @brief
15 Represents a span of time measured in seconds.
16
17 @author Cornelius Schumacher <schumacher@kde.org>
18 @author David Jarvie <djarvie@kde.org>
19*/
20#include "duration.h"
21
22#include <QDateTime>
23#include <QTimeZone>
24
25using namespace KCalendarCore;
26
27/**
28 Private class that helps to provide binary compatibility between releases.
29 @internal
30*/
31//@cond PRIVATE
32class Q_DECL_HIDDEN KCalendarCore::Duration::Private
33{
34public:
35 int seconds() const
36 {
37 return mDaily ? mDuration * 86400 : mDuration;
38 }
39 int mDuration; // number of seconds or days in the duration
40 bool mDaily = false; // specified in terms of days rather than seconds
41};
42//@endcond
43
45 : d(new KCalendarCore::Duration::Private())
46{
47}
48
50 : d(new KCalendarCore::Duration::Private())
51{
52 if (start.time() == end.time() && start.timeZone() == end.timeZone()) {
53 d->mDuration = start.daysTo(end);
54 d->mDaily = true;
55 } else {
56 d->mDuration = start.secsTo(end);
57 d->mDaily = false;
58 }
59}
60
62 : d(new KCalendarCore::Duration::Private())
63{
64 if (type == Days) {
65 QDateTime endSt(end.toTimeZone(start.timeZone()));
66 d->mDuration = start.daysTo(endSt);
67 if (d->mDuration) {
68 // Round down to whole number of days if necessary
69 if (start < endSt) {
70 if (endSt.time() < start.time()) {
71 --d->mDuration;
72 }
73 } else {
74 if (endSt.time() > start.time()) {
75 ++d->mDuration;
76 }
77 }
78 }
79 d->mDaily = true;
80 } else {
81 d->mDuration = start.secsTo(end);
82 d->mDaily = false;
83 }
84}
85
86Duration::Duration(int duration, Type type)
87 : d(new KCalendarCore::Duration::Private())
88{
89 d->mDuration = duration;
90 d->mDaily = (type == Days);
91}
92
94 : d(new KCalendarCore::Duration::Private(*duration.d))
95{
96}
97
99{
100 delete d;
101}
102
104{
105 // check for self assignment
106 if (&duration == this) {
107 return *this;
108 }
109
110 *d = *duration.d;
111 return *this;
112}
113
114Duration::operator bool() const
115{
116 return d->mDuration;
117}
118
119bool Duration::operator<(const Duration &other) const
120{
121 if (d->mDaily == other.d->mDaily) {
122 // guard against integer overflow for two daily durations
123 return d->mDuration < other.d->mDuration;
124 }
125 return d->seconds() < other.d->seconds();
126}
127
128bool Duration::operator==(const Duration &other) const
129{
130 // Note: daily and non-daily durations are always unequal, since a day's
131 // duration may differ from 24 hours if it happens to span a daylight saving
132 // time change.
133 return d->mDuration == other.d->mDuration && d->mDaily == other.d->mDaily;
134}
135
137{
138 if (d->mDaily == other.d->mDaily) {
139 d->mDuration += other.d->mDuration;
140 } else if (d->mDaily) {
141 d->mDuration = d->mDuration * 86400 + other.d->mDuration;
142 d->mDaily = false;
143 } else {
144 d->mDuration += other.d->mDuration + 86400;
145 }
146 return *this;
147}
148
150{
151 return Duration(-d->mDuration, (d->mDaily ? Days : Seconds));
152}
153
155{
156 return operator+=(-duration);
157}
158
160{
161 d->mDuration *= value;
162 return *this;
163}
164
166{
167 d->mDuration /= value;
168 return *this;
169}
170
172{
173 return d->mDaily ? start.addDays(d->mDuration) : start.addSecs(d->mDuration);
174}
175
177{
178 return d->mDaily ? Days : Seconds;
179}
180
182{
183 return d->mDaily;
184}
185
187{
188 return d->seconds();
189}
190
192{
193 return d->mDaily ? d->mDuration : d->mDuration / 86400;
194}
195
197{
198 return d->mDuration;
199}
200
202{
203 return d->mDuration == 0;
204}
205
207{
208 out << duration.d->mDuration << duration.d->mDaily;
209 return out;
210}
211
213{
214 in >> duration.d->mDuration >> duration.d->mDaily;
215 return in;
216}
Represents a span of time measured in seconds or days.
Definition duration.h:44
bool operator==(const Duration &other) const
Returns true if this duration is equal to the other.
Definition duration.cpp:128
QDateTime end(const QDateTime &start) const
Computes a duration end time by adding the number of seconds or days in the duration to the specified...
Definition duration.cpp:171
~Duration()
Destroys a duration.
Definition duration.cpp:98
Duration & operator=(const Duration &duration)
Sets this duration equal to duration.
Definition duration.cpp:103
Duration()
Constructs a duration of 0 seconds.
Definition duration.cpp:44
Type
The unit of time used to define the duration.
Definition duration.h:49
@ Days
duration is a number of days
Definition duration.h:51
@ Seconds
duration is a number of seconds
Definition duration.h:50
bool isDaily() const
Returns whether the duration is specified in terms of days rather than seconds.
Definition duration.cpp:181
int asDays() const
Returns the length of the duration in days.
Definition duration.cpp:191
bool isNull() const
Returns true if the duration is 0 seconds.
Definition duration.cpp:201
Duration operator-() const
Returns the negative of this duration.
Definition duration.cpp:149
int value() const
Returns the length of the duration in seconds or days.
Definition duration.cpp:196
Type type() const
Returns the time units (seconds or days) used to specify the duration.
Definition duration.cpp:176
Duration & operator/=(int value)
Divides this duration by a value.
Definition duration.cpp:165
Duration & operator+=(const Duration &other)
Adds another duration to this one.
Definition duration.cpp:136
int asSeconds() const
Returns the length of the duration in seconds.
Definition duration.cpp:186
bool operator<(const Duration &other) const
Returns true if this duration is smaller than the other.
Definition duration.cpp:119
Duration & operator-=(const Duration &other)
Subtracts another duration from this one.
Definition duration.cpp:154
Duration & operator*=(int value)
Multiplies this duration by a value.
Definition duration.cpp:159
This file is part of the API for handling calendar data and defines the Duration class.
Q_SCRIPTABLE Q_NOREPLY void start()
Namespace for all KCalendarCore types.
Definition alarm.h:37
KCALENDARCORE_EXPORT QDataStream & operator>>(QDataStream &in, const KCalendarCore::Alarm::Ptr &)
Alarm deserializer.
Definition alarm.cpp:833
KCALENDARCORE_EXPORT QDataStream & operator<<(QDataStream &out, const KCalendarCore::Alarm::Ptr &)
Alarm serializer.
Definition alarm.cpp:820
QDateTime addDays(qint64 ndays) const const
QDateTime addSecs(qint64 s) const const
QTime time() const const
QTimeZone timeZone() const const
QDateTime toTimeZone(const QTimeZone &timeZone) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:13:47 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.