source: trunk/src/xmlpatterns/data/qduration.cpp

Last change on this file was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

File size: 7.7 KB
RevLine 
[2]1/****************************************************************************
2**
[846]3** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
[561]4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
[2]6**
7** This file is part of the QtXmlPatterns module of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
15**
16** GNU Lesser General Public License Usage
17** Alternatively, this file may be used under the terms of the GNU Lesser
18** General Public License version 2.1 as published by the Free Software
19** Foundation and appearing in the file LICENSE.LGPL included in the
20** packaging of this file. Please review the following information to
21** ensure the GNU Lesser General Public License version 2.1 requirements
22** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23**
[561]24** In addition, as a special exception, Nokia gives you certain additional
25** rights. These rights are described in the Nokia Qt LGPL Exception
26** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
[2]27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
[561]36** If you have questions regarding the use of this file, please contact
37** Nokia at [email protected].
[2]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
48QT_BEGIN_NAMESPACE
49
50using namespace QPatternist;
51
52Duration::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
70Duration::Ptr Duration::fromLexical(const QString &lexical)
71{
72 static const CaptureTable captureTable(
73 /* The extra paranthesis is a build fix for GCC 3.3. */
74 (QRegExp(QLatin1String(
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;
98 MonthProperty months = 0;
99 DayCountProperty days = 0;
100 HourProperty hours = 0;
101 MinuteProperty minutes = 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
113Duration::Ptr Duration::fromComponents(const bool isPositive,
114 const YearProperty years,
115 const MonthProperty months,
116 const DayCountProperty days,
117 const HourProperty hours,
118 const MinuteProperty minutes,
119 const SecondProperty seconds,
120 const MSecondProperty mseconds)
121{
122 return Duration::Ptr(new Duration(isPositive, years, months, days,
123 hours, minutes, seconds, mseconds));
124}
125
126AbstractDuration::Value Duration::value() const
127{
128 Q_ASSERT_X(false, Q_FUNC_INFO,
129 "Calling Duration::value() makes no sense");
130 return 0;
131}
132
133Item Duration::fromValue(const Value) const
134{
135 Q_ASSERT_X(false, Q_FUNC_INFO,
136 "Calling Duration::fromValue() makes no sense");
137 return Item();
138}
139
140QString Duration::stringValue() const
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 {
157 retval.append(QString::number(m_months));
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 {
185 retval.append(QString::number(m_minutes));
186 retval.append(QLatin1Char('M'));
187 }
188
189 if(m_seconds || m_seconds)
190 {
191 retval.append(QString::number(m_seconds));
192
193 if(m_mseconds)
194 retval.append(serializeMSeconds(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
204YearProperty Duration::years() const
205{
206 return m_years;
207}
208
209MonthProperty Duration::months() const
210{
211 return m_months;
212}
213
214DayCountProperty Duration::days() const
215{
216 return m_days;
217}
218
219HourProperty Duration::hours() const
220{
221 return m_hours;
222}
223
224MinuteProperty Duration::minutes() const
225{
226 return m_minutes;
227}
228
229SecondProperty Duration::seconds() const
230{
231 return m_seconds;
232}
233
234MSecondProperty Duration::mseconds() const
235{
236 return m_mseconds;
237}
238
239ItemType::Ptr Duration::type() const
240{
241 return BuiltinTypes::xsDuration;
242}
243
244QT_END_NAMESPACE
Note: See TracBrowser for help on using the repository browser.