File indexing completed on 2024-04-28 08:32:04

0001 /*
0002     SPDX-FileCopyrightText: 2020 Volker Krause <vkrause@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #ifndef KOPENINGHOURS_INTERVAL_H
0008 #define KOPENINGHOURS_INTERVAL_H
0009 
0010 #include "kopeninghours_export.h"
0011 
0012 #include <QDateTime>
0013 #include <QDebug>
0014 #include <QExplicitlySharedDataPointer>
0015 #include <QMetaType>
0016 
0017 class QDateTime;
0018 
0019 namespace KOpeningHours {
0020 
0021 class IntervalPrivate;
0022 
0023 /** A time interval for which an opening hours expression has been evaluated. */
0024 class KOPENINGHOURS_EXPORT Interval
0025 {
0026     Q_GADGET
0027     Q_PROPERTY(State state READ state)
0028     Q_PROPERTY(QDateTime begin READ begin)
0029     Q_PROPERTY(bool hasOpenBegin READ hasOpenBegin)
0030     Q_PROPERTY(QDateTime end READ end)
0031     Q_PROPERTY(bool hasOpenEnd READ hasOpenEnd)
0032     Q_PROPERTY(bool hasOpenEndTime READ hasOpenEndTime)
0033     Q_PROPERTY(QString comment READ comment)
0034     Q_PROPERTY(QDateTime estimatedEnd READ estimatedEnd)
0035     Q_PROPERTY(int dstOffset READ dstOffset)
0036 public:
0037     Interval();
0038     Interval(const Interval&);
0039     Interval(Interval&&);
0040     ~Interval();
0041     Interval& operator=(const Interval&);
0042     Interval& operator=(Interval&&);
0043 
0044     /** Check whether this interval starts before @p other. */
0045     bool operator<(const Interval &other) const;
0046 
0047     /** Default constructed empty/invalid interval. */
0048     bool isValid() const;
0049 
0050     /** Checks whether this interval overlaps with @p other. */
0051     bool intersects(const Interval &other) const;
0052 
0053     /** Begin of the interval.
0054      *  This is the first point in time included in the interval, or invalid if this is an interval with an open begin.
0055      */
0056     QDateTime begin() const;
0057     void setBegin(const QDateTime &begin);
0058 
0059     /** Returns @c true if this is an interval with an open begin, ie. starting at the beginning of time. */
0060     bool hasOpenBegin() const;
0061 
0062     /** End of the interval.
0063      *  This is the first point in time not included in the interval anymore, or invalid for open-ended intervals.
0064      *  That is, the end of an interval describing the year 2020 would be Jan 1st 2021 at midnight (00:00).
0065      */
0066     QDateTime end() const;
0067     void setEnd(const QDateTime &end);
0068 
0069     /** Returns @c true if this is an interval with an open end date, ie. continuing for all eternity.
0070      *  @note This is different from an interval with an open end time.
0071      */
0072     bool hasOpenEnd() const;
0073 
0074     /** Returns @c true if this is an interval with an open end time, ie. an interval generated by a selector like "20:00+".
0075      *  @note This is different from an interval with an open end.
0076      */
0077     bool hasOpenEndTime() const;
0078     void setOpenEndTime(bool openEndTime);
0079 
0080     /** Returns an estimated end time for intervals with an open end time.
0081      *  By default this is the same as end() would return, unless higher-level
0082      *  logic with a view on multiple intervals and/or other context actually sets this.
0083      */
0084     QDateTime estimatedEnd() const;
0085     void setEstimatedEnd(const QDateTime &estimatedEnd);
0086 
0087     /** Returns the UTC offset change between estimatedEnd() and begin().
0088      *  This is 0, unless there is a DST transition happening in that interval.
0089      *  @since 23.04.0
0090      */
0091     int dstOffset() const;
0092 
0093     /** Check if this interval contains @p dt. */
0094     bool contains(const QDateTime &dt) const;
0095 
0096     /** Opening state during a time interval */
0097     enum State {
0098         Invalid,
0099         Open,
0100         Closed,
0101         Unknown
0102     };
0103     Q_ENUM(State)
0104 
0105     /** The opening state for this time interval. */
0106     State state() const;
0107     void setState(State state);
0108 
0109     /** Comment. */
0110     QString comment() const;
0111     void setComment(const QString &comment);
0112 
0113 private:
0114     QExplicitlySharedDataPointer<IntervalPrivate> d;
0115 };
0116 
0117 }
0118 
0119 Q_DECLARE_METATYPE(KOpeningHours::Interval)
0120 
0121 KOPENINGHOURS_EXPORT QDebug operator<<(QDebug debug, const KOpeningHours::Interval &interval);
0122 
0123 #endif // KOPENINGHOURS_INTERVAL_H