datetime.h
00001 /* 00002 * datetime.h - date/time representation with optional date-only value 00003 * Program: kalarm 00004 * Copyright © 2003,2005,2007 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 <tqdatetime.h> 00024 00025 00039 class DateTime 00040 { 00041 public: 00045 DateTime() : mDateOnly(false), mTimeValid(false) { } 00047 DateTime(const TQDate& d) : mDateTime(d), mDateOnly(true) { } 00049 DateTime(const TQDate& d, const TQTime& t) 00050 : mDateTime(d, t), mDateOnly(false), mTimeValid(true) { } 00055 DateTime(const TQDateTime& dt, bool dateOnly = false) 00056 : mDateTime(dt), mDateOnly(dateOnly), mTimeValid(true) 00057 { if (dateOnly) mDateTime.setTime(TQTime()); } 00061 DateTime& operator=(const DateTime& dt) 00062 { mDateTime = dt.mDateTime; mDateOnly = dt.mDateOnly; mTimeValid = dt.mTimeValid; return *this; } 00066 DateTime& operator=(const TQDateTime& dt) 00067 { mDateTime = dt; mDateOnly = false; mTimeValid = true; return *this; } 00071 DateTime& operator=(const TQDate& d) 00072 { mDateTime.setDate(d); mDateTime.setTime(TQTime()); 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(TQTime()); 00083 else if (mDateOnly) mTimeValid = false; 00084 mDateOnly = d; 00085 } 00087 TQDate date() const { return mDateTime.date(); } 00091 TQTime time() const; 00096 TQDateTime dateTime() const; 00100 TQDateTime rawDateTime() const { return mDateTime; } 00105 void set(const TQDateTime& dt, bool dateOnly = false) 00106 { 00107 mDateTime = dt; 00108 mDateOnly = dateOnly; 00109 if (dateOnly) 00110 mDateTime.setTime(TQTime()); 00111 mTimeValid = true; 00112 } 00114 void set(const TQDate& d, const TQTime& t) 00115 { mDateTime.setDate(d); mDateTime.setTime(t); mDateOnly = false; mTimeValid = true; } 00119 void setTime(const TQTime& t) { mDateTime.setTime(t); mDateOnly = false; mTimeValid = true; } 00124 void setTime_t(uint secs) { mDateTime.setTime_t(secs); mDateOnly = false; mTimeValid = true; } 00129 DateTime addSecs(int n) const 00130 { 00131 if (mDateOnly) 00132 return DateTime(mDateTime.date().addDays(n / (3600*24)), true); 00133 else 00134 return DateTime(mDateTime.addSecs(n), false); 00135 } 00140 DateTime addMins(int n) const 00141 { 00142 if (mDateOnly) 00143 return DateTime(mDateTime.addDays(n / (60*24)), true); 00144 else 00145 return DateTime(mDateTime.addSecs(n * 60), false); 00146 } 00148 DateTime addDays(int n) const { return DateTime(mDateTime.addDays(n), mDateOnly); } 00150 DateTime addMonths(int n) const { return DateTime(mDateTime.addMonths(n), mDateOnly); } 00152 DateTime addYears(int n) const { return DateTime(mDateTime.addYears(n), mDateOnly); } 00154 int daysTo(const DateTime& dt) const 00155 { return (mDateOnly || dt.mDateOnly) ? mDateTime.date().daysTo(dt.date()) : mDateTime.daysTo(dt.mDateTime); } 00160 int minsTo(const DateTime& dt) const 00161 { return (mDateOnly || dt.mDateOnly) ? mDateTime.date().daysTo(dt.date()) * 24*60 : mDateTime.secsTo(dt.mDateTime) / 60; } 00166 int secsTo(const DateTime& dt) const 00167 { return (mDateOnly || dt.mDateOnly) ? mDateTime.date().daysTo(dt.date()) * 24*3600 : mDateTime.secsTo(dt.mDateTime); } 00172 TQString toString(Qt::DateFormat f = Qt::TextDate) const 00173 { 00174 if (mDateOnly) 00175 return mDateTime.date().toString(f); 00176 else if (mTimeValid) 00177 return mDateTime.toString(f); 00178 else 00179 return TQString(); 00180 } 00185 TQString toString(const TQString& format) const 00186 { 00187 if (mDateOnly) 00188 return mDateTime.date().toString(format); 00189 else if (mTimeValid) 00190 return mDateTime.toString(format); 00191 else 00192 return TQString(); 00193 } 00198 TQString formatLocale(bool shortFormat = true) const; 00202 static void setStartOfDay(const TQTime& sod) { mStartOfDay = sod; } 00204 static TQTime startOfDay() { return mStartOfDay; } 00205 00206 friend bool operator==(const DateTime& dt1, const DateTime& dt2); 00207 friend bool operator<(const DateTime& dt1, const DateTime& dt2); 00208 00209 private: 00210 static TQTime mStartOfDay; 00211 TQDateTime mDateTime; 00212 bool mDateOnly; 00213 bool mTimeValid; // whether the time is potentially valid - applicable only if mDateOnly false 00214 }; 00215 00217 bool operator==(const DateTime& dt1, const DateTime& dt2); 00222 bool operator<(const DateTime& dt1, const DateTime& dt2); 00224 inline bool operator!=(const DateTime& dt1, const DateTime& dt2) { return !operator==(dt1, dt2); } 00229 inline bool operator>(const DateTime& dt1, const DateTime& dt2) { return operator<(dt2, dt1); } 00234 inline bool operator>=(const DateTime& dt1, const DateTime& dt2) { return !operator<(dt1, dt2); } 00239 inline bool operator<=(const DateTime& dt1, const DateTime& dt2) { return !operator<(dt2, dt1); } 00240 00241 #endif // DATETIME_H