Skip to content

Commit

Permalink
Fix last_day_of_month for timestamp with time zone
Browse files Browse the repository at this point in the history
  • Loading branch information
oneonestar authored and findepi committed Feb 21, 2020
1 parent 4a52b0b commit 13e53d7
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -981,8 +981,12 @@ public static long dayFromInterval(@SqlType(StandardTypes.INTERVAL_DAY_TO_SECOND
@SqlType(StandardTypes.DATE)
public static long lastDayOfMonthFromTimestampWithTimeZone(@SqlType(StandardTypes.TIMESTAMP_WITH_TIME_ZONE) long timestampWithTimeZone)
{
ISOChronology isoChronology = unpackChronology(timestampWithTimeZone);
long millis = unpackMillisUtc(timestampWithTimeZone);
millis = unpackChronology(timestampWithTimeZone).monthOfYear().roundCeiling(millis + 1);
// Calculate point in time corresponding to midnight (00:00) of first day of next month in the given zone.
millis = isoChronology.monthOfYear().roundCeiling(millis + 1);
// Convert to UTC and take the previous day
millis = isoChronology.getZone().convertUTCToLocal(millis) - MILLISECONDS_IN_DAY;
return MILLISECONDS.toDays(millis);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
*/
package io.prestosql.operator.scalar;

import com.google.common.collect.ImmutableList;
import io.prestosql.Session;
import io.prestosql.spi.StandardErrorCode;
import io.prestosql.spi.type.BigintType;
Expand Down Expand Up @@ -355,10 +356,19 @@ public void testLastDayOfMonth()
assertFunction("last_day_of_month(TIMESTAMP '2019-08-31 23:59:59.999')", DateType.DATE, toDate(LocalDate.of(2019, 8, 31)));

assertFunction("last_day_of_month(" + WEIRD_TIMESTAMP_LITERAL + ")", DateType.DATE, toDate(DATE.withDayOfMonth(31)));
assertFunction("last_day_of_month(TIMESTAMP '2019-08-01 00:00:00.000 +05:45')", DateType.DATE, toDate(LocalDate.of(2019, 8, 31)));
assertFunction("last_day_of_month(TIMESTAMP '2019-08-01 17:00:00.000 +05:45')", DateType.DATE, toDate(LocalDate.of(2019, 8, 31)));
assertFunction("last_day_of_month(TIMESTAMP '2019-08-01 23:59:59.999 +05:45')", DateType.DATE, toDate(LocalDate.of(2019, 8, 31)));
assertFunction("last_day_of_month(TIMESTAMP '2019-08-31 23:59:59.999 +05:45')", DateType.DATE, toDate(LocalDate.of(2019, 8, 31)));
ImmutableList.of("+05:45", "+00:00", "-05:45", "Asia/Tokyo", "Europe/London", "America/Los_Angeles", "America/Bahia_Banderas").forEach(timeZone -> {
assertFunction("last_day_of_month(TIMESTAMP '2018-12-31 17:00:00.000 " + timeZone + "')", DateType.DATE, toDate(LocalDate.of(2018, 12, 31)));
assertFunction("last_day_of_month(TIMESTAMP '2018-12-31 20:00:00.000 " + timeZone + "')", DateType.DATE, toDate(LocalDate.of(2018, 12, 31)));
assertFunction("last_day_of_month(TIMESTAMP '2018-12-31 23:59:59.999 " + timeZone + "')", DateType.DATE, toDate(LocalDate.of(2018, 12, 31)));
assertFunction("last_day_of_month(TIMESTAMP '2019-01-01 00:00:00.000 " + timeZone + "')", DateType.DATE, toDate(LocalDate.of(2019, 1, 31)));
assertFunction("last_day_of_month(TIMESTAMP '2019-01-01 00:00:00.001 " + timeZone + "')", DateType.DATE, toDate(LocalDate.of(2019, 1, 31)));
assertFunction("last_day_of_month(TIMESTAMP '2019-01-01 03:00:00.000 " + timeZone + "')", DateType.DATE, toDate(LocalDate.of(2019, 1, 31)));
assertFunction("last_day_of_month(TIMESTAMP '2019-01-01 06:00:00.000 " + timeZone + "')", DateType.DATE, toDate(LocalDate.of(2019, 1, 31)));
assertFunction("last_day_of_month(TIMESTAMP '2019-08-01 00:00:00.000 " + timeZone + "')", DateType.DATE, toDate(LocalDate.of(2019, 8, 31)));
assertFunction("last_day_of_month(TIMESTAMP '2019-08-01 17:00:00.000 " + timeZone + "')", DateType.DATE, toDate(LocalDate.of(2019, 8, 31)));
assertFunction("last_day_of_month(TIMESTAMP '2019-08-01 23:59:59.999 " + timeZone + "')", DateType.DATE, toDate(LocalDate.of(2019, 8, 31)));
assertFunction("last_day_of_month(TIMESTAMP '2019-08-31 23:59:59.999 " + timeZone + "')", DateType.DATE, toDate(LocalDate.of(2019, 8, 31)));
});
}

@Test
Expand Down

0 comments on commit 13e53d7

Please sign in to comment.