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