Qt 4.8
qduration.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 "qitem_p.h"
45 
46 #include "qduration_p.h"
47 
49 
50 using namespace QPatternist;
51 
52 Duration::Duration(const bool isPositiveP,
53  const YearProperty yearsP,
54  const MonthProperty monthsP,
55  const DayCountProperty daysP,
56  const HourProperty hoursP,
57  const MinuteProperty mins,
58  const SecondProperty secs,
59  const MSecondProperty msecs) : AbstractDuration(isPositiveP),
60  m_years(yearsP),
61  m_months(monthsP),
62  m_days(daysP),
63  m_hours(hoursP),
64  m_minutes(mins),
65  m_seconds(secs),
66  m_mseconds(msecs)
67 {
68 }
69 
71 {
72  static const CaptureTable captureTable(
73  /* The extra paranthesis is a build fix for GCC 3.3. */
75  "^\\s*" /* Any preceding whitespace. */
76  "(-)?" /* Any minus sign. */
77  "P" /* Delimiter. */
78  "(?:(\\d+)Y)?" /* Year part. */
79  "(?:(\\d+)M)?" /* Month part. */
80  "(?:(\\d+)D)?" /* Day part. */
81  "(?:" /* Here starts the optional time part. */
82  "(T)" /* SchemaTime delimiter. */
83  "(?:(\\d+)H)?" /* Hour part. */
84  "(?:(\\d+)M)?" /* Minute part. */
85  "(?:(\\d+)(?:\\.(\\d+))?S)?" /* Seconds & milli seconds. */
86  ")?" /* End of optional time part. */
87  "\\s*$" /* Any terminating whitespace. */))),
88  /*yearP*/ 2,
89  /*monthP*/ 3,
90  /*dayP*/ 4,
91  /*tDelimiterP*/ 5,
92  /*hourP*/ 6,
93  /*minutesP*/ 7,
94  /*secondsP*/ 8,
95  /*msecondsP*/ 9);
96 
97  YearProperty years = 0;
100  HourProperty hours = 0;
102  SecondProperty sec = 0;
103  MSecondProperty msec = 0;
104  bool isPos;
105 
106  const AtomicValue::Ptr err(create(captureTable, lexical, &isPos, &years, &months,
107  &days, &hours, &minutes, &sec, &msec));
108 
109  return err ? err : Duration::Ptr(new Duration(isPos, years, months, days, hours,
110  minutes, sec, msec));
111 }
112 
114  const YearProperty years,
115  const MonthProperty months,
116  const DayCountProperty days,
117  const HourProperty hours,
118  const MinuteProperty minutes,
119  const SecondProperty seconds,
121 {
122  return Duration::Ptr(new Duration(isPositive, years, months, days,
123  hours, minutes, seconds, mseconds));
124 }
125 
127 {
128  Q_ASSERT_X(false, Q_FUNC_INFO,
129  "Calling Duration::value() makes no sense");
130  return 0;
131 }
132 
134 {
135  Q_ASSERT_X(false, Q_FUNC_INFO,
136  "Calling Duration::fromValue() makes no sense");
137  return Item();
138 }
139 
141 {
142  QString retval;
143 
144  if(!m_isPositive)
145  retval.append(QLatin1Char('-'));
146 
147  retval.append(QLatin1Char('P'));
148 
149  if(m_years)
150  {
151  retval.append(QString::number(m_years));
152  retval.append(QLatin1Char('Y'));
153  }
154 
155  if(m_months)
156  {
158  retval.append(QLatin1Char('M'));
159  }
160 
161  if(m_days)
162  {
163  retval.append(QString::number(m_days));
164  retval.append(QLatin1Char('D'));
165  }
166 
167  if(!m_hours && !m_minutes && !m_seconds && !m_seconds)
168  {
169  if(!m_years && !m_months && !m_days)
170  return QLatin1String("PT0S");
171  else
172  return retval;
173  }
174 
175  retval.append(QLatin1Char('T'));
176 
177  if(m_hours)
178  {
179  retval.append(QString::number(m_hours));
180  retval.append(QLatin1Char('H'));
181  }
182 
183  if(m_minutes)
184  {
186  retval.append(QLatin1Char('M'));
187  }
188 
189  if(m_seconds || m_seconds)
190  {
192 
193  if(m_mseconds)
195 
196  retval.append(QLatin1Char('S'));
197  }
198  else if(!m_years && !m_months && !m_days && !m_hours && !m_minutes)
199  retval.append(QLatin1String("0S"));
200 
201  return retval;
202 }
203 
205 {
206  return m_years;
207 }
208 
210 {
211  return m_months;
212 }
213 
215 {
216  return m_days;
217 }
218 
220 {
221  return m_hours;
222 }
223 
225 {
226  return m_minutes;
227 }
228 
230 {
231  return m_seconds;
232 }
233 
235 {
236  return m_mseconds;
237 }
238 
240 {
242 }
243 
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
const DayCountProperty m_days
Definition: qduration_p.h:124
qint32 DayCountProperty
const SecondProperty m_seconds
Definition: qduration_p.h:127
virtual YearProperty years() const
Definition: qduration.cpp:204
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
Base class for classes implementing durations.
virtual HourProperty hours() const
Definition: qduration.cpp:219
The QRegExp class provides pattern matching using regular expressions.
Definition: qregexp.h:61
static const AtomicType::Ptr xsDuration
Due to strong interdependencies, this file contains the definitions for the classes Item...
Duration(const bool isPositive, const YearProperty years, const MonthProperty months, const DayCountProperty days, const HourProperty hours, const MinuteProperty minutes, const SecondProperty seconds, const MSecondProperty mseconds)
Definition: qduration.cpp:52
virtual ItemType::Ptr type() const
Definition: qduration.cpp:239
qint8 MinuteProperty
qint32 SecondProperty
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
static Duration::Ptr fromComponents(const bool isPositive, const YearProperty years, const MonthProperty months, const DayCountProperty days, const HourProperty hours, const MinuteProperty minutes, const SecondProperty seconds, const MSecondProperty mseconds)
Definition: qduration.cpp:113
The QString class provides a Unicode character string.
Definition: qstring.h:83
virtual MonthProperty months() const
Definition: qduration.cpp:209
virtual MSecondProperty mseconds() const
Definition: qduration.cpp:234
qint8 MonthProperty
virtual Value value() const
Definition: qduration.cpp:126
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
const MinuteProperty m_minutes
Definition: qduration_p.h:126
The namespace for the internal API of QtXmlPatterns.
static Duration::Ptr fromLexical(const QString &string)
Definition: qduration.cpp:70
qint8 HourProperty
qint32 YearProperty
AtomicValue::Ptr Ptr
Definition: qduration_p.h:72
#define Q_ASSERT_X(cond, where, what)
Definition: qglobal.h:1837
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
virtual QString stringValue() const
Definition: qduration.cpp:140
virtual SecondProperty seconds() const
Definition: qduration.cpp:229
const YearProperty m_years
Definition: qduration_p.h:122
const HourProperty m_hours
Definition: qduration_p.h:125
virtual MinuteProperty minutes() const
Definition: qduration.cpp:224
const MonthProperty m_months
Definition: qduration_p.h:123
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)
qint16 MSecondProperty
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
const MSecondProperty m_mseconds
Definition: qduration_p.h:128
virtual Item fromValue(const Value val) const
Definition: qduration.cpp:133
static QString serializeMSeconds(const MSecondProperty mseconds)
virtual DayCountProperty days() const
Definition: qduration.cpp:214
#define Q_FUNC_INFO
Definition: qglobal.h:1871