kalarm/lib

datetime.cpp
1/*
2 * datetime.cpp - date/time representation with optional date-only value
3 * Program: kalarm
4 * Copyright (C) 2003, 2005 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#include "kalarm.h"
21
22#include <tdeglobal.h>
23#include <tdelocale.h>
24
25#include "datetime.h"
26
27TQTime DateTime::mStartOfDay;
28
29TQTime DateTime::time() const
30{
31 return mDateOnly ? mStartOfDay : mDateTime.time();
32}
33
34TQDateTime DateTime::dateTime() const
35{
36 return mDateOnly ? TQDateTime(mDateTime.date(), mStartOfDay) : mDateTime;
37}
38
39TQString DateTime::formatLocale(bool shortFormat) const
40{
41 if (mDateOnly)
42 return TDEGlobal::locale()->formatDate(mDateTime.date(), shortFormat);
43 else if (mTimeValid)
44 return TDEGlobal::locale()->formatDateTime(mDateTime, shortFormat);
45 else
46 return TQString();
47}
48
49bool operator==(const DateTime& dt1, const DateTime& dt2)
50{
51 if (dt1.mDateTime.date() != dt2.mDateTime.date())
52 return false;
53 if (dt1.mDateOnly && dt2.mDateOnly)
54 return true;
55 if (!dt1.mDateOnly && !dt2.mDateOnly)
56 {
57 bool valid1 = dt1.mTimeValid && dt1.mDateTime.time().isValid();
58 bool valid2 = dt2.mTimeValid && dt2.mDateTime.time().isValid();
59 if (!valid1 && !valid2)
60 return true;
61 if (!valid1 || !valid2)
62 return false;
63 return dt1.mDateTime.time() == dt2.mDateTime.time();
64 }
65 return (dt1.mDateOnly ? dt2.mDateTime.time() : dt1.mDateTime.time()) == DateTime::startOfDay();
66}
67
68bool operator<(const DateTime& dt1, const DateTime& dt2)
69{
70 if (dt1.mDateTime.date() != dt2.mDateTime.date())
71 return dt1.mDateTime.date() < dt2.mDateTime.date();
72 if (dt1.mDateOnly && dt2.mDateOnly)
73 return false;
74 if (!dt1.mDateOnly && !dt2.mDateOnly)
75 return dt1.mDateTime.time() < dt2.mDateTime.time();
76 TQTime t = DateTime::startOfDay();
77 if (dt1.mDateOnly)
78 return t < dt2.mDateTime.time();
79 return dt1.mDateTime.time() < t;
80}
A TQDateTime with date-only option.
Definition: datetime.h:40
static TQTime startOfDay()
Returns the start-of-day time.
Definition: datetime.h:204
TQDateTime dateTime() const
Returns the date and time of the value.
Definition: datetime.cpp:34
TQString formatLocale(bool shortFormat=true) const
Returns the value as a string, formatted according to the user's locale.
Definition: datetime.cpp:39
TQTime time() const
Returns the time part of the value.
Definition: datetime.cpp:29