1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_connectivity.hxx"
26 
27 #include "file/FDateFunctions.hxx"
28 #include <tools/date.hxx>
29 #include <tools/time.hxx>
30 #include <tools/datetime.hxx>
31 
32 using namespace connectivity;
33 using namespace connectivity::file;
34 //------------------------------------------------------------------
operate(const ORowSetValue & lhs) const35 ORowSetValue OOp_DayOfWeek::operate(const ORowSetValue& lhs) const
36 {
37 	if ( lhs.isNull() )
38 		return lhs;
39 
40 	sal_Int32 nRet = 0;
41 	::com::sun::star::util::Date aD = lhs;
42 	Date aDate(aD.Day,aD.Month,aD.Year);
43 	DayOfWeek eDayOfWeek = aDate.GetDayOfWeek();
44 	switch(eDayOfWeek)
45 	{
46 		case MONDAY:
47 			nRet = 2;
48 			break;
49 		case TUESDAY:
50 			nRet = 3;
51 			break;
52 		case WEDNESDAY:
53 			nRet = 4;
54 			break;
55 		case THURSDAY:
56 			nRet = 5;
57 			break;
58 		case FRIDAY:
59 			nRet = 6;
60 			break;
61 		case SATURDAY:
62 			nRet = 7;
63 			break;
64 		case SUNDAY:
65 			nRet = 1;
66 			break;
67 		default:
68 			OSL_ENSURE(0,"Error in enum values for date");
69 	}
70 	return nRet;
71 }
72 //------------------------------------------------------------------
operate(const ORowSetValue & lhs) const73 ORowSetValue OOp_DayOfMonth::operate(const ORowSetValue& lhs) const
74 {
75 	if ( lhs.isNull() )
76 		return lhs;
77 
78 	::com::sun::star::util::Date aD = lhs;
79 	return static_cast<sal_Int16>(aD.Day);
80 }
81 //------------------------------------------------------------------
operate(const ORowSetValue & lhs) const82 ORowSetValue OOp_DayOfYear::operate(const ORowSetValue& lhs) const
83 {
84 	if ( lhs.isNull() )
85 		return lhs;
86 
87 	::com::sun::star::util::Date aD = lhs;
88 	Date aDate(aD.Day,aD.Month,aD.Year);
89 	return static_cast<sal_Int16>(aDate.GetDayOfYear());
90 }
91 //------------------------------------------------------------------
operate(const ORowSetValue & lhs) const92 ORowSetValue OOp_Month::operate(const ORowSetValue& lhs) const
93 {
94 	if ( lhs.isNull() )
95 		return lhs;
96 
97 	::com::sun::star::util::Date aD = lhs;
98 	return static_cast<sal_Int16>(aD.Month);
99 }
100 //------------------------------------------------------------------
operate(const ORowSetValue & lhs) const101 ORowSetValue OOp_DayName::operate(const ORowSetValue& lhs) const
102 {
103 	if ( lhs.isNull() )
104 		return lhs;
105 
106 	::rtl::OUString sRet;
107 	::com::sun::star::util::Date aD = lhs;
108 	Date aDate(aD.Day,aD.Month,aD.Year);
109 	DayOfWeek eDayOfWeek = aDate.GetDayOfWeek();
110 	switch(eDayOfWeek)
111 	{
112 		case MONDAY:
113 			sRet = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Monday"));
114 			break;
115 		case TUESDAY:
116 			sRet = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Tuesday"));
117 			break;
118 		case WEDNESDAY:
119 			sRet = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Wednesday"));
120 			break;
121 		case THURSDAY:
122 			sRet = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Thursday"));
123 			break;
124 		case FRIDAY:
125 			sRet = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Friday"));
126 			break;
127 		case SATURDAY:
128 			sRet = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Saturday"));
129 			break;
130 		case SUNDAY:
131 			sRet = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Sunday"));
132 			break;
133 		default:
134 			OSL_ENSURE(0,"Error in enum values for date");
135 	}
136 	return sRet;
137 }
138 //------------------------------------------------------------------
operate(const ORowSetValue & lhs) const139 ORowSetValue OOp_MonthName::operate(const ORowSetValue& lhs) const
140 {
141 	if ( lhs.isNull() )
142 		return lhs;
143 
144 	::rtl::OUString sRet;
145 	::com::sun::star::util::Date aD = lhs;
146 	switch(aD.Month)
147 	{
148 		case 1:
149 			sRet = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("January"));
150 			break;
151 		case 2:
152 			sRet = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("February"));
153 			break;
154 		case 3:
155 			sRet = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("March"));
156 			break;
157 		case 4:
158 			sRet = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("April"));
159 			break;
160 		case 5:
161 			sRet = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("May"));
162 			break;
163 		case 6:
164 			sRet = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("June"));
165 			break;
166 		case 7:
167 			sRet = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("July"));
168 			break;
169 		case 8:
170 			sRet = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("August"));
171 			break;
172 		case 9:
173 			sRet = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("September"));
174 			break;
175 		case 10:
176 			sRet = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("October"));
177 			break;
178 		case 11:
179 			sRet = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("November"));
180 			break;
181 		case 12:
182 			sRet = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("December"));
183 			break;
184 	}
185 	return sRet;
186 }
187 //------------------------------------------------------------------
operate(const ORowSetValue & lhs) const188 ORowSetValue OOp_Quarter::operate(const ORowSetValue& lhs) const
189 {
190 	if ( lhs.isNull() )
191 		return lhs;
192 
193 	sal_Int32 nRet = 1;
194 	::com::sun::star::util::Date aD = lhs;
195 	Date aDate(aD.Day,aD.Month,aD.Year);
196 	if ( aD.Month >= 4 && aD.Month < 7 )
197 		nRet = 2;
198 	else if ( aD.Month >= 7 && aD.Month < 10 )
199 		nRet = 3;
200 	else if ( aD.Month >= 10 && aD.Month <= 12 )
201 		nRet = 4;
202 	return nRet;
203 }
204 //------------------------------------------------------------------
operate(const::std::vector<ORowSetValue> & lhs) const205 ORowSetValue OOp_Week::operate(const ::std::vector<ORowSetValue>& lhs) const
206 {
207 	if ( lhs.empty() || lhs.size() > 2 )
208 		return ORowSetValue();
209 
210 	size_t nSize = lhs.size();
211 
212 	::com::sun::star::util::Date aD = lhs[nSize-1];
213 	Date aDate(aD.Day,aD.Month,aD.Year);
214 
215 	sal_Int16 nStartDay = SUNDAY;
216 	if ( nSize == 2 && !lhs[0].isNull() )
217 		nStartDay = lhs[0];
218 
219 	return static_cast<sal_Int16>(aDate.GetWeekOfYear(static_cast<DayOfWeek>(nStartDay)));
220 }
221 // -----------------------------------------------------------------------------
operate(const ORowSetValue & lhs) const222 ORowSetValue OOp_Year::operate(const ORowSetValue& lhs) const
223 {
224 	if ( lhs.isNull() )
225 		return lhs;
226 
227 	::com::sun::star::util::Date aD = lhs;
228 	return static_cast<sal_Int16>(aD.Year);
229 }
230 //------------------------------------------------------------------
operate(const ORowSetValue & lhs) const231 ORowSetValue OOp_Hour::operate(const ORowSetValue& lhs) const
232 {
233 	if ( lhs.isNull() )
234 		return lhs;
235 
236 	::com::sun::star::util::Time aT = lhs;
237 	return static_cast<sal_Int16>(aT.Hours);
238 }
239 //------------------------------------------------------------------
operate(const ORowSetValue & lhs) const240 ORowSetValue OOp_Minute::operate(const ORowSetValue& lhs) const
241 {
242 	if ( lhs.isNull() )
243 		return lhs;
244 
245 	::com::sun::star::util::Time aT = lhs;
246 	return static_cast<sal_Int16>(aT.Minutes);
247 }
248 //------------------------------------------------------------------
operate(const ORowSetValue & lhs) const249 ORowSetValue OOp_Second::operate(const ORowSetValue& lhs) const
250 {
251 	if ( lhs.isNull() )
252 		return lhs;
253 
254 	::com::sun::star::util::Time aT = lhs;
255 	return static_cast<sal_Int16>(aT.Seconds);
256 }
257 //------------------------------------------------------------------
operate(const::std::vector<ORowSetValue> & lhs) const258 ORowSetValue OOp_CurDate::operate(const ::std::vector<ORowSetValue>& lhs) const
259 {
260 	if ( !lhs.empty() )
261 		return ORowSetValue();
262 
263 	Date aCurDate;
264 	return ::com::sun::star::util::Date(aCurDate.GetDay(),aCurDate.GetMonth(),aCurDate.GetYear());
265 }
266 //------------------------------------------------------------------
operate(const::std::vector<ORowSetValue> & lhs) const267 ORowSetValue OOp_CurTime::operate(const ::std::vector<ORowSetValue>& lhs) const
268 {
269 	if ( !lhs.empty() )
270 		return ORowSetValue();
271 
272 	Time aCurTime;
273 	return ::com::sun::star::util::Time(aCurTime.Get100Sec(),aCurTime.GetSec(),aCurTime.GetMin(),aCurTime.GetHour());
274 }
275 //------------------------------------------------------------------
operate(const::std::vector<ORowSetValue> & lhs) const276 ORowSetValue OOp_Now::operate(const ::std::vector<ORowSetValue>& lhs) const
277 {
278 	if ( !lhs.empty() )
279 		return ORowSetValue();
280 
281 	DateTime aCurTime;
282 	return ::com::sun::star::util::DateTime(aCurTime.Get100Sec(),aCurTime.GetSec(),aCurTime.GetMin(),aCurTime.GetHour(),
283 											aCurTime.GetDay(),aCurTime.GetMonth(),aCurTime.GetYear());
284 }
285 //------------------------------------------------------------------
286