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 */
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 <[email protected]>
18  @author David Jarvie <[email protected]>
19 */
20 #include "duration.h"
21 
22 #include <QDateTime>
23 #include <QTimeZone>
24 
25 using namespace KCalendarCore;
26 
27 /**
28  Private class that helps to provide binary compatibility between releases.
29  @internal
30 */
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 
61 Duration::Duration(const QDateTime &start, const QDateTime &end, Type type)
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 
86 Duration::Duration(int duration, Type type)
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 && 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 
181 bool Duration::isDaily() const
182 {
183  return d->mDaily;
184 }
185 
187 {
188  return d->seconds();
189 }
190 
191 int Duration::asDays() const
192 {
193  return d->mDaily ? d->mDuration : d->mDuration / 86400;
194 }
195 
196 int Duration::value() const
197 {
198  return d->mDuration;
199 }
200 
201 bool Duration::isNull() const
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 }
KCALENDARCORE_EXPORT QDataStream & operator<<(QDataStream &out, const KCalendarCore::Alarm::Ptr &)
Alarm serializer.
Definition: alarm.cpp:820
Represents a span of time measured in seconds or days.
Definition: duration.h:43
QTimeZone timeZone() const const
Duration & operator=(const Duration &duration)
Sets this duration equal to duration.
Definition: duration.cpp:103
@ Seconds
duration is a number of seconds
Definition: duration.h:50
Namespace for all KCalendarCore types.
Definition: alarm.h:36
QTime time() const const
KCALENDARCORE_EXPORT QDataStream & operator>>(QDataStream &in, const KCalendarCore::Alarm::Ptr &)
Alarm deserializer.
Definition: alarm.cpp:833
@ Days
duration is a number of days
Definition: duration.h:51
Duration operator-() const
Returns the negative of this duration.
Definition: duration.cpp:149
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 & operator*=(int value)
Multiplies this duration by a value.
Definition: duration.cpp:159
QDateTime toTimeZone(const QTimeZone &timeZone) const const
int asSeconds() const
Returns the length of the duration in seconds.
Definition: duration.cpp:186
Q_SCRIPTABLE Q_NOREPLY void start()
Duration & operator-=(const Duration &other)
Subtracts another duration from this one.
Definition: duration.cpp:154
Type type() const
Returns the time units (seconds or days) used to specify the duration.
Definition: duration.cpp:176
bool operator<(const Duration &other) const
Returns true if this duration is smaller than the other.
Definition: duration.cpp:119
int asDays() const
Returns the length of the duration in days.
Definition: duration.cpp:191
bool operator==(const Duration &other) const
Returns true if this duration is equal to the other.
Definition: duration.cpp:128
bool isNull() const
Returns true if the duration is 0 seconds.
Definition: duration.cpp:201
Type
The unit of time used to define the duration.
Definition: duration.h:49
Duration & operator+=(const Duration &other)
Adds another duration to this one.
Definition: duration.cpp:136
Duration & operator/=(int value)
Divides this duration by a value.
Definition: duration.cpp:165
~Duration()
Destroys a duration.
Definition: duration.cpp:98
bool isDaily() const
Returns whether the duration is specified in terms of days rather than seconds.
Definition: duration.cpp:181
Duration()
Constructs a duration of 0 seconds.
Definition: duration.cpp:44
int value() const
Returns the length of the duration in seconds or days.
Definition: duration.cpp:196
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Fri Sep 22 2023 03:55:21 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.