1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
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 The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/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 3 as published by the Free Software |
20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
21 | ** packaging of this file. Please review the following information to |
22 | ** ensure the GNU Lesser General Public License version 3 requirements |
23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
24 | ** |
25 | ** GNU General Public License Usage |
26 | ** Alternatively, this file may be used under the terms of the GNU |
27 | ** General Public License version 2.0 or (at your option) the GNU General |
28 | ** Public license version 3 or any later version approved by the KDE Free |
29 | ** Qt Foundation. The licenses are as published by the Free Software |
30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
31 | ** included in the packaging of this file. Please review the following |
32 | ** information to ensure the GNU General Public License requirements will |
33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
35 | ** |
36 | ** $QT_END_LICENSE$ |
37 | ** |
38 | ****************************************************************************/ |
39 | |
40 | #include "qabstractdatetime_p.h" |
41 | #include "qbuiltintypes_p.h" |
42 | #include "qcommonvalues_p.h" |
43 | |
44 | #include "qdaytimeduration_p.h" |
45 | |
46 | QT_BEGIN_NAMESPACE |
47 | |
48 | using namespace QPatternist; |
49 | |
50 | DayTimeDuration::DayTimeDuration(const bool isPositiveP, |
51 | const DayCountProperty daysP, |
52 | const HourProperty hoursP, |
53 | const MinuteProperty minutesP, |
54 | const SecondProperty secs, |
55 | const MSecondProperty msecs) : AbstractDuration(isPositiveP), |
56 | m_days(daysP), |
57 | m_hours(hoursP), |
58 | m_minutes(minutesP), |
59 | m_seconds(secs), |
60 | m_mseconds(msecs) |
61 | { |
62 | } |
63 | |
64 | DayTimeDuration::Ptr DayTimeDuration::fromLexical(const QString &lexical) |
65 | { |
66 | static const CaptureTable captureTable( |
67 | /* The extra paranthesis is a build fix for GCC 3.3. */ |
68 | (QRegExp(QLatin1String( |
69 | "^\\s*" /* Any preceding whitespace. */ |
70 | "(-)?" /* Any minus sign. */ |
71 | "P" /* Delimiter. */ |
72 | "(?:(\\d+)D)?" /* Day part. */ |
73 | "(?:" /* Here starts the optional time part. */ |
74 | "(T)" /* SchemaTime delimiter. */ |
75 | "(?:(\\d+)H)?" /* Hour part. */ |
76 | "(?:(\\d+)M)?" /* Minute part. */ |
77 | "(?:(\\d+)(?:\\.(\\d+))?S)?" /* Seconds & milli seconds. */ |
78 | ")?" /* End of optional time part. */ |
79 | "\\s*$" /* Any terminating whitespace. */))), |
80 | /*yearP*/ -1, |
81 | /*monthP*/ -1, |
82 | /*dayP*/ 2, |
83 | /*tDelimiterP*/ 3, |
84 | /*hourP*/ 4, |
85 | /*minutesP*/ 5, |
86 | /*secondsP*/ 6, |
87 | /*msecondsP*/ 7); |
88 | |
89 | DayCountProperty days = 0; |
90 | HourProperty hours = 0; |
91 | MinuteProperty minutes = 0; |
92 | SecondProperty sec = 0; |
93 | MSecondProperty msec = 0; |
94 | bool isPos; |
95 | |
96 | const DayTimeDuration::Ptr err(create(captTable: captureTable, lexical, isPositive: &isPos, years: 0, months: 0, days: &days, |
97 | hours: &hours, minutes: &minutes, seconds: &sec, mseconds: &msec)); |
98 | return err ? err : DayTimeDuration::Ptr(new DayTimeDuration(isPos, days, hours, minutes, |
99 | sec, msec)); |
100 | } |
101 | |
102 | DayTimeDuration::Ptr DayTimeDuration::fromComponents(const bool isPositive, |
103 | const DayCountProperty days, |
104 | const HourProperty hours, |
105 | const MinuteProperty minutes, |
106 | const SecondProperty seconds, |
107 | const MSecondProperty mseconds) |
108 | { |
109 | return DayTimeDuration::Ptr(new DayTimeDuration(isPositive, |
110 | days, |
111 | hours, |
112 | minutes, |
113 | seconds, |
114 | mseconds)); |
115 | } |
116 | |
117 | DayTimeDuration::Ptr DayTimeDuration::fromSeconds(const SecondCountProperty sourceSecs, |
118 | const MSecondProperty msecs) |
119 | { |
120 | Q_ASSERT(msecs >= 0); |
121 | const SecondCountProperty source = qAbs(t: sourceSecs); |
122 | const bool isPos = sourceSecs >= 0; |
123 | const SecondCountProperty secs = source % 60; |
124 | const MinuteCountProperty mins = (source / 60) % 60; |
125 | const HourCountProperty hours = source / (60 * 60) % 24; |
126 | const DayCountProperty days = source / (60 * 60) / 24; |
127 | |
128 | return DayTimeDuration::Ptr(new DayTimeDuration(isPos, days, hours, mins, secs, msecs)); |
129 | } |
130 | |
131 | QString DayTimeDuration::stringValue() const |
132 | { |
133 | QString retval; |
134 | |
135 | if(!m_isPositive) |
136 | retval.append(c: QLatin1Char('-')); |
137 | |
138 | retval.append(c: QLatin1Char('P')); |
139 | |
140 | if(m_days) |
141 | { |
142 | retval.append(s: QString::number(m_days)); |
143 | retval.append(c: QLatin1Char('D')); |
144 | } |
145 | |
146 | if (!m_hours && !m_minutes && !m_seconds && !m_mseconds) { |
147 | if(!m_days) |
148 | return QLatin1String("PT0S" ); |
149 | else |
150 | return retval; |
151 | } |
152 | |
153 | retval.append(c: QLatin1Char('T')); |
154 | |
155 | if(m_hours) |
156 | { |
157 | retval.append(s: QString::number(m_hours)); |
158 | retval.append(c: QLatin1Char('H')); |
159 | } |
160 | |
161 | if(m_minutes) |
162 | { |
163 | retval.append(s: QString::number(m_minutes)); |
164 | retval.append(c: QLatin1Char('M')); |
165 | } |
166 | |
167 | if (m_seconds || m_mseconds) { |
168 | retval.append(s: QString::number(m_seconds)); |
169 | |
170 | if(m_mseconds) |
171 | retval.append(s: serializeMSeconds(mseconds: m_mseconds)); |
172 | |
173 | retval.append(c: QLatin1Char('S')); |
174 | } |
175 | else if(!m_days && !m_hours && !m_minutes) |
176 | retval.append(s: QLatin1String("0S" )); |
177 | |
178 | return retval; |
179 | } |
180 | |
181 | AbstractDuration::Value DayTimeDuration::value() const |
182 | { |
183 | return ((m_days * 24 * 60 * 60 * 1000) + |
184 | (m_hours * 60 * 60 * 1000) + |
185 | (m_minutes * 60 * 1000) + |
186 | (m_seconds * 1000) + |
187 | m_mseconds) * (m_isPositive ? 1 : -1); |
188 | } |
189 | |
190 | Item DayTimeDuration::fromValue(const Value val) const |
191 | { |
192 | if(val == 0) |
193 | return toItem(atomicValue: CommonValues::DayTimeDurationZero); |
194 | else |
195 | return toItem(atomicValue: fromSeconds(sourceSecs: val / 1000, msecs: qAbs(t: val) % 1000)); |
196 | } |
197 | |
198 | ItemType::Ptr DayTimeDuration::type() const |
199 | { |
200 | return BuiltinTypes::xsDayTimeDuration; |
201 | } |
202 | |
203 | YearProperty DayTimeDuration::years() const |
204 | { |
205 | return 0; |
206 | } |
207 | |
208 | MonthProperty DayTimeDuration::months() const |
209 | { |
210 | return 0; |
211 | } |
212 | |
213 | DayCountProperty DayTimeDuration::days() const |
214 | { |
215 | return m_days; |
216 | } |
217 | |
218 | HourProperty DayTimeDuration::hours() const |
219 | { |
220 | return m_hours; |
221 | } |
222 | |
223 | MinuteProperty DayTimeDuration::minutes() const |
224 | { |
225 | return m_minutes; |
226 | } |
227 | |
228 | SecondProperty DayTimeDuration::seconds() const |
229 | { |
230 | return m_seconds; |
231 | } |
232 | |
233 | MSecondProperty DayTimeDuration::mseconds() const |
234 | { |
235 | return m_mseconds; |
236 | } |
237 | |
238 | QT_END_NAMESPACE |
239 | |