• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdepimlibs-4.10.5 API Reference
  • KDE Home
  • Contact Us
 

KAlarm Library

  • kalarmcal
datetime.cpp
1 /*
2  * datetime.cpp - date/time with start-of-day time for date-only values
3  * This file is part of kalarmcal library, which provides access to KAlarm
4  * calendar data.
5  * Copyright © 2003,2005-2007,2009-2011 by David Jarvie <djarvie@kde.org>
6  *
7  * This library is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU Library General Public License as published
9  * by the Free Software Foundation; either version 2 of the License, or (at
10  * your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
15  * License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public License
18  * along with this library; see the file COPYING.LIB. If not, write to the
19  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
20  * MA 02110-1301, USA.
21  */
22 #include "datetime.h"
23 
24 #include <kglobal.h>
25 #include <klocale.h>
26 
27 namespace KAlarmCal
28 {
29 
30 class DateTime::Private
31 {
32  public:
33  Private() {}
34  Private(const QDate& d, const KDateTime::Spec& spec) : mDateTime(d, spec) {}
35  Private(const QDate& d, const QTime& t, const KDateTime::Spec& spec) : mDateTime(d, t, spec) {}
36  Private(const QDateTime& dt, const KDateTime::Spec& spec) : mDateTime(dt, spec) {}
37  Private(const KDateTime& dt) : mDateTime(dt) {}
38 
39  static QTime mStartOfDay;
40  KDateTime mDateTime;
41 };
42 
43 QTime DateTime::Private::mStartOfDay;
44 
45 DateTime::DateTime()
46  : d(new Private)
47 {
48 }
49 
50 DateTime::DateTime(const QDate& d, const KDateTime::Spec& spec)
51  : d(new Private(d, spec))
52 {
53 }
54 
55 DateTime::DateTime(const QDate& d, const QTime& t, const KDateTime::Spec& spec)
56  : d(new Private(d, t, spec))
57 {
58 }
59 
60 DateTime::DateTime(const QDateTime& dt, const KDateTime::Spec& spec)
61  : d(new Private(dt, spec))
62 {
63 }
64 
65 DateTime::DateTime(const KDateTime& dt)
66  : d(new Private(dt))
67 {
68 }
69 
70 DateTime::DateTime(const DateTime& dt)
71  : d(new Private(*dt.d))
72 {
73 }
74 
75 DateTime::~DateTime()
76 {
77  delete d;
78 }
79 
80 DateTime& DateTime::operator=(const DateTime& dt)
81 {
82  if (&dt != this)
83  *d = *dt.d;
84  return *this;
85 }
86 
87 DateTime& DateTime::operator=(const KDateTime& dt)
88 {
89  d->mDateTime = dt;
90  return *this;
91 }
92 
93 bool DateTime::isNull() const
94 {
95  return d->mDateTime.isNull();
96 }
97 
98 bool DateTime::isValid() const
99 {
100  return d->mDateTime.isValid();
101 }
102 
103 bool DateTime::isDateOnly() const
104 {
105  return d->mDateTime.isDateOnly();
106 }
107 
108 void DateTime::setDateOnly(bool dateOnly)
109 {
110  d->mDateTime.setDateOnly(dateOnly);
111 }
112 
113 QDate DateTime::date() const
114 {
115  return d->mDateTime.date();
116 }
117 
118 void DateTime::setDate(const QDate& date)
119 {
120  d->mDateTime.setDate(date);
121 }
122 
123 QDateTime DateTime::rawDateTime() const
124 {
125  return d->mDateTime.dateTime();
126 }
127 
128 KDateTime DateTime::kDateTime() const
129 {
130  return d->mDateTime;
131 }
132 
133 QTime DateTime::effectiveTime() const
134 {
135  return d->mDateTime.isDateOnly() ? d->mStartOfDay : d->mDateTime.time();
136 }
137 
138 void DateTime::setTime(const QTime& t)
139 {
140  d->mDateTime.setTime(t);
141 }
142 
143 QDateTime DateTime::effectiveDateTime() const
144 {
145  if (d->mDateTime.isDateOnly())
146  {
147  QDateTime dt = d->mDateTime.dateTime(); // preserve Qt::UTC or Qt::LocalTime
148  dt.setTime(d->mStartOfDay);
149  return dt;
150  }
151  return d->mDateTime.dateTime();
152 }
153 
154 void DateTime::setDateTime(const QDateTime& dt)
155 {
156  d->mDateTime.setDateTime(dt);
157 }
158 
159 KDateTime DateTime::effectiveKDateTime() const
160 {
161  if (d->mDateTime.isDateOnly())
162  {
163  KDateTime dt = d->mDateTime;
164  dt.setTime(d->mStartOfDay);
165  return dt;
166  }
167  return d->mDateTime;
168 }
169 
170 KDateTime DateTime::calendarKDateTime() const
171 {
172  if (d->mDateTime.isDateOnly())
173  {
174  KDateTime dt = d->mDateTime;
175  dt.setTime(QTime(0, 0));
176  return dt;
177  }
178  return d->mDateTime;
179 }
180 
181 KTimeZone DateTime::timeZone() const
182 {
183  return d->mDateTime.timeZone();
184 }
185 
186 KDateTime::Spec DateTime::timeSpec() const
187 {
188  return d->mDateTime.timeSpec();
189 }
190 
191 void DateTime::setTimeSpec(const KDateTime::Spec &spec)
192 {
193  d->mDateTime.setTimeSpec(spec);
194 }
195 
196 KDateTime::SpecType DateTime::timeType() const
197 {
198  return d->mDateTime.timeType();
199 }
200 
201 bool DateTime::isLocalZone() const
202 {
203  return d->mDateTime.isLocalZone();
204 }
205 
206 bool DateTime::isClockTime() const
207 {
208  return d->mDateTime.isClockTime();
209 }
210 
211 bool DateTime::isUtc() const
212 {
213  return d->mDateTime.isUtc();
214 }
215 
216 bool DateTime::isOffsetFromUtc() const
217 {
218  return d->mDateTime.isOffsetFromUtc();
219 }
220 
221 int DateTime::utcOffset() const
222 {
223  return d->mDateTime.utcOffset();
224 }
225 
226 bool DateTime::isSecondOccurrence() const
227 {
228  return d->mDateTime.isSecondOccurrence();
229 }
230 
231 void DateTime::setSecondOccurrence(bool second)
232 {
233  d->mDateTime.setSecondOccurrence(second);
234 }
235 
236 DateTime DateTime::toUtc() const
237 {
238  return DateTime(d->mDateTime.toUtc());
239 }
240 
241 DateTime DateTime::toOffsetFromUtc() const
242 {
243  return DateTime(d->mDateTime.toOffsetFromUtc());
244 }
245 
246 DateTime DateTime::toOffsetFromUtc(int utcOffset) const
247 {
248  return DateTime(d->mDateTime.toOffsetFromUtc(utcOffset));
249 }
250 
251 DateTime DateTime::toLocalZone() const
252 {
253  return DateTime(d->mDateTime.toLocalZone());
254 }
255 
256 DateTime DateTime::toClockTime() const
257 {
258  return DateTime(d->mDateTime.toClockTime());
259 }
260 
261 DateTime DateTime::toZone(const KTimeZone& zone) const
262 {
263  return DateTime(d->mDateTime.toZone(zone));
264 }
265 
266 DateTime DateTime::toTimeSpec(const KDateTime::Spec &spec) const
267 {
268  return DateTime(d->mDateTime.toTimeSpec(spec));
269 }
270 
271 uint DateTime::toTime_t() const
272 {
273  return d->mDateTime.toTime_t();
274 }
275 
276 void DateTime::setTime_t(uint secs)
277 {
278  d->mDateTime.setTime_t(secs);
279 }
280 
281 DateTime DateTime::addSecs(qint64 n) const
282 {
283  return DateTime(d->mDateTime.addSecs(n));
284 }
285 
286 DateTime DateTime::addMins(qint64 n) const
287 {
288  return DateTime(d->mDateTime.addSecs(n * 60));
289 }
290 
291 DateTime DateTime::addDays(int n) const
292 {
293  return DateTime(d->mDateTime.addDays(n));
294 }
295 
296 DateTime DateTime::addMonths(int n) const
297 {
298  return DateTime(d->mDateTime.addMonths(n));
299 }
300 
301 DateTime DateTime::addYears(int n) const
302 {
303  return DateTime(d->mDateTime.addYears(n));
304 }
305 
306 int DateTime::daysTo(const DateTime& dt) const
307 {
308  return d->mDateTime.daysTo(dt.d->mDateTime);
309 }
310 
311 int DateTime::minsTo(const DateTime& dt) const
312 {
313  return d->mDateTime.secsTo(dt.d->mDateTime) / 60;
314 }
315 
316 int DateTime::secsTo(const DateTime& dt) const
317 {
318  return d->mDateTime.secsTo(dt.d->mDateTime);
319 }
320 
321 qint64 DateTime::secsTo_long(const DateTime& dt) const
322 {
323  return d->mDateTime.secsTo_long(dt.d->mDateTime);
324 }
325 
326 QString DateTime::toString(Qt::DateFormat f) const
327 {
328  if (d->mDateTime.isDateOnly())
329  return d->mDateTime.date().toString(f);
330  else
331  return d->mDateTime.dateTime().toString(f);
332 }
333 
334 QString DateTime::toString(const QString& format) const
335 {
336  if (d->mDateTime.isDateOnly())
337  return d->mDateTime.date().toString(format);
338  else
339  return d->mDateTime.dateTime().toString(format);
340 }
341 
342 QString DateTime::formatLocale(bool shortFormat) const
343 {
344  return KGlobal::locale()->formatDateTime(d->mDateTime, (shortFormat ? KLocale::ShortDate : KLocale::LongDate));
345 }
346 
347 void DateTime::setStartOfDay(const QTime& sod)
348 {
349  Private::mStartOfDay = sod;
350 }
351 
352 KDateTime::Comparison DateTime::compare(const DateTime &other) const
353 {
354  return d->mDateTime.compare(other.d->mDateTime);
355 }
356 
357 QTime DateTime::startOfDay()
358 {
359  return Private::mStartOfDay;
360 }
361 
362 bool operator==(const DateTime& dt1, const DateTime& dt2)
363 {
364  return dt1.d->mDateTime == dt2.d->mDateTime;
365 }
366 
367 bool operator==(const KDateTime& dt1, const DateTime& dt2)
368 {
369  return dt1 == dt2.d->mDateTime;
370 }
371 
372 bool operator<(const DateTime& dt1, const DateTime& dt2)
373 {
374  if (dt1.d->mDateTime.isDateOnly() && !dt2.d->mDateTime.isDateOnly())
375  {
376  KDateTime dt = dt1.d->mDateTime.addDays(1);
377  dt.setTime(DateTime::Private::mStartOfDay);
378  return dt <= dt2.d->mDateTime;
379  }
380  if (!dt1.d->mDateTime.isDateOnly() && dt2.d->mDateTime.isDateOnly())
381  {
382  KDateTime dt = dt2.d->mDateTime;
383  dt.setTime(DateTime::Private::mStartOfDay);
384  return dt1.d->mDateTime < dt;
385  }
386  return dt1.d->mDateTime < dt2.d->mDateTime;
387 }
388 
389 } // namespace KAlarmCal
390 
391 // vim: et sw=4:
This file is part of the KDE documentation.
Documentation copyright © 1996-2013 The KDE developers.
Generated on Sat Jul 13 2013 01:30:13 by doxygen 1.8.3.1 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KAlarm Library

Skip menu "KAlarm Library"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • Related Pages

kdepimlibs-4.10.5 API Reference

Skip menu "kdepimlibs-4.10.5 API Reference"
  • akonadi
  •   contact
  •   kmime
  •   socialutils
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • microblog
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal