KDECore
kdayperiod.cpp
Go to the documentation of this file.
00001 /* 00002 Copyright (c) 2010 John Layt <john@layt.net> 00003 00004 This library is free software; you can redistribute it and/or 00005 modify it under the terms of the GNU Library General Public 00006 License as published by the Free Software Foundation; either 00007 version 2 of the License, or (at your option) any later version. 00008 00009 This library is distributed in the hope that it will be useful, 00010 but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00012 Library General Public License for more details. 00013 00014 You should have received a copy of the GNU Library General Public License 00015 along with this library; see the file COPYING.LIB. If not, write to 00016 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00017 Boston, MA 02110-1301, USA. 00018 */ 00019 00020 #include "kdayperiod_p.h" 00021 00022 #include <QtCore/QSharedData> 00023 #include <QtCore/QString> 00024 #include <QtCore/QTime> 00025 00026 class KDayPeriodPrivate : public QSharedData 00027 { 00028 public: 00029 KDayPeriodPrivate(const QString &periodCode, 00030 const QString &longName, 00031 const QString &shortName, 00032 const QString &narrowName, 00033 const QTime &periodStart, 00034 const QTime &periodEnd, 00035 int offsetFromStart, 00036 int offsetIfZero); 00037 KDayPeriodPrivate(const KDayPeriodPrivate &other); 00038 ~KDayPeriodPrivate(); 00039 00040 QString m_periodCode, m_longName, m_shortName, m_narrowName; 00041 QTime m_periodStart, m_periodEnd; 00042 int m_offsetFromStart, m_offsetIfZero; 00043 }; 00044 00045 KDayPeriodPrivate::KDayPeriodPrivate(const QString &periodCode, 00046 const QString &longName, 00047 const QString &shortName, 00048 const QString &narrowName, 00049 const QTime &periodStart, 00050 const QTime &periodEnd, 00051 int offsetFromStart, 00052 int offsetIfZero) 00053 : QSharedData(), 00054 m_periodCode(periodCode), 00055 m_longName(longName), 00056 m_shortName(shortName), 00057 m_narrowName(narrowName), 00058 m_periodStart(periodStart), 00059 m_periodEnd(periodEnd), 00060 m_offsetFromStart(offsetFromStart), 00061 m_offsetIfZero(offsetIfZero) 00062 { 00063 } 00064 00065 KDayPeriodPrivate::KDayPeriodPrivate(const KDayPeriodPrivate &other) 00066 : QSharedData(other), 00067 m_periodCode(other.m_periodCode), 00068 m_longName(other.m_longName), 00069 m_shortName(other.m_shortName), 00070 m_narrowName(other.m_narrowName), 00071 m_periodStart(other.m_periodStart), 00072 m_periodEnd(other.m_periodEnd), 00073 m_offsetFromStart(other.m_offsetFromStart), 00074 m_offsetIfZero(other.m_offsetIfZero) 00075 { 00076 } 00077 00078 KDayPeriodPrivate::~KDayPeriodPrivate() 00079 { 00080 } 00081 00082 KDayPeriod::KDayPeriod(const QString &periodCode, 00083 const QString &longName, 00084 const QString &shortName, 00085 const QString &narrowName, 00086 const QTime &periodStart, 00087 const QTime &periodEnd, 00088 int offsetFromStart, 00089 int offsetIfZero) 00090 : d(new KDayPeriodPrivate(periodCode, 00091 longName, 00092 shortName, 00093 narrowName, 00094 periodStart, 00095 periodEnd, 00096 offsetFromStart, 00097 offsetIfZero)) 00098 { 00099 } 00100 00101 KDayPeriod::KDayPeriod() 00102 : d(new KDayPeriodPrivate(QString(), QString(), QString(), QString(), QTime(), QTime(), -1, -1)) 00103 { 00104 } 00105 00106 KDayPeriod::KDayPeriod(const KDayPeriod &rhs) 00107 : d(rhs.d) 00108 { 00109 } 00110 00111 KDayPeriod::~KDayPeriod() 00112 { 00113 } 00114 00115 KDayPeriod& KDayPeriod::operator=(const KDayPeriod &rhs) 00116 { 00117 if (&rhs != this) { 00118 d = rhs.d; 00119 } 00120 return *this; 00121 } 00122 00123 QString KDayPeriod::periodCode() const 00124 { 00125 return d->m_periodCode; 00126 } 00127 00128 QTime KDayPeriod::periodStart() const 00129 { 00130 return d->m_periodStart; 00131 } 00132 00133 QTime KDayPeriod::periodEnd() const 00134 { 00135 return d->m_periodEnd; 00136 } 00137 00138 QString KDayPeriod::periodName(KLocale::DateTimeComponentFormat format) const 00139 { 00140 if (format == KLocale::LongName) { 00141 return d->m_longName; 00142 } else if (format == KLocale::NarrowName) { 00143 return d->m_narrowName; 00144 } else { 00145 return d->m_shortName; 00146 } 00147 } 00148 00149 int KDayPeriod::hourInPeriod(const QTime &time) const 00150 { 00151 int hourInPeriod = -1; 00152 if (time.isValid() && isValid(time)) { 00153 hourInPeriod = time.hour() - periodStart().hour() + d->m_offsetFromStart; 00154 while (d->m_offsetIfZero > 0 && hourInPeriod <= 0) { 00155 hourInPeriod = hourInPeriod + d->m_offsetIfZero; 00156 } 00157 } 00158 return hourInPeriod; 00159 } 00160 00161 QTime KDayPeriod::time(int hip, int minute, int second, int millisecond) const 00162 { 00163 QTime time; 00164 if (isValid()) { 00165 if (hip == d->m_offsetIfZero) { 00166 hip = 0; 00167 } 00168 int hour; 00169 if (periodStart() <= periodEnd() || 00170 (hip >= hourInPeriod(periodStart()) && 00171 hip <= hourInPeriod(QTime(23, 59, 59, 999)))) { 00172 hour = hip + periodStart().hour() - d->m_offsetFromStart; 00173 } else { 00174 hour = hip; 00175 } 00176 time = QTime(hour, minute, second, millisecond); 00177 if (time.isValid() && isValid(time)) { 00178 return time; 00179 } else { 00180 return QTime(); 00181 } 00182 } 00183 return time; 00184 } 00185 00186 bool KDayPeriod::isValid() const 00187 { 00188 return !d->m_periodCode.isEmpty() && 00189 d->m_periodStart.isValid() && 00190 d->m_periodEnd.isValid(); 00191 } 00192 00193 bool KDayPeriod::isValid(const QTime &time) const 00194 { 00195 if (isValid()) { 00196 if (periodStart() <= periodEnd()) { 00197 return time >= periodStart() && time <= periodEnd(); 00198 } else { 00199 return ((time >= periodStart() && time <= QTime(23, 59, 59, 999)) || 00200 (time >= QTime( 0, 0, 0 ) && time <= periodEnd())); 00201 } 00202 } else { 00203 return false; 00204 } 00205 } 00206
KDE 4.6 API Reference