kalarm/lib

datetime.h
1/*
2 * datetime.h - date/time representation with optional date-only value
3 * Program: kalarm
4 * Copyright © 2003,2005,2007 by David Jarvie <software@astrojar.org.uk>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 */
20#ifndef DATETIME_H
21#define DATETIME_H
22
23#include <tqdatetime.h>
24
25
40{
41 public:
45 DateTime() : mDateOnly(false), mTimeValid(false) { }
47 DateTime(const TQDate& d) : mDateTime(d), mDateOnly(true) { }
49 DateTime(const TQDate& d, const TQTime& t)
50 : mDateTime(d, t), mDateOnly(false), mTimeValid(true) { }
55 DateTime(const TQDateTime& dt, bool dateOnly = false)
56 : mDateTime(dt), mDateOnly(dateOnly), mTimeValid(true)
57 { if (dateOnly) mDateTime.setTime(TQTime()); }
62 { mDateTime = dt.mDateTime; mDateOnly = dt.mDateOnly; mTimeValid = dt.mTimeValid; return *this; }
66 DateTime& operator=(const TQDateTime& dt)
67 { mDateTime = dt; mDateOnly = false; mTimeValid = true; return *this; }
71 DateTime& operator=(const TQDate& d)
72 { mDateTime.setDate(d); mDateTime.setTime(TQTime()); mDateOnly = true; return *this; }
74 bool isNull() const { return mDateTime.date().isNull() && (mDateOnly || mDateTime.time().isNull()); }
76 bool isValid() const { return mDateTime.date().isValid() && (mDateOnly || (mTimeValid && mDateTime.time().isValid())); }
78 bool isDateOnly() const { return mDateOnly; }
82 void setDateOnly(bool d) { if (d) mDateTime.setTime(TQTime());
83 else if (mDateOnly) mTimeValid = false;
84 mDateOnly = d;
85 }
87 TQDate date() const { return mDateTime.date(); }
91 TQTime time() const;
96 TQDateTime dateTime() const;
100 TQDateTime rawDateTime() const { return mDateTime; }
105 void set(const TQDateTime& dt, bool dateOnly = false)
106 {
107 mDateTime = dt;
108 mDateOnly = dateOnly;
109 if (dateOnly)
110 mDateTime.setTime(TQTime());
111 mTimeValid = true;
112 }
114 void set(const TQDate& d, const TQTime& t)
115 { mDateTime.setDate(d); mDateTime.setTime(t); mDateOnly = false; mTimeValid = true; }
119 void setTime(const TQTime& t) { mDateTime.setTime(t); mDateOnly = false; mTimeValid = true; }
124 void setTime_t(uint secs) { mDateTime.setTime_t(secs); mDateOnly = false; mTimeValid = true; }
129 DateTime addSecs(int n) const
130 {
131 if (mDateOnly)
132 return DateTime(mDateTime.date().addDays(n / (3600*24)), true);
133 else
134 return DateTime(mDateTime.addSecs(n), false);
135 }
140 DateTime addMins(int n) const
141 {
142 if (mDateOnly)
143 return DateTime(mDateTime.addDays(n / (60*24)), true);
144 else
145 return DateTime(mDateTime.addSecs(n * 60), false);
146 }
148 DateTime addDays(int n) const { return DateTime(mDateTime.addDays(n), mDateOnly); }
150 DateTime addMonths(int n) const { return DateTime(mDateTime.addMonths(n), mDateOnly); }
152 DateTime addYears(int n) const { return DateTime(mDateTime.addYears(n), mDateOnly); }
154 int daysTo(const DateTime& dt) const
155 { return (mDateOnly || dt.mDateOnly) ? mDateTime.date().daysTo(dt.date()) : mDateTime.daysTo(dt.mDateTime); }
160 int minsTo(const DateTime& dt) const
161 { return (mDateOnly || dt.mDateOnly) ? mDateTime.date().daysTo(dt.date()) * 24*60 : mDateTime.secsTo(dt.mDateTime) / 60; }
166 int secsTo(const DateTime& dt) const
167 { return (mDateOnly || dt.mDateOnly) ? mDateTime.date().daysTo(dt.date()) * 24*3600 : mDateTime.secsTo(dt.mDateTime); }
172 TQString toString(TQt::DateFormat f = TQt::TextDate) const
173 {
174 if (mDateOnly)
175 return mDateTime.date().toString(f);
176 else if (mTimeValid)
177 return mDateTime.toString(f);
178 else
179 return TQString();
180 }
185 TQString toString(const TQString& format) const
186 {
187 if (mDateOnly)
188 return mDateTime.date().toString(format);
189 else if (mTimeValid)
190 return mDateTime.toString(format);
191 else
192 return TQString();
193 }
198 TQString formatLocale(bool shortFormat = true) const;
202 static void setStartOfDay(const TQTime& sod) { mStartOfDay = sod; }
204 static TQTime startOfDay() { return mStartOfDay; }
205
206 friend bool operator==(const DateTime& dt1, const DateTime& dt2);
207 friend bool operator<(const DateTime& dt1, const DateTime& dt2);
208
209 private:
210 static TQTime mStartOfDay;
211 TQDateTime mDateTime;
212 bool mDateOnly;
213 bool mTimeValid; // whether the time is potentially valid - applicable only if mDateOnly false
214};
215
217bool operator==(const DateTime& dt1, const DateTime& dt2);
222bool operator<(const DateTime& dt1, const DateTime& dt2);
224inline bool operator!=(const DateTime& dt1, const DateTime& dt2) { return !operator==(dt1, dt2); }
229inline bool operator>(const DateTime& dt1, const DateTime& dt2) { return operator<(dt2, dt1); }
234inline bool operator>=(const DateTime& dt1, const DateTime& dt2) { return !operator<(dt1, dt2); }
239inline bool operator<=(const DateTime& dt1, const DateTime& dt2) { return !operator<(dt2, dt1); }
240
241#endif // DATETIME_H
A TQDateTime with date-only option.
Definition: datetime.h:40
friend bool operator<(const DateTime &dt1, const DateTime &dt2)
Returns true if the dt1 is earlier than dt2.
Definition: datetime.cpp:68
void setTime_t(uint secs)
Sets the value to a specified date-time value.
Definition: datetime.h:124
bool isDateOnly() const
Returns true if it is date-only value.
Definition: datetime.h:78
static TQTime startOfDay()
Returns the start-of-day time.
Definition: datetime.h:204
bool isValid() const
Returns true if the date is valid and, if it is a date-time value, the time is also valid.
Definition: datetime.h:76
void set(const TQDateTime &dt, bool dateOnly=false)
Sets a date-time or date-only value.
Definition: datetime.h:105
TQDateTime dateTime() const
Returns the date and time of the value.
Definition: datetime.cpp:34
void set(const TQDate &d, const TQTime &t)
Sets a date-time value.
Definition: datetime.h:114
bool isNull() const
Returns true if the date is null and, if it is a date-time value, the time is also null.
Definition: datetime.h:74
DateTime()
Default constructor.
Definition: datetime.h:45
DateTime addMins(int n) const
Returns a DateTime value mins minutes later than the value of this object.
Definition: datetime.h:140
void setDateOnly(bool d)
Sets the value to be either date-only or date-time.
Definition: datetime.h:82
DateTime addMonths(int n) const
Returns a DateTime value n months later than the value of this object.
Definition: datetime.h:150
int daysTo(const DateTime &dt) const
Returns the number of days from this date or date-time to dt.
Definition: datetime.h:154
TQString formatLocale(bool shortFormat=true) const
Returns the value as a string, formatted according to the user's locale.
Definition: datetime.cpp:39
DateTime & operator=(const TQDate &d)
Assignment operator.
Definition: datetime.h:71
DateTime(const TQDate &d)
Constructor for a date-only value.
Definition: datetime.h:47
DateTime(const TQDateTime &dt, bool dateOnly=false)
Constructor for a date-time or date-only value.
Definition: datetime.h:55
DateTime addDays(int n) const
Returns a DateTime value n days later than the value of this object.
Definition: datetime.h:148
int minsTo(const DateTime &dt) const
Returns the number of minutes from this date or date-time to dt.
Definition: datetime.h:160
int secsTo(const DateTime &dt) const
Returns the number of seconds from this date or date-time to dt.
Definition: datetime.h:166
DateTime addSecs(int n) const
Returns a DateTime value secs seconds later than the value of this object.
Definition: datetime.h:129
DateTime(const TQDate &d, const TQTime &t)
Constructor for a date-time value.
Definition: datetime.h:49
static void setStartOfDay(const TQTime &sod)
Sets the start-of-day time.
Definition: datetime.h:202
TQString toString(const TQString &format) const
Returns the value as a string.
Definition: datetime.h:185
DateTime & operator=(const DateTime &dt)
Assignment operator.
Definition: datetime.h:61
friend bool operator==(const DateTime &dt1, const DateTime &dt2)
Returns true if the two values are equal.
Definition: datetime.cpp:49
TQDate date() const
Returns the date part of the value.
Definition: datetime.h:87
TQString toString(TQt::DateFormat f=TQt::TextDate) const
Returns the value as a string.
Definition: datetime.h:172
TQTime time() const
Returns the time part of the value.
Definition: datetime.cpp:29
TQDateTime rawDateTime() const
Returns the date and time of the value.
Definition: datetime.h:100
DateTime addYears(int n) const
Returns a DateTime value n years later than the value of this object.
Definition: datetime.h:152
DateTime & operator=(const TQDateTime &dt)
Assignment operator.
Definition: datetime.h:66
void setTime(const TQTime &t)
Sets the time component of the value.
Definition: datetime.h:119