kalarm/lib

datetime.cpp

00001 /*
00002  *  datetime.cpp  -  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 #include "kalarm.h"
00021 
00022 #include <kglobal.h>
00023 #include <klocale.h>
00024 
00025 #include "datetime.h"
00026 
00027 QTime DateTime::mStartOfDay;
00028 
00029 QTime DateTime::time() const
00030 {
00031     return mDateOnly ? mStartOfDay : mDateTime.time();
00032 }
00033 
00034 QDateTime DateTime::dateTime() const
00035 {
00036     return mDateOnly ? QDateTime(mDateTime.date(), mStartOfDay) : mDateTime;
00037 }
00038 
00039 QString DateTime::formatLocale(bool shortFormat) const
00040 {
00041     if (mDateOnly)
00042         return KGlobal::locale()->formatDate(mDateTime.date(), shortFormat);
00043     else if (mTimeValid)
00044         return KGlobal::locale()->formatDateTime(mDateTime, shortFormat);
00045     else
00046         return QString::null;
00047 }
00048 
00049 bool operator==(const DateTime& dt1, const DateTime& dt2)
00050 {
00051     if (dt1.mDateTime.date() != dt2.mDateTime.date())
00052         return false;
00053     if (dt1.mDateOnly && dt2.mDateOnly)
00054         return true;
00055     if (!dt1.mDateOnly && !dt2.mDateOnly)
00056     {
00057         bool valid1 = dt1.mTimeValid && dt1.mDateTime.time().isValid();
00058         bool valid2 = dt2.mTimeValid && dt2.mDateTime.time().isValid();
00059         if (!valid1  &&  !valid2)
00060             return true;
00061         if (!valid1  ||  !valid2)
00062             return false;
00063         return dt1.mDateTime.time() == dt2.mDateTime.time();
00064     }
00065     return (dt1.mDateOnly ? dt2.mDateTime.time() : dt1.mDateTime.time()) == DateTime::startOfDay();
00066 }
00067 
00068 bool operator<(const DateTime& dt1, const DateTime& dt2)
00069 {
00070     if (dt1.mDateTime.date() != dt2.mDateTime.date())
00071         return dt1.mDateTime.date() < dt2.mDateTime.date();
00072     if (dt1.mDateOnly && dt2.mDateOnly)
00073         return false;
00074     if (!dt1.mDateOnly && !dt2.mDateOnly)
00075         return dt1.mDateTime.time() < dt2.mDateTime.time();
00076     QTime t = DateTime::startOfDay();
00077     if (dt1.mDateOnly)
00078         return t < dt2.mDateTime.time();
00079     return dt1.mDateTime.time() < t;
00080 }
KDE Home | KDE Accessibility Home | Description of Access Keys