Version 4.3.0.0.beta1, tag libreoffice-4.3.0.0.beta1
[LibreOffice.git] / connectivity / source / drivers / file / FDateFunctions.cxx
blobf6bfca8af2070e3458fbb2f515428409dc7fe9c3
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 #include "file/FDateFunctions.hxx"
22 #include <tools/date.hxx>
23 #include <tools/time.hxx>
24 #include <tools/datetime.hxx>
26 using namespace connectivity;
27 using namespace connectivity::file;
29 ORowSetValue OOp_DayOfWeek::operate(const ORowSetValue& lhs) const
31 if ( lhs.isNull() )
32 return lhs;
34 sal_Int32 nRet = 0;
35 ::com::sun::star::util::Date aD = lhs;
36 Date aDate(aD.Day,aD.Month,aD.Year);
37 DayOfWeek eDayOfWeek = aDate.GetDayOfWeek();
38 switch(eDayOfWeek)
40 case MONDAY:
41 nRet = 2;
42 break;
43 case TUESDAY:
44 nRet = 3;
45 break;
46 case WEDNESDAY:
47 nRet = 4;
48 break;
49 case THURSDAY:
50 nRet = 5;
51 break;
52 case FRIDAY:
53 nRet = 6;
54 break;
55 case SATURDAY:
56 nRet = 7;
57 break;
58 case SUNDAY:
59 nRet = 1;
60 break;
61 default:
62 OSL_FAIL("Error in enum values for date");
64 return nRet;
67 ORowSetValue OOp_DayOfMonth::operate(const ORowSetValue& lhs) const
69 if ( lhs.isNull() )
70 return lhs;
72 ::com::sun::star::util::Date aD = lhs;
73 return static_cast<sal_Int16>(aD.Day);
76 ORowSetValue OOp_DayOfYear::operate(const ORowSetValue& lhs) const
78 if ( lhs.isNull() )
79 return lhs;
81 ::com::sun::star::util::Date aD = lhs;
82 Date aDate(aD.Day,aD.Month,aD.Year);
83 return static_cast<sal_Int16>(aDate.GetDayOfYear());
86 ORowSetValue OOp_Month::operate(const ORowSetValue& lhs) const
88 if ( lhs.isNull() )
89 return lhs;
91 ::com::sun::star::util::Date aD = lhs;
92 return static_cast<sal_Int16>(aD.Month);
95 ORowSetValue OOp_DayName::operate(const ORowSetValue& lhs) const
97 if ( lhs.isNull() )
98 return lhs;
100 OUString sRet;
101 ::com::sun::star::util::Date aD = lhs;
102 Date aDate(aD.Day,aD.Month,aD.Year);
103 DayOfWeek eDayOfWeek = aDate.GetDayOfWeek();
104 switch(eDayOfWeek)
106 case MONDAY:
107 sRet = "Monday";
108 break;
109 case TUESDAY:
110 sRet = "Tuesday";
111 break;
112 case WEDNESDAY:
113 sRet = "Wednesday";
114 break;
115 case THURSDAY:
116 sRet = "Thursday";
117 break;
118 case FRIDAY:
119 sRet = "Friday";
120 break;
121 case SATURDAY:
122 sRet = "Saturday";
123 break;
124 case SUNDAY:
125 sRet = "Sunday";
126 break;
127 default:
128 OSL_FAIL("Error in enum values for date");
130 return sRet;
133 ORowSetValue OOp_MonthName::operate(const ORowSetValue& lhs) const
135 if ( lhs.isNull() )
136 return lhs;
138 OUString sRet;
139 ::com::sun::star::util::Date aD = lhs;
140 switch(aD.Month)
142 case 1:
143 sRet = "January";
144 break;
145 case 2:
146 sRet = "February";
147 break;
148 case 3:
149 sRet = "March";
150 break;
151 case 4:
152 sRet = "April";
153 break;
154 case 5:
155 sRet = "May";
156 break;
157 case 6:
158 sRet = "June";
159 break;
160 case 7:
161 sRet = "July";
162 break;
163 case 8:
164 sRet = "August";
165 break;
166 case 9:
167 sRet = "September";
168 break;
169 case 10:
170 sRet = "October";
171 break;
172 case 11:
173 sRet = "November";
174 break;
175 case 12:
176 sRet = "December";
177 break;
179 return sRet;
182 ORowSetValue OOp_Quarter::operate(const ORowSetValue& lhs) const
184 if ( lhs.isNull() )
185 return lhs;
187 sal_Int32 nRet = 1;
188 ::com::sun::star::util::Date aD = lhs;
189 if ( aD.Month >= 4 && aD.Month < 7 )
190 nRet = 2;
191 else if ( aD.Month >= 7 && aD.Month < 10 )
192 nRet = 3;
193 else if ( aD.Month >= 10 && aD.Month <= 12 )
194 nRet = 4;
195 return nRet;
198 ORowSetValue OOp_Week::operate(const ::std::vector<ORowSetValue>& lhs) const
200 if ( lhs.empty() || lhs.size() > 2 )
201 return ORowSetValue();
203 size_t nSize = lhs.size();
205 ::com::sun::star::util::Date aD = lhs[nSize-1];
206 Date aDate(aD.Day,aD.Month,aD.Year);
208 sal_Int16 nStartDay = SUNDAY;
209 if ( nSize == 2 && !lhs[0].isNull() )
210 nStartDay = lhs[0];
212 return static_cast<sal_Int16>(aDate.GetWeekOfYear(static_cast<DayOfWeek>(nStartDay)));
215 ORowSetValue OOp_Year::operate(const ORowSetValue& lhs) const
217 if ( lhs.isNull() )
218 return lhs;
220 ::com::sun::star::util::Date aD = lhs;
221 return static_cast<sal_Int16>(aD.Year);
224 ORowSetValue OOp_Hour::operate(const ORowSetValue& lhs) const
226 if ( lhs.isNull() )
227 return lhs;
229 ::com::sun::star::util::Time aT = lhs;
230 return static_cast<sal_Int16>(aT.Hours);
233 ORowSetValue OOp_Minute::operate(const ORowSetValue& lhs) const
235 if ( lhs.isNull() )
236 return lhs;
238 ::com::sun::star::util::Time aT = lhs;
239 return static_cast<sal_Int16>(aT.Minutes);
242 ORowSetValue OOp_Second::operate(const ORowSetValue& lhs) const
244 if ( lhs.isNull() )
245 return lhs;
247 ::com::sun::star::util::Time aT = lhs;
248 return static_cast<sal_Int16>(aT.Seconds);
251 ORowSetValue OOp_CurDate::operate(const ::std::vector<ORowSetValue>& lhs) const
253 if ( !lhs.empty() )
254 return ORowSetValue();
256 Date aCurDate( Date::SYSTEM );
257 return ::com::sun::star::util::Date(aCurDate.GetDay(),aCurDate.GetMonth(),aCurDate.GetYear());
260 ORowSetValue OOp_CurTime::operate(const ::std::vector<ORowSetValue>& lhs) const
262 if ( !lhs.empty() )
263 return ORowSetValue();
265 Time aCurTime( Time::SYSTEM );
266 return ::com::sun::star::util::Time(aCurTime.GetNanoSec(),
267 aCurTime.GetSec(), aCurTime.GetMin(), aCurTime.GetHour(),
268 false);
271 ORowSetValue OOp_Now::operate(const ::std::vector<ORowSetValue>& lhs) const
273 if ( !lhs.empty() )
274 return ORowSetValue();
276 DateTime aCurTime( DateTime::SYSTEM );
277 return ::com::sun::star::util::DateTime(aCurTime.GetNanoSec(),
278 aCurTime.GetSec(), aCurTime.GetMin(), aCurTime.GetHour(),
279 aCurTime.GetDay(), aCurTime.GetMonth(), aCurTime.GetYear(),
280 false);
284 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */