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