Qt 4.8
qdaytimeduration.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtXmlPatterns module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #include "qabstractdatetime_p.h"
43 #include "qbuiltintypes_p.h"
44 #include "qcommonvalues_p.h"
45 
46 #include "qdaytimeduration_p.h"
47 
49 
50 using namespace QPatternist;
51 
52 DayTimeDuration::DayTimeDuration(const bool isPositiveP,
53  const DayCountProperty daysP,
54  const HourProperty hoursP,
55  const MinuteProperty minutesP,
56  const SecondProperty secs,
57  const MSecondProperty msecs) : AbstractDuration(isPositiveP),
58  m_days(daysP),
59  m_hours(hoursP),
60  m_minutes(minutesP),
61  m_seconds(secs),
62  m_mseconds(msecs)
63 {
64 }
65 
67 {
68  static const CaptureTable captureTable(
69  /* The extra paranthesis is a build fix for GCC 3.3. */
71  "^\\s*" /* Any preceding whitespace. */
72  "(-)?" /* Any minus sign. */
73  "P" /* Delimiter. */
74  "(?:(\\d+)D)?" /* Day part. */
75  "(?:" /* Here starts the optional time part. */
76  "(T)" /* SchemaTime delimiter. */
77  "(?:(\\d+)H)?" /* Hour part. */
78  "(?:(\\d+)M)?" /* Minute part. */
79  "(?:(\\d+)(?:\\.(\\d+))?S)?" /* Seconds & milli seconds. */
80  ")?" /* End of optional time part. */
81  "\\s*$" /* Any terminating whitespace. */))),
82  /*yearP*/ -1,
83  /*monthP*/ -1,
84  /*dayP*/ 2,
85  /*tDelimiterP*/ 3,
86  /*hourP*/ 4,
87  /*minutesP*/ 5,
88  /*secondsP*/ 6,
89  /*msecondsP*/ 7);
90 
92  HourProperty hours = 0;
94  SecondProperty sec = 0;
95  MSecondProperty msec = 0;
96  bool isPos;
97 
98  const DayTimeDuration::Ptr err(create(captureTable, lexical, &isPos, 0, 0, &days,
99  &hours, &minutes, &sec, &msec));
100  return err ? err : DayTimeDuration::Ptr(new DayTimeDuration(isPos, days, hours, minutes,
101  sec, msec));
102 }
103 
105  const DayCountProperty days,
106  const HourProperty hours,
107  const MinuteProperty minutes,
108  const SecondProperty seconds,
110 {
111  return DayTimeDuration::Ptr(new DayTimeDuration(isPositive,
112  days,
113  hours,
114  minutes,
115  seconds,
116  mseconds));
117 }
118 
120  const MSecondProperty msecs)
121 {
122  Q_ASSERT(msecs >= 0);
123  const SecondCountProperty source = qAbs(sourceSecs);
124  const bool isPos = sourceSecs >= 0;
125  const SecondCountProperty secs = source % 60;
126  const MinuteCountProperty mins = (source / 60) % 60;
127  const HourCountProperty hours = source / (60 * 60) % 24;
128  const DayCountProperty days = source / (60 * 60) / 24;
129 
130  return DayTimeDuration::Ptr(new DayTimeDuration(isPos, days, hours, mins, secs, msecs));
131 }
132 
134 {
135  QString retval;
136 
137  if(!m_isPositive)
138  retval.append(QLatin1Char('-'));
139 
140  retval.append(QLatin1Char('P'));
141 
142  if(m_days)
143  {
144  retval.append(QString::number(m_days));
145  retval.append(QLatin1Char('D'));
146  }
147 
148  if(!m_hours && !m_minutes && !m_seconds && !m_seconds)
149  {
150  if(!m_days)
151  return QLatin1String("PT0S");
152  else
153  return retval;
154  }
155 
156  retval.append(QLatin1Char('T'));
157 
158  if(m_hours)
159  {
160  retval.append(QString::number(m_hours));
161  retval.append(QLatin1Char('H'));
162  }
163 
164  if(m_minutes)
165  {
167  retval.append(QLatin1Char('M'));
168  }
169 
170  if(m_seconds || m_seconds)
171  {
173 
174  if(m_mseconds)
176 
177  retval.append(QLatin1Char('S'));
178  }
179  else if(!m_days && !m_hours && !m_minutes)
180  retval.append(QLatin1String("0S"));
181 
182  return retval;
183 }
184 
186 {
187  return ((m_days * 24 * 60 * 60 * 1000) +
188  (m_hours * 60 * 60 * 1000) +
189  (m_minutes * 60 * 1000) +
190  (m_seconds * 1000) +
191  m_mseconds) * (m_isPositive ? 1 : -1);
192 }
193 
195 {
196  if(val == 0)
198  else
199  return toItem(fromSeconds(val / 1000, qAbs(val) % 1000));
200 }
201 
203 {
205 }
206 
208 {
209  return 0;
210 }
211 
213 {
214  return 0;
215 }
216 
218 {
219  return m_days;
220 }
221 
223 {
224  return m_hours;
225 }
226 
228 {
229  return m_minutes;
230 }
231 
233 {
234  return m_seconds;
235 }
236 
238 {
239  return m_mseconds;
240 }
241 
static QString number(int, int base=10)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: qstring.cpp:6448
qint32 DayCountProperty
virtual MonthProperty months() const
static const AtomicType::Ptr xsDayTimeDuration
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
Base class for classes implementing durations.
static DayTimeDuration::Ptr fromComponents(const bool isPositive, const DayCountProperty days, const HourProperty hours, const MinuteProperty minutes, const SecondProperty seconds, const MSecondProperty mseconds)
The QRegExp class provides pattern matching using regular expressions.
Definition: qregexp.h:61
qint32 MinuteCountProperty
virtual QString stringValue() const
The QExplicitlySharedDataPointer class represents a pointer to an explicitly shared object...
Definition: qshareddata.h:136
virtual ItemType::Ptr type() const
qint8 MinuteProperty
qint32 SecondProperty
Item toItem(const QExplicitlySharedDataPointer< T > atomicValue)
Definition: qitem_p.h:431
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
const DayCountProperty m_days
The QString class provides a Unicode character string.
Definition: qstring.h:83
Q_DECL_CONSTEXPR T qAbs(const T &t)
Definition: qglobal.h:1201
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
static DayTimeDuration::Ptr fromLexical(const QString &string)
static DayTimeDuration::Ptr fromSeconds(const SecondCountProperty secs, const MSecondProperty msecs=0)
qint8 MonthProperty
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
The namespace for the internal API of QtXmlPatterns.
virtual SecondProperty seconds() const
qint8 HourProperty
qint32 YearProperty
qint64 Value
The amount in milli seconds.
QString & append(QChar c)
Definition: qstring.cpp:1777
Represents an item in the XPath 2.0 Data Model.
Definition: qitem_p.h:182
DayTimeDuration(const bool isPositive, const DayCountProperty days, const HourProperty hours, const MinuteProperty minutes, const SecondProperty seconds, const MSecondProperty mseconds)
static const DayTimeDuration::Ptr DayTimeDurationZero
QExplicitlySharedDataPointer< DayTimeDuration > Ptr
virtual HourProperty hours() const
const SecondProperty m_seconds
virtual MinuteProperty minutes() const
virtual Item fromValue(const Value val) const
const MinuteProperty m_minutes
static AtomicValue::Ptr create(const CaptureTable &captTable, const QString &lexical, bool *isPositive, YearProperty *years, MonthProperty *months, DayCountProperty *days, HourProperty *hours, MinuteProperty *minutes, SecondProperty *seconds, MSecondProperty *mseconds)
virtual YearProperty years() const
qint16 MSecondProperty
qint32 HourCountProperty
Acts as a mapping table for AbstractDuration::create() and describes where certain fields in a QRegEx...
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55
virtual DayCountProperty days() const
qint32 SecondCountProperty
virtual MSecondProperty mseconds() const
const MSecondProperty m_mseconds
static QString serializeMSeconds(const MSecondProperty mseconds)