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 <[email protected]>
5  SPDX-FileCopyrightText: 2007 David Jarvie <[email protected]>
6 
7  SPDX-License-Identifier: LGPL-2.0-or-later
8 */
20 #include "duration.h"
21 
22 #include <QDateTime>
23 #include <QTimeZone>
24 
25 using namespace KCalendarCore;
26 
31 //@cond PRIVATE
32 class Q_DECL_HIDDEN KCalendarCore::Duration::Private
33 {
34 public:
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 
87  : d(new KCalendarCore::Duration::Private())
88 {
89  d->mDuration = duration;
90  d->mDaily = (type == Days);
91 }
92 
93 Duration::Duration(const Duration &duration)
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 
114 Duration::operator bool() const
115 {
116  return d->mDuration;
117 }
118 
119 bool 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 
128 bool 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 &&
134  d->mDaily == other.d->mDaily;
135 }
136 
138 {
139  if (d->mDaily == other.d->mDaily) {
140  d->mDuration += other.d->mDuration;
141  } else if (d->mDaily) {
142  d->mDuration = d->mDuration * 86400 + other.d->mDuration;
143  d->mDaily = false;
144  } else {
145  d->mDuration += other.d->mDuration + 86400;
146  }
147  return *this;
148 }
149 
151 {
152  return Duration(-d->mDuration, (d->mDaily ? Days : Seconds));
153 }
154 
156 {
157  return operator+=(-duration);
158 }
159 
161 {
162  d->mDuration *= value;
163  return *this;
164 }
165 
167 {
168  d->mDuration /= value;
169  return *this;
170 }
171 
172 QDateTime Duration::end(const QDateTime &start) const
173 {
174  return d->mDaily ? start.addDays(d->mDuration)
175  : start.addSecs(d->mDuration);
176 }
177 
179 {
180  return d->mDaily ? Days : Seconds;
181 }
182 
183 bool Duration::isDaily() const
184 {
185  return d->mDaily;
186 }
187 
189 {
190  return d->seconds();
191 }
192 
193 int Duration::asDays() const
194 {
195  return d->mDaily ? d->mDuration : d->mDuration / 86400;
196 }
197 
198 int Duration::value() const
199 {
200  return d->mDuration;
201 }
202 
203 bool Duration::isNull() const
204 {
205  return d->mDuration == 0;
206 }
207 
209 {
210  out << duration.d->mDuration << duration.d->mDaily;
211  return out;
212 }
213 
215 {
216  in >> duration.d->mDuration >> duration.d->mDaily;
217  return in;
218 }
KCALENDARCORE_EXPORT QDataStream & operator<<(QDataStream &out, const KCalendarCore::Alarm::Ptr &)
Alarm serializer.
Definition: alarm.cpp:825
Duration()
Constructs a duration of 0 seconds.
Definition: duration.cpp:44
qint64 daysTo(const QDateTime &other) const const
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:172
Duration operator-() const
Returns the negative of this duration.
Definition: duration.cpp:150
KCALENDARCORE_EXPORT QDataStream & operator>>(QDataStream &in, const KCalendarCore::Alarm::Ptr &)
Alarm deserializer.
Definition: alarm.cpp:849
QTime time() const const
int value() const
Returns the length of the duration in seconds or days.
Definition: duration.cpp:198
Duration & operator*=(int value)
Multiplies this duration by a value.
Definition: duration.cpp:160
int asDays() const
Returns the length of the duration in days.
Definition: duration.cpp:193
Type type() const
Returns the time units (seconds or days) used to specify the duration.
Definition: duration.cpp:178
Duration & operator/=(int value)
Divides this duration by a value.
Definition: duration.cpp:166
QDateTime toTimeZone(const QTimeZone &timeZone) const const
Represents a span of time measured in seconds or days.
Definition: duration.h:44
bool isDaily() const
Returns whether the duration is specified in terms of days rather than seconds.
Definition: duration.cpp:183
~Duration()
Destroys a duration.
Definition: duration.cpp:98
Duration & operator=(const Duration &duration)
Sets this duration equal to duration.
Definition: duration.cpp:103
bool isNull() const
Returns true if the duration is 0 seconds.
Definition: duration.cpp:203
Duration & operator+=(const Duration &other)
Adds another duration to this one.
Definition: duration.cpp:137
This file is part of the API for handling calendar data and defines the Duration class.
duration is a number of seconds
Definition: duration.h:51
qint64 secsTo(const QDateTime &other) const const
duration is a number of days
Definition: duration.h:52
Type
The unit of time used to define the duration.
Definition: duration.h:50
int asSeconds() const
Returns the length of the duration in seconds.
Definition: duration.cpp:188
bool operator==(const Duration &other) const
Returns true if this duration is equal to the other.
Definition: duration.cpp:128
QDateTime addSecs(qint64 s) const const
bool operator<(const Duration &other) const
Returns true if this duration is smaller than the other.
Definition: duration.cpp:119
QDateTime addDays(qint64 ndays) const const
Duration & operator-=(const Duration &other)
Subtracts another duration from this one.
Definition: duration.cpp:155
Namespace for all KCalendarCore types.
Definition: alarm.h:36
QTimeZone timeZone() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Nov 30 2020 22:51:31 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.