1 /****************************************************************************
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
6 ** This file is part of the QtXmlPatterns module of the Qt Toolkit.
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.
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.
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.
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.
40 ****************************************************************************/
44 * @short This file is included by qdatetimefns_p.h.
45 * If you need includes in this file, put them in qdatetimefns_p.h, outside of the namespace.
48 template<typename TSubClass>
49 Item ExtractFromDurationFN<TSubClass>::evaluateSingleton(const DynamicContext::Ptr &context) const
51 const Item item(m_operands.first()->evaluateSingleton(context));
54 return static_cast<const TSubClass *>(this)->
55 extract(item.as<AbstractDuration>());
61 Item YearsFromDurationFN::extract(const AbstractDuration *const duration) const
63 return Integer::fromValue(duration->years() * (duration->isPositive() ? 1 : -1));
66 Item MonthsFromDurationFN::extract(const AbstractDuration *const duration) const
68 return Integer::fromValue(duration->months() * (duration->isPositive() ? 1 : -1));
71 Item DaysFromDurationFN::extract(const AbstractDuration *const duration) const
73 return Integer::fromValue(duration->days() * (duration->isPositive() ? 1 : -1));
76 Item HoursFromDurationFN::extract(const AbstractDuration *const duration) const
78 return Integer::fromValue(duration->hours() * (duration->isPositive() ? 1 : -1));
81 Item MinutesFromDurationFN::extract(const AbstractDuration *const duration) const
83 return Integer::fromValue(duration->minutes() * (duration->isPositive() ? 1 : -1));
86 Item SecondsFromDurationFN::extract(const AbstractDuration *const duration) const
88 return toItem(Decimal::fromValue((duration->seconds() + duration->mseconds() / 1000.0) *
89 (duration->isPositive() ? 1 : -1)));
92 template<typename TSubClass>
93 Item ExtractFromDateTimeFN<TSubClass>::evaluateSingleton(const DynamicContext::Ptr &context) const
95 const Item item(m_operands.first()->evaluateSingleton(context));
98 return static_cast<const TSubClass *>(this)->
99 extract(item.as<AbstractDateTime>()->toDateTime());
105 Item YearFromAbstractDateTimeFN::extract(const QDateTime &dt) const
107 return Integer::fromValue(dt.date().year());
110 Item DayFromAbstractDateTimeFN::extract(const QDateTime &dt) const
112 return Integer::fromValue(dt.date().day());
115 Item MinutesFromAbstractDateTimeFN::extract(const QDateTime &dt) const
117 return Integer::fromValue(dt.time().minute());
120 Item SecondsFromAbstractDateTimeFN::extract(const QDateTime &dt) const
122 const QTime time(dt.time());
123 return toItem(Decimal::fromValue(time.second() + time.msec() / 1000.0));
126 Item TimezoneFromAbstractDateTimeFN::extract(const QDateTime &dt) const
128 if(dt.timeSpec() == Qt::UTC)
129 return toItem(CommonValues::DayTimeDurationZero);
130 else if(dt.timeSpec() == Qt::OffsetFromUTC)
131 return toItem(DayTimeDuration::fromSeconds(dt.utcOffset()));
136 Item MonthFromAbstractDateTimeFN::extract(const QDateTime &dt) const
138 return Integer::fromValue(dt.date().month());
141 Item HoursFromAbstractDateTimeFN::extract(const QDateTime &dt) const
143 return Integer::fromValue(dt.time().hour());