Skip to content

Commit 3425c7e

Browse files
committed
Fix localtime handling for legacy timestamp semantics
Correct `localtime` behavior in legacy timestamp semantics when the session time zone’s current offset differs from its offset on 1970-01-01.
1 parent 8828a3b commit 3425c7e

File tree

4 files changed

+13
-25
lines changed

4 files changed

+13
-25
lines changed

presto-main-base/src/main/java/com/facebook/presto/operator/scalar/DateTimeFunctions.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,8 @@ public static long currentTime(SqlFunctionProperties properties)
143143
public static long localTime(SqlFunctionProperties properties)
144144
{
145145
if (properties.isLegacyTimestamp()) {
146-
return UTC_CHRONOLOGY.millisOfDay().get(properties.getSessionStartTime());
146+
long millis = UTC_CHRONOLOGY.millisOfDay().get(properties.getSessionStartTime());
147+
return millis - valueToSessionTimeZoneOffsetDiff(properties.getSessionStartTime(), getDateTimeZone(properties.getTimeZoneKey()));
147148
}
148149
ISOChronology localChronology = getChronology(properties.getTimeZoneKey());
149150
return localChronology.millisOfDay().get(properties.getSessionStartTime());

presto-main-base/src/test/java/com/facebook/presto/operator/scalar/TestDateTimeFunctions.java

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
package com.facebook.presto.operator.scalar;
1616

1717
import com.facebook.presto.Session;
18-
import com.facebook.presto.common.type.TimeType;
1918
import com.facebook.presto.common.type.TimestampType;
2019
import org.joda.time.DateTime;
2120
import org.testng.annotations.Test;
@@ -45,17 +44,6 @@ public void testFormatDateCannotImplicitlyAddTimeZoneToTimestampLiteral()
4544
"format_datetime for TIMESTAMP type, cannot use 'Z' nor 'z' in format, as this type does not contain TZ information");
4645
}
4746

48-
@Test
49-
public void testLocalTime()
50-
{
51-
Session localSession = Session.builder(session)
52-
.setStartTime(new DateTime(2017, 3, 1, 14, 30, 0, 0, DATE_TIME_ZONE).getMillis())
53-
.build();
54-
try (FunctionAssertions localAssertion = new FunctionAssertions(localSession)) {
55-
localAssertion.assertFunctionString("LOCALTIME", TimeType.TIME, "14:30:00.000");
56-
}
57-
}
58-
5947
@Test
6048
public void testLocalTimestamp()
6149
{

presto-main-base/src/test/java/com/facebook/presto/operator/scalar/TestDateTimeFunctionsBase.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,17 @@ private static long epochDaysInZone(TimeZoneKey timeZoneKey, long instant)
186186
return LocalDate.from(Instant.ofEpochMilli(instant).atZone(ZoneId.of(timeZoneKey.getId()))).toEpochDay();
187187
}
188188

189+
@Test
190+
public void testLocalTime()
191+
{
192+
Session localSession = Session.builder(session)
193+
.setStartTime(new DateTime(2017, 3, 1, 14, 30, 0, 0, DATE_TIME_ZONE).getMillis())
194+
.build();
195+
try (FunctionAssertions localAssertion = new FunctionAssertions(localSession)) {
196+
localAssertion.assertFunctionString("LOCALTIME", TimeType.TIME, "14:30:00.000");
197+
}
198+
}
199+
189200
@Test
190201
public void testCurrentTime()
191202
{

presto-main-base/src/test/java/com/facebook/presto/operator/scalar/TestDateTimeFunctionsLegacy.java

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
package com.facebook.presto.operator.scalar;
1616

1717
import com.facebook.presto.Session;
18-
import com.facebook.presto.common.type.TimeType;
1918
import com.facebook.presto.common.type.TimestampType;
2019
import org.joda.time.DateTime;
2120
import org.testng.annotations.Test;
@@ -44,17 +43,6 @@ public void testFormatDateCanImplicitlyAddTimeZoneToTimestampLiteral()
4443
assertFunction("format_datetime(" + TIMESTAMP_LITERAL + ", 'YYYY/MM/dd HH:mm ZZZZ')", VARCHAR, "2001/08/22 03:04 " + DATE_TIME_ZONE.getID());
4544
}
4645

47-
@Test
48-
public void testLocalTime()
49-
{
50-
Session localSession = Session.builder(session)
51-
.setStartTime(new DateTime(2017, 3, 1, 14, 30, 0, 0, DATE_TIME_ZONE).getMillis())
52-
.build();
53-
try (FunctionAssertions localAssertion = new FunctionAssertions(localSession)) {
54-
localAssertion.assertFunctionString("LOCALTIME", TimeType.TIME, "13:30:00.000");
55-
}
56-
}
57-
5846
@Test
5947
public void testLocalTimestamp()
6048
{

0 commit comments

Comments
 (0)