-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-15613] [SQL] Fix incorrect days to millis conversion due to Daylight Saving Time #13652
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -30,7 +30,7 @@ import org.apache.spark.sql.catalyst.ScalaReflectionLock | |
| * | ||
| * Please use the singleton [[DataTypes.DateType]]. | ||
| * | ||
| * Internally, this is represented as the number of days from epoch (1970-01-01 00:00:00 UTC). | ||
| * Internally, this is represented as the number of days from 1970-01-01. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can this comment more explicitly say that this is time-zone sensitive / with respect to the local time zone for some definition of "local"?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The DateType here has nothing with timezone, timezone is considered only when DateType is converted to/from TimestampType, right? |
||
| */ | ||
| @DeveloperApi | ||
| class DateType private() extends AtomicType { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| /* | ||
| * 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.spark.sql.catalyst.util | ||
|
|
||
| import java.util.TimeZone | ||
|
|
||
| /** | ||
| * Helper functions for testing date and time functionality. | ||
| */ | ||
| object DateTimeTestUtils { | ||
|
|
||
| val ALL_TIMEZONES: Seq[TimeZone] = TimeZone.getAvailableIDs.toSeq.map(TimeZone.getTimeZone) | ||
|
|
||
| def withDefaultTimeZone[T](newDefaultTimeZone: TimeZone)(block: => T): T = { | ||
| val originalDefaultTimeZone = TimeZone.getDefault | ||
| try { | ||
| DateTimeUtils.resetThreadLocals() | ||
| TimeZone.setDefault(newDefaultTimeZone) | ||
| block | ||
| } finally { | ||
| TimeZone.setDefault(originalDefaultTimeZone) | ||
| DateTimeUtils.resetThreadLocals() | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -492,6 +492,13 @@ class DateTimeUtilsSuite extends SparkFunSuite { | |
| test("2011-12-25 09:00:00.123456", "JST", "2011-12-25 18:00:00.123456") | ||
| test("2011-12-25 09:00:00.123456", "PST", "2011-12-25 01:00:00.123456") | ||
| test("2011-12-25 09:00:00.123456", "Asia/Shanghai", "2011-12-25 17:00:00.123456") | ||
|
|
||
| // Daylight Saving Time | ||
| test("2016-03-13 09:59:59.0", "PST", "2016-03-13 01:59:59.0") | ||
| test("2016-03-13 10:00:00.0", "PST", "2016-03-13 03:00:00.0") | ||
| test("2016-11-06 08:59:59.0", "PST", "2016-11-06 01:59:59.0") | ||
| test("2016-11-06 09:00:00.0", "PST", "2016-11-06 01:00:00.0") | ||
| test("2016-11-06 10:00:00.0", "PST", "2016-11-06 02:00:00.0") | ||
| } | ||
|
|
||
| test("to UTC timestamp") { | ||
|
|
@@ -503,5 +510,26 @@ class DateTimeUtilsSuite extends SparkFunSuite { | |
| test("2011-12-25 18:00:00.123456", "JST", "2011-12-25 09:00:00.123456") | ||
| test("2011-12-25 01:00:00.123456", "PST", "2011-12-25 09:00:00.123456") | ||
| test("2011-12-25 17:00:00.123456", "Asia/Shanghai", "2011-12-25 09:00:00.123456") | ||
|
|
||
| // Daylight Saving Time | ||
| test("2016-03-13 01:59:59", "PST", "2016-03-13 09:59:59.0") | ||
| // 2016-03-13 02:00:00 PST does not exists | ||
| test("2016-03-13 02:00:00", "PST", "2016-03-13 10:00:00.0") | ||
| test("2016-03-13 03:00:00", "PST", "2016-03-13 10:00:00.0") | ||
| test("2016-11-06 00:59:59", "PST", "2016-11-06 07:59:59.0") | ||
| // 2016-11-06 01:00:00 PST could be 2016-11-06 08:00:00 UTC or 2016-11-06 09:00:00 UTC | ||
| test("2016-11-06 01:00:00", "PST", "2016-11-06 09:00:00.0") | ||
| test("2016-11-06 01:59:59", "PST", "2016-11-06 09:59:59.0") | ||
| test("2016-11-06 02:00:00", "PST", "2016-11-06 10:00:00.0") | ||
| } | ||
|
|
||
| test("daysToMillis and millisToDays") { | ||
| for (tz <- DateTimeTestUtils.ALL_TIMEZONES) { | ||
| DateTimeTestUtils.withDefaultTimeZone(tz) { | ||
| (-2000 to 2000).foreach { d => | ||
| assert(millisToDays(daysToMillis(d)) === d) | ||
| } | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the range ...which will show more problem(s): ... list of the incorrectly converted days, with dates in time zones: |
||
| } | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
since 1970-01-01 00:00:00 UTC or local timezone?