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

KAlarm Library

datetime.cpp
00001 /*
00002  *  datetime.cpp  -  date/time with start-of-day time for date-only values
00003  *  This file is part of kalarmcal library, which provides access to KAlarm
00004  *  calendar data.
00005  *  Copyright © 2003,2005-2007,2009-2011 by David Jarvie <djarvie@kde.org>
00006  *
00007  *  This library is free software; you can redistribute it and/or modify
00008  *  it under the terms of the GNU Library General Public License as published
00009  *  by the Free Software Foundation; either version 2 of the License, or (at
00010  *  your option) any later version.
00011  *
00012  *  This library is distributed in the hope that it will be useful, but WITHOUT
00013  *  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00014  *  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
00015  *  License for more details.
00016  *
00017  *  You should have received a copy of the GNU Library General Public License
00018  *  along with this library; see the file COPYING.LIB.  If not, write to the
00019  *  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
00020  *  MA 02110-1301, USA.
00021  */
00022 #include "datetime.h"
00023 
00024 #include <kglobal.h>
00025 #include <klocale.h>
00026 
00027 namespace KAlarmCal
00028 {
00029 
00030 class DateTime::Private
00031 {
00032     public:
00033         Private() {}
00034         Private(const QDate& d, const KDateTime::Spec& spec) : mDateTime(d, spec) {}
00035         Private(const QDate& d, const QTime& t, const KDateTime::Spec& spec) : mDateTime(d, t, spec) {}
00036         Private(const QDateTime& dt, const KDateTime::Spec& spec) : mDateTime(dt, spec) {}
00037         Private(const KDateTime& dt) : mDateTime(dt) {}
00038 
00039         static QTime mStartOfDay;
00040         KDateTime    mDateTime;
00041 };
00042 
00043 QTime DateTime::Private::mStartOfDay;
00044 
00045 DateTime::DateTime()
00046     : d(new Private)
00047 {
00048 }
00049 
00050 DateTime::DateTime(const QDate& d, const KDateTime::Spec& spec)
00051     : d(new Private(d, spec))
00052 {
00053 }
00054 
00055 DateTime::DateTime(const QDate& d, const QTime& t, const KDateTime::Spec& spec)
00056     : d(new Private(d, t, spec))
00057 {
00058 }
00059 
00060 DateTime::DateTime(const QDateTime& dt, const KDateTime::Spec& spec)
00061     : d(new Private(dt, spec))
00062 {
00063 }
00064 
00065 DateTime::DateTime(const KDateTime& dt)
00066     : d(new Private(dt))
00067 {
00068 }
00069 
00070 DateTime::DateTime(const DateTime& dt)
00071     : d(new Private(*dt.d))
00072 {
00073 }
00074 
00075 DateTime::~DateTime()
00076 {
00077     delete d;
00078 }
00079 
00080 DateTime& DateTime::operator=(const DateTime& dt)
00081 {
00082     if (&dt != this)
00083         *d = *dt.d;
00084     return *this;
00085 }
00086 
00087 DateTime& DateTime::operator=(const KDateTime& dt)
00088 {
00089     d->mDateTime = dt;
00090     return *this;
00091 }
00092 
00093 bool DateTime::isNull() const
00094 {
00095     return d->mDateTime.isNull();
00096 }
00097 
00098 bool DateTime::isValid() const
00099 {
00100     return d->mDateTime.isValid();
00101 }
00102 
00103 bool DateTime::isDateOnly() const
00104 {
00105     return d->mDateTime.isDateOnly();
00106 }
00107 
00108 void DateTime::setDateOnly(bool dateOnly)
00109 {
00110     d->mDateTime.setDateOnly(dateOnly);
00111 }
00112 
00113 QDate DateTime::date() const
00114 {
00115     return d->mDateTime.date();
00116 }
00117 
00118 void DateTime::setDate(const QDate& date)
00119 {
00120     d->mDateTime.setDate(date);
00121 }
00122 
00123 QDateTime DateTime::rawDateTime() const
00124 {
00125     return d->mDateTime.dateTime();
00126 }
00127 
00128 KDateTime DateTime::kDateTime() const
00129 {
00130     return d->mDateTime;
00131 }
00132 
00133 QTime DateTime::effectiveTime() const
00134 {
00135     return d->mDateTime.isDateOnly() ? d->mStartOfDay : d->mDateTime.time();
00136 }
00137 
00138 void DateTime::setTime(const QTime& t)
00139 {
00140     d->mDateTime.setTime(t);
00141 }
00142 
00143 QDateTime DateTime::effectiveDateTime() const
00144 {
00145     if (d->mDateTime.isDateOnly())
00146     {
00147         QDateTime dt = d->mDateTime.dateTime();    // preserve Qt::UTC or Qt::LocalTime
00148         dt.setTime(d->mStartOfDay);
00149         return dt;
00150     }
00151     return d->mDateTime.dateTime();
00152 }
00153 
00154 void DateTime::setDateTime(const QDateTime& dt)
00155 {
00156     d->mDateTime.setDateTime(dt);
00157 }
00158 
00159 KDateTime DateTime::effectiveKDateTime() const
00160 {
00161     if (d->mDateTime.isDateOnly())
00162     {
00163         KDateTime dt = d->mDateTime;
00164         dt.setTime(d->mStartOfDay);
00165         return dt;
00166     }
00167     return d->mDateTime;
00168 }
00169 
00170 KDateTime DateTime::calendarKDateTime() const
00171 {
00172     if (d->mDateTime.isDateOnly())
00173     {
00174         KDateTime dt = d->mDateTime;
00175         dt.setTime(QTime(0, 0));
00176         return dt;
00177     }
00178     return d->mDateTime;
00179 }
00180 
00181 KTimeZone DateTime::timeZone() const
00182 {
00183     return d->mDateTime.timeZone();
00184 }
00185 
00186 KDateTime::Spec DateTime::timeSpec() const
00187 {
00188     return d->mDateTime.timeSpec();
00189 }
00190 
00191 void DateTime::setTimeSpec(const KDateTime::Spec &spec)
00192 {
00193     d->mDateTime.setTimeSpec(spec);
00194 }
00195 
00196 KDateTime::SpecType DateTime::timeType() const
00197 {
00198     return d->mDateTime.timeType();
00199 }
00200 
00201 bool DateTime::isLocalZone() const
00202 {
00203     return d->mDateTime.isLocalZone();
00204 }
00205 
00206 bool DateTime::isClockTime() const
00207 {
00208     return d->mDateTime.isClockTime();
00209 }
00210 
00211 bool DateTime::isUtc() const
00212 {
00213     return d->mDateTime.isUtc();
00214 }
00215 
00216 bool DateTime::isOffsetFromUtc() const
00217 {
00218     return d->mDateTime.isOffsetFromUtc();
00219 }
00220 
00221 int DateTime::utcOffset() const
00222 {
00223     return d->mDateTime.utcOffset();
00224 }
00225 
00226 bool DateTime::isSecondOccurrence() const
00227 {
00228     return d->mDateTime.isSecondOccurrence();
00229 }
00230 
00231 void DateTime::setSecondOccurrence(bool second)
00232 {
00233     d->mDateTime.setSecondOccurrence(second);
00234 }
00235 
00236 DateTime DateTime::toUtc() const
00237 {
00238     return DateTime(d->mDateTime.toUtc());
00239 }
00240 
00241 DateTime DateTime::toOffsetFromUtc() const
00242 {
00243     return DateTime(d->mDateTime.toOffsetFromUtc());
00244 }
00245 
00246 DateTime DateTime::toOffsetFromUtc(int utcOffset) const
00247 {
00248     return DateTime(d->mDateTime.toOffsetFromUtc(utcOffset));
00249 }
00250 
00251 DateTime DateTime::toLocalZone() const
00252 {
00253     return DateTime(d->mDateTime.toLocalZone());
00254 }
00255 
00256 DateTime DateTime::toClockTime() const
00257 {
00258     return DateTime(d->mDateTime.toClockTime());
00259 }
00260 
00261 DateTime DateTime::toZone(const KTimeZone& zone) const
00262 {
00263     return DateTime(d->mDateTime.toZone(zone));
00264 }
00265 
00266 DateTime DateTime::toTimeSpec(const KDateTime::Spec &spec) const
00267 {
00268     return DateTime(d->mDateTime.toTimeSpec(spec));
00269 }
00270 
00271 uint DateTime::toTime_t() const
00272 {
00273     return d->mDateTime.toTime_t();
00274 }
00275 
00276 void DateTime::setTime_t(uint secs)
00277 {
00278     d->mDateTime.setTime_t(secs);
00279 }
00280 
00281 DateTime DateTime::addSecs(qint64 n) const
00282 {
00283     return DateTime(d->mDateTime.addSecs(n));
00284 }
00285 
00286 DateTime DateTime::addMins(qint64 n) const
00287 {
00288     return DateTime(d->mDateTime.addSecs(n * 60));
00289 }
00290 
00291 DateTime DateTime::addDays(int n) const
00292 {
00293     return DateTime(d->mDateTime.addDays(n));
00294 }
00295 
00296 DateTime DateTime::addMonths(int n) const
00297 {
00298     return DateTime(d->mDateTime.addMonths(n));
00299 }
00300 
00301 DateTime DateTime::addYears(int n) const
00302 {
00303     return DateTime(d->mDateTime.addYears(n));
00304 }
00305 
00306 int DateTime::daysTo(const DateTime& dt) const
00307 {
00308     return d->mDateTime.daysTo(dt.d->mDateTime);
00309 }
00310 
00311 int DateTime::minsTo(const DateTime& dt) const
00312 {
00313     return d->mDateTime.secsTo(dt.d->mDateTime) / 60;
00314 }
00315 
00316 int DateTime::secsTo(const DateTime& dt) const
00317 {
00318     return d->mDateTime.secsTo(dt.d->mDateTime);
00319 }
00320 
00321 qint64 DateTime::secsTo_long(const DateTime& dt) const
00322 {
00323     return d->mDateTime.secsTo_long(dt.d->mDateTime);
00324 }
00325 
00326 QString DateTime::toString(Qt::DateFormat f) const
00327 {
00328     if (d->mDateTime.isDateOnly())
00329         return d->mDateTime.date().toString(f);
00330     else
00331         return d->mDateTime.dateTime().toString(f);
00332 }
00333 
00334 QString DateTime::toString(const QString& format) const
00335 {
00336     if (d->mDateTime.isDateOnly())
00337         return d->mDateTime.date().toString(format);
00338     else
00339         return d->mDateTime.dateTime().toString(format);
00340 }
00341 
00342 QString DateTime::formatLocale(bool shortFormat) const
00343 {
00344     return KGlobal::locale()->formatDateTime(d->mDateTime, (shortFormat ? KLocale::ShortDate : KLocale::LongDate));
00345 }
00346 
00347 void DateTime::setStartOfDay(const QTime& sod)
00348 {
00349     Private::mStartOfDay = sod;
00350 }
00351 
00352 KDateTime::Comparison DateTime::compare(const DateTime &other) const
00353 {
00354     return d->mDateTime.compare(other.d->mDateTime);
00355 }
00356 
00357 QTime DateTime::startOfDay()
00358 {
00359     return Private::mStartOfDay;
00360 }
00361 
00362 bool operator==(const DateTime& dt1, const DateTime& dt2)
00363 {
00364     return dt1.d->mDateTime == dt2.d->mDateTime;
00365 }
00366 
00367 bool operator==(const KDateTime& dt1, const DateTime& dt2)
00368 {
00369     return dt1 == dt2.d->mDateTime;
00370 }
00371 
00372 bool operator<(const DateTime& dt1, const DateTime& dt2)
00373 {
00374     if (dt1.d->mDateTime.isDateOnly()  &&  !dt2.d->mDateTime.isDateOnly())
00375     {
00376         KDateTime dt = dt1.d->mDateTime.addDays(1);
00377         dt.setTime(DateTime::Private::mStartOfDay);
00378         return dt <= dt2.d->mDateTime;
00379     }
00380     if (!dt1.d->mDateTime.isDateOnly()  &&  dt2.d->mDateTime.isDateOnly())
00381     {
00382         KDateTime dt = dt2.d->mDateTime;
00383         dt.setTime(DateTime::Private::mStartOfDay);
00384         return dt1.d->mDateTime < dt;
00385     }
00386     return dt1.d->mDateTime < dt2.d->mDateTime;
00387 }
00388 
00389 } // namespace KAlarmCal
00390 
00391 // vim: et sw=4:
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Mon Apr 30 2012 21:50:08 by doxygen 1.8.0 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.8.3 API Reference

Skip menu "kdepimlibs-4.8.3 API Reference"
  • akonadi
  •   contact
  •   kmime
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  •   richtextbuilders
  • 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