File indexing completed on 2023-09-24 04:04:48
0001 /* 0002 Copyright (c) 2010 John Layt <john@layt.net> 0003 0004 This library is free software; you can redistribute it and/or 0005 modify it under the terms of the GNU Library General Public 0006 License as published by the Free Software Foundation; either 0007 version 2 of the License, or (at your option) any later version. 0008 0009 This library is distributed in the hope that it will be useful, 0010 but WITHOUT ANY WARRANTY; without even the implied warranty of 0011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 0012 Library General Public License for more details. 0013 0014 You should have received a copy of the GNU Library General Public License 0015 along with this library; see the file COPYING.LIB. If not, write to 0016 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 0017 Boston, MA 02110-1301, USA. 0018 */ 0019 0020 #include "kdayperiod_p.h" 0021 0022 #include <QSharedData> 0023 #include <QString> 0024 #include <QTime> 0025 0026 class KDayPeriodPrivate : public QSharedData 0027 { 0028 public: 0029 KDayPeriodPrivate(const QString &periodCode, 0030 const QString &longName, 0031 const QString &shortName, 0032 const QString &narrowName, 0033 const QTime &periodStart, 0034 const QTime &periodEnd, 0035 int offsetFromStart, 0036 int offsetIfZero); 0037 KDayPeriodPrivate(const KDayPeriodPrivate &other); 0038 ~KDayPeriodPrivate(); 0039 0040 QString m_periodCode, m_longName, m_shortName, m_narrowName; 0041 QTime m_periodStart, m_periodEnd; 0042 int m_offsetFromStart, m_offsetIfZero; 0043 }; 0044 0045 KDayPeriodPrivate::KDayPeriodPrivate(const QString &periodCode, 0046 const QString &longName, 0047 const QString &shortName, 0048 const QString &narrowName, 0049 const QTime &periodStart, 0050 const QTime &periodEnd, 0051 int offsetFromStart, 0052 int offsetIfZero) 0053 : QSharedData(), 0054 m_periodCode(periodCode), 0055 m_longName(longName), 0056 m_shortName(shortName), 0057 m_narrowName(narrowName), 0058 m_periodStart(periodStart), 0059 m_periodEnd(periodEnd), 0060 m_offsetFromStart(offsetFromStart), 0061 m_offsetIfZero(offsetIfZero) 0062 { 0063 } 0064 0065 KDayPeriodPrivate::KDayPeriodPrivate(const KDayPeriodPrivate &other) 0066 : QSharedData(other), 0067 m_periodCode(other.m_periodCode), 0068 m_longName(other.m_longName), 0069 m_shortName(other.m_shortName), 0070 m_narrowName(other.m_narrowName), 0071 m_periodStart(other.m_periodStart), 0072 m_periodEnd(other.m_periodEnd), 0073 m_offsetFromStart(other.m_offsetFromStart), 0074 m_offsetIfZero(other.m_offsetIfZero) 0075 { 0076 } 0077 0078 KDayPeriodPrivate::~KDayPeriodPrivate() 0079 { 0080 } 0081 0082 KDayPeriod::KDayPeriod(const QString &periodCode, 0083 const QString &longName, 0084 const QString &shortName, 0085 const QString &narrowName, 0086 const QTime &periodStart, 0087 const QTime &periodEnd, 0088 int offsetFromStart, 0089 int offsetIfZero) 0090 : d(new KDayPeriodPrivate(periodCode, 0091 longName, 0092 shortName, 0093 narrowName, 0094 periodStart, 0095 periodEnd, 0096 offsetFromStart, 0097 offsetIfZero)) 0098 { 0099 } 0100 0101 KDayPeriod::KDayPeriod() 0102 : d(new KDayPeriodPrivate(QString(), QString(), QString(), QString(), QTime(), QTime(), -1, -1)) 0103 { 0104 } 0105 0106 KDayPeriod::KDayPeriod(const KDayPeriod &rhs) 0107 : d(rhs.d) 0108 { 0109 } 0110 0111 KDayPeriod::~KDayPeriod() 0112 { 0113 } 0114 0115 KDayPeriod &KDayPeriod::operator=(const KDayPeriod &rhs) 0116 { 0117 if (&rhs != this) { 0118 d = rhs.d; 0119 } 0120 return *this; 0121 } 0122 0123 QString KDayPeriod::periodCode() const 0124 { 0125 return d->m_periodCode; 0126 } 0127 0128 QTime KDayPeriod::periodStart() const 0129 { 0130 return d->m_periodStart; 0131 } 0132 0133 QTime KDayPeriod::periodEnd() const 0134 { 0135 return d->m_periodEnd; 0136 } 0137 0138 QString KDayPeriod::periodName(KLocale::DateTimeComponentFormat format) const 0139 { 0140 if (format == KLocale::LongName) { 0141 return d->m_longName; 0142 } else if (format == KLocale::NarrowName) { 0143 return d->m_narrowName; 0144 } else { 0145 return d->m_shortName; 0146 } 0147 } 0148 0149 int KDayPeriod::hourInPeriod(const QTime &time) const 0150 { 0151 int hourInPeriod = -1; 0152 if (time.isValid() && isValid(time)) { 0153 hourInPeriod = time.hour() - periodStart().hour() + d->m_offsetFromStart; 0154 while (d->m_offsetIfZero > 0 && hourInPeriod <= 0) { 0155 hourInPeriod = hourInPeriod + d->m_offsetIfZero; 0156 } 0157 } 0158 return hourInPeriod; 0159 } 0160 0161 QTime KDayPeriod::time(int hip, int minute, int second, int millisecond) const 0162 { 0163 QTime time; 0164 if (isValid()) { 0165 if (hip == d->m_offsetIfZero) { 0166 hip = 0; 0167 } 0168 int hour; 0169 if (periodStart() <= periodEnd() || 0170 (hip >= hourInPeriod(periodStart()) && 0171 hip <= hourInPeriod(QTime(23, 59, 59, 999)))) { 0172 hour = hip + periodStart().hour() - d->m_offsetFromStart; 0173 } else { 0174 hour = hip; 0175 } 0176 time = QTime(hour, minute, second, millisecond); 0177 if (time.isValid() && isValid(time)) { 0178 return time; 0179 } else { 0180 return QTime(); 0181 } 0182 } 0183 return time; 0184 } 0185 0186 bool KDayPeriod::isValid() const 0187 { 0188 return !d->m_periodCode.isEmpty() && 0189 d->m_periodStart.isValid() && 0190 d->m_periodEnd.isValid(); 0191 } 0192 0193 bool KDayPeriod::isValid(const QTime &time) const 0194 { 0195 if (isValid()) { 0196 if (periodStart() <= periodEnd()) { 0197 return time >= periodStart() && time <= periodEnd(); 0198 } else { 0199 return ((time >= periodStart() && time <= QTime(23, 59, 59, 999)) || 0200 (time >= QTime(0, 0, 0) && time <= periodEnd())); 0201 } 0202 } else { 0203 return false; 0204 } 0205 } 0206