kalarm/lib

datetime.h

00001 /*
00002  *  datetime.h  -  date/time representation with optional date-only value
00003  *  Program:  kalarm
00004  *  Copyright (C) 2003, 2005 by David Jarvie <software@astrojar.org.uk>
00005  *
00006  *  This program is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; either version 2 of the License, or
00009  *  (at your option) any later version.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License along
00017  *  with this program; if not, write to the Free Software Foundation, Inc.,
00018  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00019  */
00020 #ifndef DATETIME_H
00021 #define DATETIME_H
00022 
00023 #include <qdatetime.h>
00024 
00025 
00039 class DateTime
00040 {
00041     public:
00045         DateTime()                     : mDateOnly(false), mTimeValid(false) { }
00047         DateTime(const QDate& d)       : mDateTime(d), mDateOnly(true) { }
00049         DateTime(const QDate& d, const QTime& t)
00050                                        : mDateTime(d, t), mDateOnly(false), mTimeValid(true) { }
00055         DateTime(const QDateTime& dt, bool dateOnly = false)
00056                                        : mDateTime(dt), mDateOnly(dateOnly), mTimeValid(true)
00057                                        { if (dateOnly) mDateTime.setTime(QTime()); }
00061         DateTime& operator=(const DateTime& dt)
00062                                        { mDateTime = dt.mDateTime;  mDateOnly = dt.mDateOnly;  mTimeValid = dt.mTimeValid;  return *this; }
00066         DateTime& operator=(const QDateTime& dt)
00067                                        { mDateTime = dt;  mDateOnly = false;  mTimeValid = true;  return *this; }
00071         DateTime& operator=(const QDate& d)
00072                                        { mDateTime.setDate(d);  mDateOnly = true;  return *this; }
00074         bool      isNull() const       { return mDateTime.date().isNull()  &&  (mDateOnly || mDateTime.time().isNull()); }
00076         bool      isValid() const      { return mDateTime.date().isValid()  &&  (mDateOnly || mTimeValid && mDateTime.time().isValid()); }
00078         bool      isDateOnly() const   { return mDateOnly; }
00082         void      setDateOnly(bool d)  { if (d) mDateTime.setTime(QTime());
00083                                          else if (mDateOnly) mTimeValid = false;
00084                                          mDateOnly = d;
00085                                        }
00087         QDate     date() const         { return mDateTime.date(); }
00091         QTime     time() const;
00096         QDateTime dateTime() const;
00101         void      set(const QDateTime& dt, bool dateOnly = false)
00102                 {
00103                     mDateTime = dt;
00104                     mDateOnly = dateOnly;
00105                     if (dateOnly)
00106                         mDateTime.setTime(QTime());
00107                     mTimeValid = true;
00108                 }
00110         void      set(const QDate& d, const QTime& t)
00111                                        { mDateTime.setDate(d);  mDateTime.setTime(t);  mDateOnly = false;  mTimeValid = true; }
00115         void      setTime(const QTime& t)  { mDateTime.setTime(t);  mDateOnly = false;  mTimeValid = true; }
00120         void      setTime_t(uint secs)     { mDateTime.setTime_t(secs);  mDateOnly = false;  mTimeValid = true; }
00125         DateTime  addSecs(int n) const
00126                 {
00127                     if (mDateOnly)
00128                         return DateTime(mDateTime.date().addDays(n / (3600*24)), true);
00129                     else
00130                         return DateTime(mDateTime.addSecs(n), false);
00131                 }
00136         DateTime  addMins(int n) const
00137                 {
00138                     if (mDateOnly)
00139                         return DateTime(mDateTime.addDays(n / (60*24)), true);
00140                     else
00141                         return DateTime(mDateTime.addSecs(n * 60), false);
00142                 }
00144         DateTime  addDays(int n) const    { return DateTime(mDateTime.addDays(n), mDateOnly); }
00146         DateTime  addMonths(int n) const  { return DateTime(mDateTime.addMonths(n), mDateOnly); }
00148         DateTime  addYears(int n) const   { return DateTime(mDateTime.addYears(n), mDateOnly); }
00150         int       daysTo(const DateTime& dt) const
00151                                           { return (mDateOnly || dt.mDateOnly) ? mDateTime.date().daysTo(dt.date()) : mDateTime.daysTo(dt.mDateTime); }
00156         int       minsTo(const DateTime& dt) const
00157                                           { return (mDateOnly || dt.mDateOnly) ? mDateTime.date().daysTo(dt.date()) * 24*60 : mDateTime.secsTo(dt.mDateTime) / 60; }
00162         int       secsTo(const DateTime& dt) const
00163                                           { return (mDateOnly || dt.mDateOnly) ? mDateTime.date().daysTo(dt.date()) * 24*3600 : mDateTime.secsTo(dt.mDateTime); }
00168         QString   toString(Qt::DateFormat f = Qt::TextDate) const
00169                 {
00170                     if (mDateOnly)
00171                         return mDateTime.date().toString(f);
00172                     else if (mTimeValid)
00173                         return mDateTime.toString(f);
00174                     else
00175                         return QString::null;
00176                 }
00181         QString   toString(const QString& format) const
00182                 {
00183                     if (mDateOnly)
00184                         return mDateTime.date().toString(format);
00185                     else if (mTimeValid)
00186                         return mDateTime.toString(format);
00187                     else
00188                         return QString::null;
00189                 }
00194         QString   formatLocale(bool shortFormat = true) const;
00198         static void setStartOfDay(const QTime& sod)  { mStartOfDay = sod; }
00200         static QTime startOfDay()   { return mStartOfDay; }
00201 
00202         friend bool operator==(const DateTime& dt1, const DateTime& dt2);
00203         friend bool operator<(const DateTime& dt1, const DateTime& dt2);
00204 
00205     private:
00206         static QTime mStartOfDay;
00207         QDateTime    mDateTime;
00208         bool         mDateOnly;
00209         bool         mTimeValid;    // whether the time is potentially valid - applicable only if mDateOnly false
00210 };
00211 
00213 bool operator==(const DateTime& dt1, const DateTime& dt2);
00218 bool operator<(const DateTime& dt1, const DateTime& dt2);
00220 inline bool operator!=(const DateTime& dt1, const DateTime& dt2)   { return !operator==(dt1, dt2); }
00225 inline bool operator>(const DateTime& dt1, const DateTime& dt2)    { return operator<(dt2, dt1); }
00230 inline bool operator>=(const DateTime& dt1, const DateTime& dt2)   { return !operator<(dt1, dt2); }
00235 inline bool operator<=(const DateTime& dt1, const DateTime& dt2)   { return !operator<(dt2, dt1); }
00236 
00237 #endif // DATETIME_H
KDE Home | KDE Accessibility Home | Description of Access Keys