summaryrefslogtreecommitdiffstats
path: root/connectivity/source/drivers/file/FDateFunctions.cxx
blob: 1be992d4b6724b917995ccece7a135f4408cb14e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

#include <file/FDateFunctions.hxx>
#include <tools/date.hxx>
#include <tools/time.hxx>
#include <tools/datetime.hxx>
#include <osl/diagnose.h>

using namespace connectivity;
using namespace connectivity::file;

ORowSetValue OOp_DayOfWeek::operate(const ORowSetValue& lhs) const
{
    if (lhs.isNull())
        return lhs;

    sal_Int32 nRet = 0;
    css::util::Date aD = lhs.getDate();
    Date aDate(aD.Day, aD.Month, aD.Year);
    DayOfWeek eDayOfWeek = aDate.GetDayOfWeek();
    switch (eDayOfWeek)
    {
        case MONDAY:
            nRet = 2;
            break;
        case TUESDAY:
            nRet = 3;
            break;
        case WEDNESDAY:
            nRet = 4;
            break;
        case THURSDAY:
            nRet = 5;
            break;
        case FRIDAY:
            nRet = 6;
            break;
        case SATURDAY:
            nRet = 7;
            break;
        case SUNDAY:
            nRet = 1;
            break;
        default:
            OSL_FAIL("Error in enum values for date");
    }
    return nRet;
}

ORowSetValue OOp_DayOfMonth::operate(const ORowSetValue& lhs) const
{
    if (lhs.isNull())
        return lhs;

    css::util::Date aD = lhs.getDate();
    return static_cast<sal_Int16>(aD.Day);
}

ORowSetValue OOp_DayOfYear::operate(const ORowSetValue& lhs) const
{
    if (lhs.isNull())
        return lhs;

    css::util::Date aD = lhs.getDate();
    Date aDate(aD.Day, aD.Month, aD.Year);
    return static_cast<sal_Int16>(aDate.GetDayOfYear());
}

ORowSetValue OOp_Month::operate(const ORowSetValue& lhs) const
{
    if (lhs.isNull())
        return lhs;

    css::util::Date aD = lhs.getDate();
    return static_cast<sal_Int16>(aD.Month);
}

ORowSetValue OOp_DayName::operate(const ORowSetValue& lhs) const
{
    if (lhs.isNull())
        return lhs;

    OUString sRet;
    css::util::Date aD = lhs.getDate();
    Date aDate(aD.Day, aD.Month, aD.Year);
    DayOfWeek eDayOfWeek = aDate.GetDayOfWeek();
    switch (eDayOfWeek)
    {
        case MONDAY:
            sRet = "Monday";
            break;
        case TUESDAY:
            sRet = "Tuesday";
            break;
        case WEDNESDAY:
            sRet = "Wednesday";
            break;
        case THURSDAY:
            sRet = "Thursday";
            break;
        case FRIDAY:
            sRet = "Friday";
            break;
        case SATURDAY:
            sRet = "Saturday";
            break;
        case SUNDAY:
            sRet = "Sunday";
            break;
        default:
            OSL_FAIL("Error in enum values for date");
    }
    return sRet;
}

ORowSetValue OOp_MonthName::operate(const ORowSetValue& lhs) const
{
    if (lhs.isNull())
        return lhs;

    OUString sRet;
    css::util::Date aD = lhs.getDate();
    switch (aD.Month)
    {
        case 1:
            sRet = "January";
            break;
        case 2:
            sRet = "February";
            break;
        case 3:
            sRet = "March";
            break;
        case 4:
            sRet = "April";
            break;
        case 5:
            sRet = "May";
            break;
        case 6:
            sRet = "June";
            break;
        case 7:
            sRet = "July";
            break;
        case 8:
            sRet = "August";
            break;
        case 9:
            sRet = "September";
            break;
        case 10:
            sRet = "October";
            break;
        case 11:
            sRet = "November";
            break;
        case 12:
            sRet = "December";
            break;
    }
    return sRet;
}

ORowSetValue OOp_Quarter::operate(const ORowSetValue& lhs) const
{
    if (lhs.isNull())
        return lhs;

    sal_Int32 nRet = 1;
    css::util::Date aD = lhs.getDate();
    if (aD.Month >= 4 && aD.Month < 7)
        nRet = 2;
    else if (aD.Month >= 7 && aD.Month < 10)
        nRet = 3;
    else if (aD.Month >= 10 && aD.Month <= 12)
        nRet = 4;
    return nRet;
}

ORowSetValue OOp_Week::operate(const std::vector<ORowSetValue>& lhs) const
{
    if (lhs.empty() || lhs.size() > 2)
        return ORowSetValue();

    size_t nSize = lhs.size();

    css::util::Date aD = lhs[nSize - 1].getDate();
    Date aDate(aD.Day, aD.Month, aD.Year);

    sal_Int16 nStartDay = SUNDAY;
    if (nSize == 2 && !lhs[0].isNull())
        nStartDay = lhs[0].getInt16();

    return static_cast<sal_Int16>(aDate.GetWeekOfYear(static_cast<DayOfWeek>(nStartDay)));
}

ORowSetValue OOp_Year::operate(const ORowSetValue& lhs) const
{
    if (lhs.isNull())
        return lhs;

    css::util::Date aD = lhs.getDate();
    return aD.Year;
}

ORowSetValue OOp_Hour::operate(const ORowSetValue& lhs) const
{
    if (lhs.isNull())
        return lhs;

    css::util::Time aT = lhs.getTime();
    return static_cast<sal_Int16>(aT.Hours);
}

ORowSetValue OOp_Minute::operate(const ORowSetValue& lhs) const
{
    if (lhs.isNull())
        return lhs;

    css::util::Time aT = lhs.getTime();
    return static_cast<sal_Int16>(aT.Minutes);
}

ORowSetValue OOp_Second::operate(const ORowSetValue& lhs) const
{
    if (lhs.isNull())
        return lhs;

    css::util::Time aT = lhs.getTime();
    return static_cast<sal_Int16>(aT.Seconds);
}

ORowSetValue OOp_CurDate::operate(const std::vector<ORowSetValue>& lhs) const
{
    if (!lhs.empty())
        return ORowSetValue();

    Date aCurDate(Date::SYSTEM);
    return aCurDate.GetUNODate();
}

ORowSetValue OOp_CurTime::operate(const std::vector<ORowSetValue>& lhs) const
{
    if (!lhs.empty())
        return ORowSetValue();

    tools::Time aCurTime(tools::Time::SYSTEM);
    return aCurTime.GetUNOTime();
}

ORowSetValue OOp_Now::operate(const std::vector<ORowSetValue>& lhs) const
{
    if (!lhs.empty())
        return ORowSetValue();

    DateTime aCurTime(DateTime::SYSTEM);
    return aCurTime.GetUNODateTime();
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */