1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the QtXmlPatterns module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #include "qabstractdatetime_p.h"
43 #include "qbuiltintypes_p.h"
44 #include "qcommonvalues_p.h"
46 #include "qdaytimeduration_p.h"
50 using namespace QPatternist;
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),
66 DayTimeDuration::Ptr DayTimeDuration::fromLexical(const QString &lexical)
68 static const CaptureTable captureTable(
69 /* The extra paranthesis is a build fix for GCC 3.3. */
70 (QRegExp(QLatin1String(
71 "^\\s*" /* Any preceding whitespace. */
72 "(-)?" /* Any minus sign. */
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. */))),
91 DayCountProperty days = 0;
92 HourProperty hours = 0;
93 MinuteProperty minutes = 0;
94 SecondProperty sec = 0;
95 MSecondProperty msec = 0;
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,
104 DayTimeDuration::Ptr DayTimeDuration::fromComponents(const bool isPositive,
105 const DayCountProperty days,
106 const HourProperty hours,
107 const MinuteProperty minutes,
108 const SecondProperty seconds,
109 const MSecondProperty mseconds)
111 return DayTimeDuration::Ptr(new DayTimeDuration(isPositive,
119 DayTimeDuration::Ptr DayTimeDuration::fromSeconds(const SecondCountProperty sourceSecs,
120 const MSecondProperty msecs)
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;
130 return DayTimeDuration::Ptr(new DayTimeDuration(isPos, days, hours, mins, secs, msecs));
133 QString DayTimeDuration::stringValue() const
138 retval.append(QLatin1Char('-'));
140 retval.append(QLatin1Char('P'));
144 retval.append(QString::number(m_days));
145 retval.append(QLatin1Char('D'));
148 if(!m_hours && !m_minutes && !m_seconds && !m_seconds)
151 return QLatin1String("PT0S");
156 retval.append(QLatin1Char('T'));
160 retval.append(QString::number(m_hours));
161 retval.append(QLatin1Char('H'));
166 retval.append(QString::number(m_minutes));
167 retval.append(QLatin1Char('M'));
170 if(m_seconds || m_seconds)
172 retval.append(QString::number(m_seconds));
175 retval.append(serializeMSeconds(m_mseconds));
177 retval.append(QLatin1Char('S'));
179 else if(!m_days && !m_hours && !m_minutes)
180 retval.append(QLatin1String("0S"));
185 AbstractDuration::Value DayTimeDuration::value() const
187 return ((m_days * 24 * 60 * 60 * 1000) +
188 (m_hours * 60 * 60 * 1000) +
189 (m_minutes * 60 * 1000) +
191 m_mseconds) * (m_isPositive ? 1 : -1);
194 Item DayTimeDuration::fromValue(const Value val) const
197 return toItem(CommonValues::DayTimeDurationZero);
199 return toItem(fromSeconds(val / 1000, qAbs(val) % 1000));
202 ItemType::Ptr DayTimeDuration::type() const
204 return BuiltinTypes::xsDayTimeDuration;
207 YearProperty DayTimeDuration::years() const
212 MonthProperty DayTimeDuration::months() const
217 DayCountProperty DayTimeDuration::days() const
222 HourProperty DayTimeDuration::hours() const
227 MinuteProperty DayTimeDuration::minutes() const
232 SecondProperty DayTimeDuration::seconds() const
237 MSecondProperty DayTimeDuration::mseconds() const