Qt 4.8
qyearmonthduration.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 "qbuiltintypes_p.h"
43 #include "qcommonvalues_p.h"
44 
45 #include "qyearmonthduration_p.h"
46 
48 
49 using namespace QPatternist;
50 
51 YearMonthDuration::YearMonthDuration(const bool isPositiveP,
52  const YearProperty yearsP,
53  const MonthProperty monthsP) : AbstractDuration(isPositiveP),
54  m_years(yearsP),
55  m_months(monthsP)
56 {
57  Q_ASSERT(monthsP < 32 && monthsP > -32);
58 }
59 
61 {
62  static const CaptureTable captureTable(
63  /* The extra paranthesis is a build fix for GCC 3.3. */
65  "^\\s*" /* Any preceding whitespace. */
66  "(-)?" /* Sign, if any. */
67  "P" /* Delimiter. */
68  "(?:(\\d+)Y)?" /* The years part. */
69  "(?:(\\d+)M)?" /* The months part. */
70  "\\s*$" /* Any terminating whitespace. */))),
71  2, /* yearP. */
72  3 /* monthP. */);
73 
74  YearProperty years = 0;
76  bool isPos;
77 
78  const AtomicValue::Ptr err(create(captureTable, lexical, &isPos, &years,
79  &months, 0, 0, 0, 0, 0));
80 
81  return err ? err : YearMonthDuration::Ptr(new YearMonthDuration(isPos, years, months));
82 }
83 
85  const YearProperty years,
86  const MonthProperty months)
87 {
88  return YearMonthDuration::Ptr(new YearMonthDuration(isPositive, years, months));
89 }
90 
92 {
93  QString retval;
94 
95  if(!m_isPositive)
96  retval.append(QLatin1Char('-'));
97 
98  retval.append(QLatin1Char('P'));
99 
100  /* When years == 0 and months == 0, we get "P0M", which
101  * is the correct canonical form. */
102  if(m_years)
103  {
104  retval.append(QString::number(m_years));
105  retval.append(QLatin1Char('Y'));
106 
107  if(m_months)
108  {
110  retval.append(QLatin1Char('M'));
111  }
112  }
113  else
114  {
115  if(m_months)
116  {
118  retval.append(QLatin1Char('M'));
119  }
120  else
121  return QLatin1String("P0M"); /* Ensure the canonical form. */
122  }
123 
124  return retval;
125 }
126 
128 {
129  return (m_years * 12 + m_months) * (m_isPositive ? 1 : -1);
130 }
131 
133 {
134  if(val == 0)
136  else
137  {
138  const Value absValue = qAbs(val);
140  absValue / 12,
141  absValue % 12));
142  }
143 }
144 
146 {
148 }
149 
151 {
152  return m_years;
153 }
154 
156 {
157  return m_months;
158 }
159 
161 {
162  return 0;
163 }
164 
166 {
167  return 0;
168 }
169 
171 {
172  return 0;
173 }
174 
176 {
177  return 0;
178 }
179 
181 {
182  return 0;
183 }
184 
185 
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 DayCountProperty days() const
static const DayTimeDuration::Ptr YearMonthDurationZero
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
Base class for classes implementing durations.
virtual MinuteProperty minutes() const
virtual ItemType::Ptr type() const
The QRegExp class provides pattern matching using regular expressions.
Definition: qregexp.h:61
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
YearMonthDuration(const bool isPositive, const YearProperty years, const MonthProperty months)
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 const AtomicType::Ptr xsYearMonthDuration
virtual QString stringValue() const
static YearMonthDuration::Ptr fromLexical(const QString &string)
qint8 MonthProperty
virtual HourProperty hours() const
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
virtual MSecondProperty mseconds() const
The namespace for the internal API of QtXmlPatterns.
qint8 HourProperty
qint32 YearProperty
virtual MonthProperty months() const
static YearMonthDuration::Ptr fromComponents(const bool isPositive, const YearProperty years, const MonthProperty months)
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 YearProperty years() const
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
virtual Item fromValue(const Value val) const
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 SecondProperty seconds() const