1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the QtXmlPatterns module of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** No Commercial Usage
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
16 ** GNU Lesser General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU Lesser
18 ** General Public License version 2.1 as published by the Free Software
19 ** Foundation and appearing in the file LICENSE.LGPL included in the
20 ** packaging of this file. Please review the following information to
21 ** ensure the GNU Lesser General Public License version 2.1 requirements
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 ** In addition, as a special exception, Nokia gives you certain additional
25 ** rights. These rights are described in the Nokia Qt LGPL Exception
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
40 ****************************************************************************/
42 #include "qabstractdatetime_p.h"
43 #include "qcontextfns_p.h"
45 #include "qschemadatetime_p.h"
46 #include "qdaytimeduration_p.h"
47 #include "qpatternistlocale_p.h"
48 #include "qschematime_p.h"
50 #include "qtimezonefns_p.h"
54 using namespace QPatternist;
56 Item AdjustTimezone::evaluateSingleton(const DynamicContext::Ptr &context) const
61 * The maximum zone offset, @c PT14H, in milli seconds.
63 MSecLimit = 14 * 60/*M*/ * 60/*S*/ * 1000/*ms*/
67 const Item arg(m_operands.first()->evaluateSingleton(context));
71 QDateTime dt(arg.as<AbstractDateTime>()->toDateTime());
72 // TODO DT dt.setDateOnly(false);
73 Q_ASSERT(dt.isValid());
74 DayTimeDuration::Ptr tz;
76 if(m_operands.count() == 2)
77 tz = DayTimeDuration::Ptr(m_operands.at(1)->evaluateSingleton(context).as<DayTimeDuration>());
79 tz = context->implicitTimezone();
83 const MSecondCountProperty tzMSecs = tz->value();
85 if(tzMSecs % (1000 * 60) != 0)
87 context->error(QtXmlPatterns::tr("A zone offset must be in the "
88 "range %1..%2 inclusive. %3 is "
90 .arg(formatData("-PT14H"))
91 .arg(formatData("PT14H"))
92 .arg(formatData(tz->stringValue())),
93 ReportContext::FODT0003, this);
96 else if(tzMSecs > MSecLimit ||
99 context->error(QtXmlPatterns::tr("%1 is not a whole number of minutes.")
100 .arg(formatData(tz->stringValue())),
101 ReportContext::FODT0003, this);
105 const SecondCountProperty tzSecs = tzMSecs / 1000;
107 if(dt.timeSpec() == Qt::LocalTime) /* $arg has no time zone. */
109 /* "If $arg does not have a timezone component and $timezone is not
110 * the empty sequence, then the result is $arg with $timezone as
111 * the timezone component." */
112 //dt.setTimeSpec(QDateTime::Spec(QDateTime::OffsetFromUTC, tzSecs));
113 dt.setUtcOffset(tzSecs);
114 Q_ASSERT(dt.isValid());
115 return createValue(dt);
119 /* "If $arg has a timezone component and $timezone is not the empty sequence,
120 * then the result is an xs:dateTime value with a timezone component of
121 * $timezone that is equal to $arg." */
123 dt = dt.addSecs(tzSecs);
124 //dt.setTimeSpec(QDateTime::Spec(QDateTime::OffsetFromUTC, tzSecs));
125 dt.setUtcOffset(tzSecs);
126 Q_ASSERT(dt.isValid());
127 return createValue(dt);
131 { /* $timezone is the empty sequence. */
132 if(dt.timeSpec() == Qt::LocalTime) /* $arg has no time zone. */
134 /* "If $arg does not have a timezone component and $timezone is
135 * the empty sequence, then the result is $arg." */
140 /* "If $arg has a timezone component and $timezone is the empty sequence,
141 * then the result is the localized value of $arg without its timezone component." */
142 dt.setTimeSpec(Qt::LocalTime);
143 return createValue(dt);
148 Item AdjustDateTimeToTimezoneFN::createValue(const QDateTime &dt) const
150 Q_ASSERT(dt.isValid());
151 return DateTime::fromDateTime(dt);
154 Item AdjustDateToTimezoneFN::createValue(const QDateTime &dt) const
156 Q_ASSERT(dt.isValid());
157 return Date::fromDateTime(dt);
160 Item AdjustTimeToTimezoneFN::createValue(const QDateTime &dt) const
162 Q_ASSERT(dt.isValid());
163 return SchemaTime::fromDateTime(dt);