Qt 4.8
qtimezonefns.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
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 Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/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 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #include "qabstractdatetime_p.h"
43 #include "qcontextfns_p.h"
44 #include "qdate_p.h"
45 #include "qschemadatetime_p.h"
46 #include "qdaytimeduration_p.h"
47 #include "qpatternistlocale_p.h"
48 #include "qschematime_p.h"
49 
50 #include "qtimezonefns_p.h"
51 
53 
54 using namespace QPatternist;
55 
57 {
58  enum
59  {
63  MSecLimit = 14 * 60/*M*/ * 60/*S*/ * 1000/*ms*/
64  };
65 
66 
67  const Item arg(m_operands.first()->evaluateSingleton(context));
68  if(!arg)
69  return Item();
70 
71  QDateTime dt(arg.as<AbstractDateTime>()->toDateTime());
72  // TODO DT dt.setDateOnly(false);
73  Q_ASSERT(dt.isValid());
75 
76  if(m_operands.count() == 2)
78  else
79  tz = context->implicitTimezone();
80 
81  if(tz)
82  {
83  const MSecondCountProperty tzMSecs = tz->value();
84 
85  if(tzMSecs % (1000 * 60) != 0)
86  {
87  context->error(QtXmlPatterns::tr("A zone offset must be in the "
88  "range %1..%2 inclusive. %3 is "
89  "out of range.")
90  .arg(formatData("-PT14H"))
91  .arg(formatData("PT14H"))
92  .arg(formatData(tz->stringValue())),
94  return Item();
95  }
96  else if(tzMSecs > MSecLimit ||
97  tzMSecs < -MSecLimit)
98  {
99  context->error(QtXmlPatterns::tr("%1 is not a whole number of minutes.")
100  .arg(formatData(tz->stringValue())),
102  return Item();
103  }
104 
105  const SecondCountProperty tzSecs = tzMSecs / 1000;
106 
107  if(dt.timeSpec() == Qt::LocalTime) /* $arg has no time zone. */
108  {
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);
116  }
117  else
118  {
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." */
122  dt = dt.toUTC();
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);
128  }
129  }
130  else
131  { /* $timezone is the empty sequence. */
132  if(dt.timeSpec() == Qt::LocalTime) /* $arg has no time zone. */
133  {
134  /* "If $arg does not have a timezone component and $timezone is
135  * the empty sequence, then the result is $arg." */
136  return arg;
137  }
138  else
139  {
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);
144  }
145  }
146 }
147 
149 {
150  Q_ASSERT(dt.isValid());
151  return DateTime::fromDateTime(dt);
152 }
153 
155 {
156  Q_ASSERT(dt.isValid());
157  return Date::fromDateTime(dt);
158 }
159 
161 {
162  Q_ASSERT(dt.isValid());
163  return SchemaTime::fromDateTime(dt);
164 }
165 
Contains classes implementing the functions found in XQuery 1.0 and XPath 2.0 Functions and Operators...
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
Base class for classes implementing values related to time, date or both.
static Date::Ptr fromDateTime(const QDateTime &date)
Definition: qdate.cpp:88
virtual QExplicitlySharedDataPointer< DayTimeDuration > implicitTimezone() const =0
bool isValid() const
Returns true if both the date and the time are valid; otherwise returns false.
Definition: qdatetime.cpp:2346
int count(const T &t) const
Returns the number of occurrences of value in the list.
Definition: qlist.h:891
static DateTime::Ptr fromDateTime(const QDateTime &dt)
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
Contains class DateTime. This file was originally called qdatetime_p.h, but various build systems can...
virtual Item evaluateSingleton(const DynamicContext::Ptr &context) const
virtual Item createValue(const QDateTime &dt) const
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
void error(const QString &message, const ReportContext::ErrorCode errorCode, const QSourceLocation &sourceLocation)
const T & at(int i) const
Returns the item at index position i in the list.
Definition: qlist.h:468
The namespace for the internal API of QtXmlPatterns.
virtual Item createValue(const QDateTime &dt) const
const QDateTime & toDateTime() const
static QString formatData(const QString &data)
T & first()
Returns a reference to the first item in the list.
Definition: qlist.h:282
Implements the value instance of the xs:dayTimeDuration type.
Contains classes implementing the functions found in XQuery 1.0 and XPath 2.0 Functions and Operators...
Contains functions used for formatting arguments, such as keywords and paths, in translated strings...
The QDateTime class provides date and time functions.
Definition: qdatetime.h:216
Represents an item in the XPath 2.0 Data Model.
Definition: qitem_p.h:182
virtual Item createValue(const QDateTime &dt) const =0
static SchemaTime::Ptr fromDateTime(const QDateTime &dt)
Definition: qschematime.cpp:87
virtual Item createValue(const QDateTime &dt) const
qint64 MSecondCountProperty
TCastTarget * as() const
Definition: qitem_p.h:278
qint32 SecondCountProperty
virtual Item evaluateSingleton(const DynamicContext::Ptr &context) const