1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_connectivity.hxx"
30 
31 #include "file/FDateFunctions.hxx"
32 #include <tools/date.hxx>
33 #include <tools/time.hxx>
34 #include <tools/datetime.hxx>
35 
36 using namespace connectivity;
37 using namespace connectivity::file;
38 //------------------------------------------------------------------
39 ORowSetValue OOp_DayOfWeek::operate(const ORowSetValue& lhs) const
40 {
41 	if ( lhs.isNull() )
42 		return lhs;
43 
44 	sal_Int32 nRet = 0;
45 	::com::sun::star::util::Date aD = lhs;
46 	Date aDate(aD.Day,aD.Month,aD.Year);
47 	DayOfWeek eDayOfWeek = aDate.GetDayOfWeek();
48 	switch(eDayOfWeek)
49 	{
50 		case MONDAY:
51 			nRet = 2;
52 			break;
53 		case TUESDAY:
54 			nRet = 3;
55 			break;
56 		case WEDNESDAY:
57 			nRet = 4;
58 			break;
59 		case THURSDAY:
60 			nRet = 5;
61 			break;
62 		case FRIDAY:
63 			nRet = 6;
64 			break;
65 		case SATURDAY:
66 			nRet = 7;
67 			break;
68 		case SUNDAY:
69 			nRet = 1;
70 			break;
71 		default:
72 			OSL_ENSURE(0,"Error in enum values for date");
73 	}
74 	return nRet;
75 }
76 //------------------------------------------------------------------
77 ORowSetValue OOp_DayOfMonth::operate(const ORowSetValue& lhs) const
78 {
79 	if ( lhs.isNull() )
80 		return lhs;
81 
82 	::com::sun::star::util::Date aD = lhs;
83 	return static_cast<sal_Int16>(aD.Day);
84 }
85 //------------------------------------------------------------------
86 ORowSetValue OOp_DayOfYear::operate(const ORowSetValue& lhs) const
87 {
88 	if ( lhs.isNull() )
89 		return lhs;
90 
91 	::com::sun::star::util::Date aD = lhs;
92 	Date aDate(aD.Day,aD.Month,aD.Year);
93 	return static_cast<sal_Int16>(aDate.GetDayOfYear());
94 }
95 //------------------------------------------------------------------
96 ORowSetValue OOp_Month::operate(const ORowSetValue& lhs) const
97 {
98 	if ( lhs.isNull() )
99 		return lhs;
100 
101 	::com::sun::star::util::Date aD = lhs;
102 	return static_cast<sal_Int16>(aD.Month);
103 }
104 //------------------------------------------------------------------
105 ORowSetValue OOp_DayName::operate(const ORowSetValue& lhs) const
106 {
107 	if ( lhs.isNull() )
108 		return lhs;
109 
110 	::rtl::OUString sRet;
111 	::com::sun::star::util::Date aD = lhs;
112 	Date aDate(aD.Day,aD.Month,aD.Year);
113 	DayOfWeek eDayOfWeek = aDate.GetDayOfWeek();
114 	switch(eDayOfWeek)
115 	{
116 		case MONDAY:
117 			sRet = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Monday"));
118 			break;
119 		case TUESDAY:
120 			sRet = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Tuesday"));
121 			break;
122 		case WEDNESDAY:
123 			sRet = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Wednesday"));
124 			break;
125 		case THURSDAY:
126 			sRet = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Thursday"));
127 			break;
128 		case FRIDAY:
129 			sRet = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Friday"));
130 			break;
131 		case SATURDAY:
132 			sRet = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Saturday"));
133 			break;
134 		case SUNDAY:
135 			sRet = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Sunday"));
136 			break;
137 		default:
138 			OSL_ENSURE(0,"Error in enum values for date");
139 	}
140 	return sRet;
141 }
142 //------------------------------------------------------------------
143 ORowSetValue OOp_MonthName::operate(const ORowSetValue& lhs) const
144 {
145 	if ( lhs.isNull() )
146 		return lhs;
147 
148 	::rtl::OUString sRet;
149 	::com::sun::star::util::Date aD = lhs;
150 	switch(aD.Month)
151 	{
152 		case 1:
153 			sRet = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("January"));
154 			break;
155 		case 2:
156 			sRet = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("February"));
157 			break;
158 		case 3:
159 			sRet = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("March"));
160 			break;
161 		case 4:
162 			sRet = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("April"));
163 			break;
164 		case 5:
165 			sRet = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("May"));
166 			break;
167 		case 6:
168 			sRet = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("June"));
169 			break;
170 		case 7:
171 			sRet = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("July"));
172 			break;
173 		case 8:
174 			sRet = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("August"));
175 			break;
176 		case 9:
177 			sRet = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("September"));
178 			break;
179 		case 10:
180 			sRet = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("October"));
181 			break;
182 		case 11:
183 			sRet = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("November"));
184 			break;
185 		case 12:
186 			sRet = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("December"));
187 			break;
188 	}
189 	return sRet;
190 }
191 //------------------------------------------------------------------
192 ORowSetValue OOp_Quarter::operate(const ORowSetValue& lhs) const
193 {
194 	if ( lhs.isNull() )
195 		return lhs;
196 
197 	sal_Int32 nRet = 1;
198 	::com::sun::star::util::Date aD = lhs;
199 	Date aDate(aD.Day,aD.Month,aD.Year);
200 	if ( aD.Month >= 4 && aD.Month < 7 )
201 		nRet = 2;
202 	else if ( aD.Month >= 7 && aD.Month < 10 )
203 		nRet = 3;
204 	else if ( aD.Month >= 10 && aD.Month <= 12 )
205 		nRet = 4;
206 	return nRet;
207 }
208 //------------------------------------------------------------------
209 ORowSetValue OOp_Week::operate(const ::std::vector<ORowSetValue>& lhs) const
210 {
211 	if ( lhs.empty() || lhs.size() > 2 )
212 		return ORowSetValue();
213 
214 	size_t nSize = lhs.size();
215 
216 	::com::sun::star::util::Date aD = lhs[nSize-1];
217 	Date aDate(aD.Day,aD.Month,aD.Year);
218 
219 	sal_Int16 nStartDay = SUNDAY;
220 	if ( nSize == 2 && !lhs[0].isNull() )
221 		nStartDay = lhs[0];
222 
223 	return static_cast<sal_Int16>(aDate.GetWeekOfYear(static_cast<DayOfWeek>(nStartDay)));
224 }
225 // -----------------------------------------------------------------------------
226 ORowSetValue OOp_Year::operate(const ORowSetValue& lhs) const
227 {
228 	if ( lhs.isNull() )
229 		return lhs;
230 
231 	::com::sun::star::util::Date aD = lhs;
232 	return static_cast<sal_Int16>(aD.Year);
233 }
234 //------------------------------------------------------------------
235 ORowSetValue OOp_Hour::operate(const ORowSetValue& lhs) const
236 {
237 	if ( lhs.isNull() )
238 		return lhs;
239 
240 	::com::sun::star::util::Time aT = lhs;
241 	return static_cast<sal_Int16>(aT.Hours);
242 }
243 //------------------------------------------------------------------
244 ORowSetValue OOp_Minute::operate(const ORowSetValue& lhs) const
245 {
246 	if ( lhs.isNull() )
247 		return lhs;
248 
249 	::com::sun::star::util::Time aT = lhs;
250 	return static_cast<sal_Int16>(aT.Minutes);
251 }
252 //------------------------------------------------------------------
253 ORowSetValue OOp_Second::operate(const ORowSetValue& lhs) const
254 {
255 	if ( lhs.isNull() )
256 		return lhs;
257 
258 	::com::sun::star::util::Time aT = lhs;
259 	return static_cast<sal_Int16>(aT.Seconds);
260 }
261 //------------------------------------------------------------------
262 ORowSetValue OOp_CurDate::operate(const ::std::vector<ORowSetValue>& lhs) const
263 {
264 	if ( !lhs.empty() )
265 		return ORowSetValue();
266 
267 	Date aCurDate;
268 	return ::com::sun::star::util::Date(aCurDate.GetDay(),aCurDate.GetMonth(),aCurDate.GetYear());
269 }
270 //------------------------------------------------------------------
271 ORowSetValue OOp_CurTime::operate(const ::std::vector<ORowSetValue>& lhs) const
272 {
273 	if ( !lhs.empty() )
274 		return ORowSetValue();
275 
276 	Time aCurTime;
277 	return ::com::sun::star::util::Time(aCurTime.Get100Sec(),aCurTime.GetSec(),aCurTime.GetMin(),aCurTime.GetHour());
278 }
279 //------------------------------------------------------------------
280 ORowSetValue OOp_Now::operate(const ::std::vector<ORowSetValue>& lhs) const
281 {
282 	if ( !lhs.empty() )
283 		return ORowSetValue();
284 
285 	DateTime aCurTime;
286 	return ::com::sun::star::util::DateTime(aCurTime.Get100Sec(),aCurTime.GetSec(),aCurTime.GetMin(),aCurTime.GetHour(),
287 											aCurTime.GetDay(),aCurTime.GetMonth(),aCurTime.GetYear());
288 }
289 //------------------------------------------------------------------
290