This is an automated email from the ASF dual-hosted git repository. zabetak pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/calcite-avatica.git
The following commit(s) were added to refs/heads/main by this push: new 0ea5d4f40 [CALCITE-3078] Move public lastDay method from Calcite to Avatica 0ea5d4f40 is described below commit 0ea5d4f400afc15141076805afdc4a81d0375fc7 Author: Stamatis Zampetakis <zabe...@gmail.com> AuthorDate: Sun Oct 23 18:51:17 2022 +0200 [CALCITE-3078] Move public lastDay method from Calcite to Avatica Close apache/calcite-avatica#185 --- .../apache/calcite/avatica/util/DateTimeUtils.java | 13 ++++ .../apache/calcite/avatica/util/LastDayTest.java | 77 ++++++++++++++++++++++ 2 files changed, 90 insertions(+) diff --git a/core/src/main/java/org/apache/calcite/avatica/util/DateTimeUtils.java b/core/src/main/java/org/apache/calcite/avatica/util/DateTimeUtils.java index 5995d22dc..a4fdb7f0a 100644 --- a/core/src/main/java/org/apache/calcite/avatica/util/DateTimeUtils.java +++ b/core/src/main/java/org/apache/calcite/avatica/util/DateTimeUtils.java @@ -1045,6 +1045,19 @@ public class DateTimeUtils { return DateTimeUtils.ymdToUnixDate(y0, m0, d0); } + /** + * SQL {@code LAST_DAY} function. + * + * @param date days since epoch + * @return days of the last day of the month since epoch + */ + public static int lastDay(int date) { + int y0 = (int) DateTimeUtils.unixDateExtract(TimeUnitRange.YEAR, date); + int m0 = (int) DateTimeUtils.unixDateExtract(TimeUnitRange.MONTH, date); + int last = lastDay(y0, m0); + return DateTimeUtils.ymdToUnixDate(y0, m0, last); + } + private static int lastDay(int y, int m) { switch (m) { case 2: diff --git a/core/src/test/java/org/apache/calcite/avatica/util/LastDayTest.java b/core/src/test/java/org/apache/calcite/avatica/util/LastDayTest.java new file mode 100644 index 000000000..0ce6a009b --- /dev/null +++ b/core/src/test/java/org/apache/calcite/avatica/util/LastDayTest.java @@ -0,0 +1,77 @@ +/* + * 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 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.calcite.avatica.util; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; + +import java.util.Arrays; +import java.util.Collection; + +import static org.apache.calcite.avatica.util.DateTimeUtils.dateStringToUnixDate; +import static org.apache.calcite.avatica.util.DateTimeUtils.lastDay; +import static org.apache.calcite.avatica.util.DateTimeUtils.unixDateToString; + +import static org.junit.Assert.assertEquals; + +/** + * Tests for {@code lastDay} methods in {@link DateTimeUtils}. + */ +@RunWith(Parameterized.class) +public class LastDayTest { + + @Parameterized.Parameters(name = "{0}") + public static Collection<Object[]> data() { + return Arrays.asList(new Object[][]{ + {"2019-02-10", "2019-02-28"}, + {"2019-06-10", "2019-06-30"}, + {"2019-07-10", "2019-07-31"}, + {"2019-09-10", "2019-09-30"}, + {"2019-12-10", "2019-12-31"}, + {"9999-12-10", "9999-12-31"}, + {"1900-01-01", "1900-01-31"}, + {"1935-02-01", "1935-02-28"}, + {"1965-09-01", "1965-09-30"}, + {"1970-01-01", "1970-01-31"}, + {"2019-02-28", "2019-02-28"}, + {"2019-12-31", "2019-12-31"}, + {"2019-01-01", "2019-01-31"}, + {"2019-06-30", "2019-06-30"}, + {"2020-02-20", "2020-02-29"}, + {"2020-02-29", "2020-02-29"}, + {"9999-12-31", "9999-12-31"} + }); + } + + + private final String inputDate; + private final String expectedDay; + + public LastDayTest(String inputDate, String expectedDay) { + this.inputDate = inputDate; + this.expectedDay = expectedDay; + } + + @Test + public void testLastDayFromDateReturnsExpectedDay() { + int lastDayFromDate = lastDay(dateStringToUnixDate(inputDate)); + assertEquals(expectedDay, unixDateToString(lastDayFromDate)); + } + +} +// End LastDayTest.java