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