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