• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.14.38 API Reference
  • KDE Home
  • Contact Us
 

KDECore

  • kdecore
  • date
kdayperiod.cpp
Go to the documentation of this file.
1/*
2 Copyright (c) 2010 John Layt <john@layt.net>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301, USA.
18*/
19
20#include "kdayperiod_p.h"
21
22#include <QtCore/QSharedData>
23#include <QtCore/QString>
24#include <QtCore/QTime>
25
26class KDayPeriodPrivate : public QSharedData
27{
28public:
29 KDayPeriodPrivate(const QString &periodCode,
30 const QString &longName,
31 const QString &shortName,
32 const QString &narrowName,
33 const QTime &periodStart,
34 const QTime &periodEnd,
35 int offsetFromStart,
36 int offsetIfZero);
37 KDayPeriodPrivate(const KDayPeriodPrivate &other);
38 ~KDayPeriodPrivate();
39
40 QString m_periodCode, m_longName, m_shortName, m_narrowName;
41 QTime m_periodStart, m_periodEnd;
42 int m_offsetFromStart, m_offsetIfZero;
43};
44
45KDayPeriodPrivate::KDayPeriodPrivate(const QString &periodCode,
46 const QString &longName,
47 const QString &shortName,
48 const QString &narrowName,
49 const QTime &periodStart,
50 const QTime &periodEnd,
51 int offsetFromStart,
52 int offsetIfZero)
53 : QSharedData(),
54 m_periodCode(periodCode),
55 m_longName(longName),
56 m_shortName(shortName),
57 m_narrowName(narrowName),
58 m_periodStart(periodStart),
59 m_periodEnd(periodEnd),
60 m_offsetFromStart(offsetFromStart),
61 m_offsetIfZero(offsetIfZero)
62{
63}
64
65KDayPeriodPrivate::KDayPeriodPrivate(const KDayPeriodPrivate &other)
66 : QSharedData(other),
67 m_periodCode(other.m_periodCode),
68 m_longName(other.m_longName),
69 m_shortName(other.m_shortName),
70 m_narrowName(other.m_narrowName),
71 m_periodStart(other.m_periodStart),
72 m_periodEnd(other.m_periodEnd),
73 m_offsetFromStart(other.m_offsetFromStart),
74 m_offsetIfZero(other.m_offsetIfZero)
75{
76}
77
78KDayPeriodPrivate::~KDayPeriodPrivate()
79{
80}
81
82KDayPeriod::KDayPeriod(const QString &periodCode,
83 const QString &longName,
84 const QString &shortName,
85 const QString &narrowName,
86 const QTime &periodStart,
87 const QTime &periodEnd,
88 int offsetFromStart,
89 int offsetIfZero)
90 : d(new KDayPeriodPrivate(periodCode,
91 longName,
92 shortName,
93 narrowName,
94 periodStart,
95 periodEnd,
96 offsetFromStart,
97 offsetIfZero))
98{
99}
100
101KDayPeriod::KDayPeriod()
102 : d(new KDayPeriodPrivate(QString(), QString(), QString(), QString(), QTime(), QTime(), -1, -1))
103{
104}
105
106KDayPeriod::KDayPeriod(const KDayPeriod &rhs)
107 : d(rhs.d)
108{
109}
110
111KDayPeriod::~KDayPeriod()
112{
113}
114
115KDayPeriod& KDayPeriod::operator=(const KDayPeriod &rhs)
116{
117 if (&rhs != this) {
118 d = rhs.d;
119 }
120 return *this;
121}
122
123QString KDayPeriod::periodCode() const
124{
125 return d->m_periodCode;
126}
127
128QTime KDayPeriod::periodStart() const
129{
130 return d->m_periodStart;
131}
132
133QTime KDayPeriod::periodEnd() const
134{
135 return d->m_periodEnd;
136}
137
138QString KDayPeriod::periodName(KLocale::DateTimeComponentFormat format) const
139{
140 if (format == KLocale::LongName) {
141 return d->m_longName;
142 } else if (format == KLocale::NarrowName) {
143 return d->m_narrowName;
144 } else {
145 return d->m_shortName;
146 }
147}
148
149int KDayPeriod::hourInPeriod(const QTime &time) const
150{
151 int hourInPeriod = -1;
152 if (time.isValid() && isValid(time)) {
153 hourInPeriod = time.hour() - periodStart().hour() + d->m_offsetFromStart;
154 while (d->m_offsetIfZero > 0 && hourInPeriod <= 0) {
155 hourInPeriod = hourInPeriod + d->m_offsetIfZero;
156 }
157 }
158 return hourInPeriod;
159}
160
161QTime KDayPeriod::time(int hip, int minute, int second, int millisecond) const
162{
163 QTime time;
164 if (isValid()) {
165 if (hip == d->m_offsetIfZero) {
166 hip = 0;
167 }
168 int hour;
169 if (periodStart() <= periodEnd() ||
170 (hip >= hourInPeriod(periodStart()) &&
171 hip <= hourInPeriod(QTime(23, 59, 59, 999)))) {
172 hour = hip + periodStart().hour() - d->m_offsetFromStart;
173 } else {
174 hour = hip;
175 }
176 time = QTime(hour, minute, second, millisecond);
177 if (time.isValid() && isValid(time)) {
178 return time;
179 } else {
180 return QTime();
181 }
182 }
183 return time;
184}
185
186bool KDayPeriod::isValid() const
187{
188 return !d->m_periodCode.isEmpty() &&
189 d->m_periodStart.isValid() &&
190 d->m_periodEnd.isValid();
191}
192
193bool KDayPeriod::isValid(const QTime &time) const
194{
195 if (isValid()) {
196 if (periodStart() <= periodEnd()) {
197 return time >= periodStart() && time <= periodEnd();
198 } else {
199 return ((time >= periodStart() && time <= QTime(23, 59, 59, 999)) ||
200 (time >= QTime(0, 0, 0) && time <= periodEnd()));
201 }
202 } else {
203 return false;
204 }
205}
206
KDayPeriod
Definition: kdayperiod_p.h:55
KDayPeriod::periodCode
QString periodCode() const
Return the Period Code.
Definition: kdayperiod.cpp:123
KDayPeriod::~KDayPeriod
virtual ~KDayPeriod()
Destructor.
Definition: kdayperiod.cpp:111
KDayPeriod::operator=
KDayPeriod & operator=(const KDayPeriod &rhs)
Assignment operator.
Definition: kdayperiod.cpp:115
KDayPeriod::time
QTime time(int hourInPeriod, int minute, int second, int millisecond=0) const
Calculate and return the 24hr time for a given hms in the Day Period.
Definition: kdayperiod.cpp:161
KDayPeriod::isValid
bool isValid() const
Return if the Day Period is valid.
Definition: kdayperiod.cpp:186
KDayPeriod::periodStart
QTime periodStart() const
Return the time the Period starts at.
Definition: kdayperiod.cpp:128
KDayPeriod::periodEnd
QTime periodEnd() const
Return the time the Period ends at.
Definition: kdayperiod.cpp:133
KDayPeriod::periodName
QString periodName(KLocale::DateTimeComponentFormat format=KLocale::ShortName) const
Return translated Period Name in the required format e.g.
Definition: kdayperiod.cpp:138
KDayPeriod::hourInPeriod
int hourInPeriod(const QTime &time) const
Calculate and return the hour in the Day Period for a given 24h time.
Definition: kdayperiod.cpp:149
KDayPeriod::KDayPeriod
KDayPeriod()
Constructs a null KDayPeriod.
Definition: kdayperiod.cpp:101
KLocale::DateTimeComponentFormat
DateTimeComponentFormat
Definition: klocale.h:908
KLocale::LongName
@ LongName
Long text format, e.g.
Definition: klocale.h:915
KLocale::NarrowName
@ NarrowName
Narrow text format, may not be unique, e.g.
Definition: klocale.h:913
QString
kdayperiod_p.h
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Feb 20 2023 00:00:00 by doxygen 1.9.6 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KDECore

Skip menu "KDECore"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Modules
  • Related Pages

kdelibs-4.14.38 API Reference

Skip menu "kdelibs-4.14.38 API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal