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