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//
41// W A R N I N G
42// -------------
43//
44// This file is not part of the Qt API. It exists purely as an
45// implementation detail. This header file may change from version to
46// version without notice, or even be removed.
47//
48// We mean it.
49//
50
51/**
52 * @file
53 * @short This file is included by qdatetimefns_p.h.
54 * If you need includes in this file, put them in qdatetimefns_p.h, outside of the namespace.
55 */
56
57template<typename TSubClass>
58Item ExtractFromDurationFN<TSubClass>::evaluateSingleton(const DynamicContext::Ptr &context) const
59{
60 const Item item(m_operands.first()->evaluateSingleton(context));
61 if(item)
62 {
63 return static_cast<const TSubClass *>(this)->
64 extract(item.as<AbstractDuration>());
65 }
66 else
67 return Item();
68}
69
70Item YearsFromDurationFN::extract(const AbstractDuration *const duration) const
71{
72 return Integer::fromValue(num: duration->years() * (duration->isPositive() ? 1 : -1));
73}
74
75Item MonthsFromDurationFN::extract(const AbstractDuration *const duration) const
76{
77 return Integer::fromValue(num: duration->months() * (duration->isPositive() ? 1 : -1));
78}
79
80Item DaysFromDurationFN::extract(const AbstractDuration *const duration) const
81{
82 return Integer::fromValue(num: duration->days() * (duration->isPositive() ? 1 : -1));
83}
84
85Item HoursFromDurationFN::extract(const AbstractDuration *const duration) const
86{
87 return Integer::fromValue(num: duration->hours() * (duration->isPositive() ? 1 : -1));
88}
89
90Item MinutesFromDurationFN::extract(const AbstractDuration *const duration) const
91{
92 return Integer::fromValue(num: duration->minutes() * (duration->isPositive() ? 1 : -1));
93}
94
95Item SecondsFromDurationFN::extract(const AbstractDuration *const duration) const
96{
97 return toItem(atomicValue: Decimal::fromValue(num: (duration->seconds() + duration->mseconds() / 1000.0) *
98 (duration->isPositive() ? 1 : -1)));
99}
100
101template<typename TSubClass>
102Item ExtractFromDateTimeFN<TSubClass>::evaluateSingleton(const DynamicContext::Ptr &context) const
103{
104 const Item item(m_operands.first()->evaluateSingleton(context));
105 if(item)
106 {
107 return static_cast<const TSubClass *>(this)->
108 extract(item.as<AbstractDateTime>()->toDateTime());
109 }
110 else
111 return Item();
112}
113
114Item YearFromAbstractDateTimeFN::extract(const QDateTime &dt) const
115{
116 return Integer::fromValue(num: dt.date().year());
117}
118
119Item DayFromAbstractDateTimeFN::extract(const QDateTime &dt) const
120{
121 return Integer::fromValue(num: dt.date().day());
122}
123
124Item MinutesFromAbstractDateTimeFN::extract(const QDateTime &dt) const
125{
126 return Integer::fromValue(num: dt.time().minute());
127}
128
129Item SecondsFromAbstractDateTimeFN::extract(const QDateTime &dt) const
130{
131 const QTime time(dt.time());
132 return toItem(atomicValue: Decimal::fromValue(num: time.second() + time.msec() / 1000.0));
133}
134
135Item TimezoneFromAbstractDateTimeFN::extract(const QDateTime &dt) const
136{
137 if(dt.timeSpec() == Qt::UTC)
138 return toItem(atomicValue: CommonValues::DayTimeDurationZero);
139 else if(dt.timeSpec() == Qt::OffsetFromUTC)
140 return toItem(atomicValue: DayTimeDuration::fromSeconds(secs: dt.offsetFromUtc()));
141 else
142 return Item();
143}
144
145Item MonthFromAbstractDateTimeFN::extract(const QDateTime &dt) const
146{
147 return Integer::fromValue(num: dt.date().month());
148}
149
150Item HoursFromAbstractDateTimeFN::extract(const QDateTime &dt) const
151{
152 return Integer::fromValue(num: dt.time().hour());
153}
154
155

source code of qtxmlpatterns/src/xmlpatterns/functions/qdatetimefns_tpl_p.h