File indexing completed on 2024-04-21 03:52:51

0001 /*
0002   This file is part of the kcalcore library.
0003 
0004   SPDX-FileCopyrightText: 2001 Cornelius Schumacher <schumacher@kde.org>
0005   SPDX-FileCopyrightText: 2007 David Jarvie <djarvie@kde.org>
0006 
0007   SPDX-License-Identifier: LGPL-2.0-or-later
0008 */
0009 /**
0010   @file
0011   This file is part of the API for handling calendar data and
0012   defines the Period class.
0013 
0014   @brief
0015   Represents a period of time.
0016 
0017   @author Cornelius Schumacher \<schumacher@kde.org\>
0018 */
0019 
0020 #include "incidencebase.h"
0021 #include "period.h"
0022 #include "utils_p.h"
0023 
0024 #include <QHash>
0025 #include <QTimeZone>
0026 
0027 using namespace KCalendarCore;
0028 
0029 //@cond PRIVATE
0030 class Q_DECL_HIDDEN KCalendarCore::Period::Private
0031 {
0032 public:
0033     Private()
0034         : mHasDuration(false)
0035         , mDailyDuration(false)
0036     {
0037     }
0038     Private(const QDateTime &start, const QDateTime &end, bool hasDuration)
0039         : mStart(start)
0040         , mEnd(end)
0041         , mHasDuration(hasDuration)
0042         , mDailyDuration(false)
0043     {
0044     }
0045     QDateTime mStart; // period starting date/time
0046     QDateTime mEnd; // period ending date/time
0047     bool mHasDuration = false; // does period have a duration?
0048     bool mDailyDuration = false; // duration is defined as number of days, not seconds
0049 };
0050 //@endcond
0051 
0052 Period::Period()
0053     : d(new KCalendarCore::Period::Private())
0054 {
0055 }
0056 
0057 Period::Period(const QDateTime &start, const QDateTime &end)
0058     : d(new KCalendarCore::Period::Private(start, end, false))
0059 {
0060 }
0061 
0062 Period::Period(const QDateTime &start, const Duration &duration)
0063     : d(new KCalendarCore::Period::Private(start, duration.end(start), true))
0064 {
0065     d->mDailyDuration = duration.isDaily();
0066 }
0067 
0068 Period::Period(const Period &period)
0069     : d(new KCalendarCore::Period::Private(*period.d))
0070 {
0071 }
0072 
0073 Period::~Period()
0074 {
0075     delete d;
0076 }
0077 
0078 bool Period::operator<(const Period &other) const
0079 {
0080     return d->mStart < other.d->mStart;
0081 }
0082 
0083 bool Period::operator==(const Period &other) const
0084 {
0085     return identical(d->mStart, other.d->mStart) && identical(d->mEnd, other.d->mEnd)
0086         && d->mHasDuration == other.d->mHasDuration;
0087 }
0088 
0089 Period &Period::operator=(const Period &other)
0090 {
0091     // check for self assignment
0092     if (&other == this) {
0093         return *this;
0094     }
0095 
0096     *d = *other.d;
0097     return *this;
0098 }
0099 
0100 bool Period::isValid() const
0101 {
0102     return d->mStart.isValid();
0103 }
0104 
0105 QDateTime Period::start() const
0106 {
0107     return d->mStart;
0108 }
0109 
0110 QDateTime Period::end() const
0111 {
0112     return d->mEnd;
0113 }
0114 
0115 Duration Period::duration() const
0116 {
0117     if (d->mHasDuration) {
0118         return Duration(d->mStart, d->mEnd, d->mDailyDuration ? Duration::Days : Duration::Seconds);
0119     } else {
0120         return Duration(d->mStart, d->mEnd);
0121     }
0122 }
0123 
0124 Duration Period::duration(Duration::Type type) const
0125 {
0126     return Duration(d->mStart, d->mEnd, type);
0127 }
0128 
0129 bool Period::hasDuration() const
0130 {
0131     return d->mHasDuration;
0132 }
0133 
0134 void Period::shiftTimes(const QTimeZone &oldZone, const QTimeZone &newZone)
0135 {
0136     if (oldZone.isValid() && newZone.isValid() && oldZone != newZone) {
0137         d->mStart = d->mStart.toTimeZone(oldZone);
0138         d->mStart.setTimeZone(newZone);
0139         d->mEnd = d->mEnd.toTimeZone(oldZone);
0140         d->mEnd.setTimeZone(newZone);
0141     }
0142 }
0143 
0144 QDataStream &KCalendarCore::operator<<(QDataStream &stream, const KCalendarCore::Period &period)
0145 {
0146     serializeQDateTimeAsKDateTime(stream, period.d->mStart);
0147     serializeQDateTimeAsKDateTime(stream, period.d->mEnd);
0148     return stream << period.d->mDailyDuration << period.d->mHasDuration;
0149 }
0150 
0151 QDataStream &KCalendarCore::operator>>(QDataStream &stream, KCalendarCore::Period &period)
0152 {
0153     deserializeKDateTimeAsQDateTime(stream, period.d->mStart);
0154     deserializeKDateTimeAsQDateTime(stream, period.d->mEnd);
0155     stream >> period.d->mDailyDuration >> period.d->mHasDuration;
0156     return stream;
0157 }
0158 
0159 size_t KCalendarCore::qHash(const KCalendarCore::Period &key, size_t seed)
0160 {
0161     if (key.hasDuration()) {
0162         return qHash(key.duration(), seed);
0163     } else {
0164         return qHashMulti(seed, key.start(), key.end());
0165     }
0166 }