| 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 "qcontextfns_p.h" |
| 42 | #include "qdate_p.h" |
| 43 | #include "qschemadatetime_p.h" |
| 44 | #include "qdaytimeduration_p.h" |
| 45 | #include "qpatternistlocale_p.h" |
| 46 | #include "qschematime_p.h" |
| 47 | |
| 48 | #include "qtimezonefns_p.h" |
| 49 | |
| 50 | QT_BEGIN_NAMESPACE |
| 51 | |
| 52 | using namespace QPatternist; |
| 53 | |
| 54 | Item AdjustTimezone::evaluateSingleton(const DynamicContext::Ptr &context) const |
| 55 | { |
| 56 | enum |
| 57 | { |
| 58 | /** |
| 59 | * The maximum zone offset, @c PT14H, in milli seconds. |
| 60 | */ |
| 61 | MSecLimit = 14 * 60/*M*/ * 60/*S*/ * 1000/*ms*/ |
| 62 | }; |
| 63 | |
| 64 | |
| 65 | const Item arg(m_operands.first()->evaluateSingleton(context)); |
| 66 | if(!arg) |
| 67 | return Item(); |
| 68 | |
| 69 | QDateTime dt(arg.as<AbstractDateTime>()->toDateTime()); |
| 70 | // TODO DT dt.setDateOnly(false); |
| 71 | Q_ASSERT(dt.isValid()); |
| 72 | DayTimeDuration::Ptr tz; |
| 73 | |
| 74 | if(m_operands.count() == 2) |
| 75 | tz = DayTimeDuration::Ptr(m_operands.at(i: 1)->evaluateSingleton(context).as<DayTimeDuration>()); |
| 76 | else |
| 77 | tz = context->implicitTimezone(); |
| 78 | |
| 79 | if(tz) |
| 80 | { |
| 81 | const MSecondCountProperty tzMSecs = tz->value(); |
| 82 | |
| 83 | if(tzMSecs % (1000 * 60) != 0) |
| 84 | { |
| 85 | context->error(message: QtXmlPatterns::tr(sourceText: "A zone offset must be in the " |
| 86 | "range %1..%2 inclusive. %3 is " |
| 87 | "out of range." ) |
| 88 | .arg(a: formatData(data: "-PT14H" )) |
| 89 | .arg(a: formatData(data: "PT14H" )) |
| 90 | .arg(a: formatData(data: tz->stringValue())), |
| 91 | errorCode: ReportContext::FODT0003, reflection: this); |
| 92 | return Item(); |
| 93 | } |
| 94 | else if(tzMSecs > MSecLimit || |
| 95 | tzMSecs < -MSecLimit) |
| 96 | { |
| 97 | context->error(message: QtXmlPatterns::tr(sourceText: "%1 is not a whole number of minutes." ) |
| 98 | .arg(a: formatData(data: tz->stringValue())), |
| 99 | errorCode: ReportContext::FODT0003, reflection: this); |
| 100 | return Item(); |
| 101 | } |
| 102 | |
| 103 | const SecondCountProperty tzSecs = tzMSecs / 1000; |
| 104 | |
| 105 | if(dt.timeSpec() == Qt::LocalTime) /* $arg has no time zone. */ |
| 106 | { |
| 107 | /* "If $arg does not have a timezone component and $timezone is not |
| 108 | * the empty sequence, then the result is $arg with $timezone as |
| 109 | * the timezone component." */ |
| 110 | //dt.setTimeSpec(QDateTime::Spec(QDateTime::OffsetFromUTC, tzSecs)); |
| 111 | dt.setOffsetFromUtc(tzSecs); |
| 112 | Q_ASSERT(dt.isValid()); |
| 113 | return createValue(dt); |
| 114 | } |
| 115 | else |
| 116 | { |
| 117 | /* "If $arg has a timezone component and $timezone is not the empty sequence, |
| 118 | * then the result is an xs:dateTime value with a timezone component of |
| 119 | * $timezone that is equal to $arg." */ |
| 120 | dt = dt.toUTC(); |
| 121 | dt = dt.addSecs(secs: tzSecs); |
| 122 | //dt.setTimeSpec(QDateTime::Spec(QDateTime::OffsetFromUTC, tzSecs)); |
| 123 | dt.setOffsetFromUtc(tzSecs); |
| 124 | Q_ASSERT(dt.isValid()); |
| 125 | return createValue(dt); |
| 126 | } |
| 127 | } |
| 128 | else |
| 129 | { /* $timezone is the empty sequence. */ |
| 130 | if(dt.timeSpec() == Qt::LocalTime) /* $arg has no time zone. */ |
| 131 | { |
| 132 | /* "If $arg does not have a timezone component and $timezone is |
| 133 | * the empty sequence, then the result is $arg." */ |
| 134 | return arg; |
| 135 | } |
| 136 | else |
| 137 | { |
| 138 | /* "If $arg has a timezone component and $timezone is the empty sequence, |
| 139 | * then the result is the localized value of $arg without its timezone component." */ |
| 140 | dt.setTimeSpec(Qt::LocalTime); |
| 141 | return createValue(dt); |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | Item AdjustDateTimeToTimezoneFN::createValue(const QDateTime &dt) const |
| 147 | { |
| 148 | Q_ASSERT(dt.isValid()); |
| 149 | return DateTime::fromDateTime(dt); |
| 150 | } |
| 151 | |
| 152 | Item AdjustDateToTimezoneFN::createValue(const QDateTime &dt) const |
| 153 | { |
| 154 | Q_ASSERT(dt.isValid()); |
| 155 | return Date::fromDateTime(date: dt); |
| 156 | } |
| 157 | |
| 158 | Item AdjustTimeToTimezoneFN::createValue(const QDateTime &dt) const |
| 159 | { |
| 160 | Q_ASSERT(dt.isValid()); |
| 161 | return SchemaTime::fromDateTime(dt); |
| 162 | } |
| 163 | |
| 164 | QT_END_NAMESPACE |
| 165 | |