File indexing completed on 2024-04-21 11:26:20

0001 /*
0002   This file is part of the kcalcore library.
0003 
0004   SPDX-FileCopyrightText: 2001-2003 Cornelius Schumacher <schumacher@kde.org>
0005 
0006   SPDX-License-Identifier: LGPL-2.0-or-later
0007 */
0008 /**
0009   @file
0010   This file is part of the API for handling calendar data and
0011   defines the Period class.
0012 
0013   @brief
0014   Represents a period of time.
0015 
0016   @author Cornelius Schumacher \<schumacher@kde.org\>
0017 */
0018 #ifndef KCALCORE_PERIOD_H
0019 #define KCALCORE_PERIOD_H
0020 
0021 #include "duration.h"
0022 #include "kcalendarcore_export.h"
0023 
0024 #include <QDataStream>
0025 #include <QDateTime>
0026 #include <QMetaType>
0027 #include <QVector>
0028 
0029 class QTimeZone;
0030 
0031 namespace KCalendarCore
0032 {
0033 /**
0034   The period can be defined by either a start time and an end time or
0035   by a start time and a duration.
0036 */
0037 class KCALENDARCORE_EXPORT Period
0038 {
0039 public:
0040     /**
0041        List of periods.
0042      */
0043     typedef QVector<Period> List;
0044 
0045     /**
0046       Constructs a period without a duration.
0047     */
0048     Period();
0049 
0050     /**
0051       Constructs a period from @p start to @p end.
0052 
0053       @param start the time the period begins.
0054       @param end the time the period ends.
0055     */
0056     Period(const QDateTime &start, const QDateTime &end);
0057 
0058     /**
0059       Constructs a period from @p start and lasting @p duration.
0060 
0061       @param start the time when the period starts.
0062       @param duration how long the period lasts.
0063     */
0064     Period(const QDateTime &start, const Duration &duration);
0065 
0066     /**
0067       Constructs a period by copying another period object
0068 
0069       @param period the period to copy
0070      */
0071 
0072     Period(const Period &period);
0073 
0074     /**
0075       Destroys a period.
0076     */
0077     ~Period();
0078 
0079     /**
0080       Returns true if the start of this period is earlier than the start of
0081       the @p other one.
0082 
0083       @param other is the other period to compare.
0084     */
0085     bool operator<(const Period &other) const;
0086 
0087     /**
0088       Returns true if the start of this period is later than the start of
0089       the @p other one.
0090 
0091       @param other the other period to compare
0092     */
0093     bool operator>(const Period &other) const
0094     {
0095         return other.operator<(*this);
0096     }
0097 
0098     /**
0099       Returns true if this period is equal to the @p other one.
0100       Even if their start and end times are the same, two periods are
0101       considered not equal if one is defined in terms of a duration and the
0102       other in terms of a start and end time.
0103 
0104       @param other the other period to compare
0105     */
0106     bool operator==(const Period &other) const;
0107 
0108     /**
0109       Returns true if this period is not equal to the @p other one.
0110 
0111       @param other the other period to compare
0112       @see operator==()
0113     */
0114     bool operator!=(const Period &other) const
0115     {
0116         return !operator==(other);
0117     }
0118 
0119     /**
0120       Sets this period equal to the @p other one.
0121 
0122       @param other is the other period to compare.
0123     */
0124     Period &operator=(const Period &other);
0125 
0126     /**
0127       Returns true if the Period is not empty.
0128       @since 5.87
0129     */
0130     Q_REQUIRED_RESULT bool isValid() const;
0131 
0132     /**
0133       Returns when this period starts.
0134     */
0135     Q_REQUIRED_RESULT QDateTime start() const;
0136 
0137     /**
0138       Returns when this period ends.
0139     */
0140     Q_REQUIRED_RESULT QDateTime end() const;
0141 
0142     /**
0143       Returns the duration of the period.
0144 
0145       If the period is defined in terms of a start and end time, the duration
0146       is computed from these. In this case, if the time of day in @p start and
0147       @p end is equal, and their time specifications (i.e. time zone etc.) are
0148       the same, the duration will be set in terms of days. Otherwise, the
0149       duration will be set in terms of seconds.
0150 
0151       If the period is defined in terms of a duration, that duration is
0152       returned unchanged.
0153     */
0154     Q_REQUIRED_RESULT Duration duration() const;
0155 
0156     /**
0157       Returns the duration of the period.
0158 
0159       If the period is defined in terms of a start and end time, the duration
0160       is first computed from these.
0161 
0162       If @p type is Days, and the duration is not an exact number of days,
0163       the duration will be rounded down to the nearest whole number of days.
0164 
0165       @param type the unit of time to use (seconds or days)
0166     */
0167     Q_REQUIRED_RESULT Duration duration(Duration::Type type) const;
0168 
0169     /**
0170       Returns true if this period has a set duration, false
0171       if it just has a start and an end.
0172     */
0173     Q_REQUIRED_RESULT bool hasDuration() const;
0174 
0175     /**
0176       Shift the times of the period so that they appear at the same clock
0177       time as before but in a new time zone. The shift is done from a viewing
0178       time zone rather than from the actual period time zone.
0179 
0180       For example, shifting a period whose start time is 09:00 America/New York,
0181       using an old viewing time zone (@p oldSpec) of Europe/London, to a new
0182       time zone (@p newSpec) of Europe/Paris, will result in the time being
0183       shifted from 14:00 (which is the London time of the period start) to
0184       14:00 Paris time.
0185 
0186       @param oldZone the time zone which provides the clock times
0187       @param newZone the new time zone
0188     */
0189     void shiftTimes(const QTimeZone &oldZone, const QTimeZone &newZone);
0190 
0191 private:
0192     //@cond PRIVATE
0193     class Private;
0194     Private *const d;
0195     //@endcond
0196 
0197     friend KCALENDARCORE_EXPORT QDataStream &operator<<(QDataStream &stream, const KCalendarCore::Period &period);
0198 
0199     friend KCALENDARCORE_EXPORT QDataStream &operator>>(QDataStream &stream, KCalendarCore::Period &period);
0200 };
0201 
0202 /** Write @p period to the datastream @p stream, in binary format. */
0203 KCALENDARCORE_EXPORT QDataStream &operator<<(QDataStream &stream, const KCalendarCore::Period &period);
0204 
0205 /** Read a Period object into @p period from @p stream, in binary format. */
0206 KCALENDARCORE_EXPORT QDataStream &operator>>(QDataStream &stream, KCalendarCore::Period &period);
0207 
0208 /**
0209   Return a hash value for a Period argument.
0210   @param key is a Period.
0211 */
0212 KCALENDARCORE_EXPORT uint qHash(const KCalendarCore::Period &key);
0213 }
0214 
0215 //@cond PRIVATE
0216 Q_DECLARE_METATYPE(KCalendarCore::Period)
0217 Q_DECLARE_TYPEINFO(KCalendarCore::Period, Q_MOVABLE_TYPE);
0218 //@endcond
0219 
0220 #endif