Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,10 @@ object DateTimeUtils {
i += 3
} else if (i < 2) {
if (b == '-') {
if (i == 0 && j != 4) {
// year should have exact four digits
return None
}
segments(i) = currentSegmentValue
currentSegmentValue = 0
i += 1
Expand Down Expand Up @@ -308,13 +312,17 @@ object DateTimeUtils {
}

segments(i) = currentSegmentValue
if (!justTime && i == 0 && j != 4) {
// year should have exact four digits
return None
}

while (digitsMilli < 6) {
segments(6) *= 10
digitsMilli += 1
}

if (!justTime && (segments(0) < 1000 || segments(0) > 9999 || segments(1) < 1 ||
if (!justTime && (segments(0) < 0 || segments(0) > 9999 || segments(1) < 1 ||
segments(1) > 12 || segments(2) < 1 || segments(2) > 31)) {
return None
}
Expand Down Expand Up @@ -368,6 +376,10 @@ object DateTimeUtils {
while (j < bytes.length && (i < 3 && !(bytes(j) == ' ' || bytes(j) == 'T'))) {
val b = bytes(j)
if (i < 2 && b == '-') {
if (i == 0 && j != 4) {
// year should have exact four digits
return None
}
segments(i) = currentSegmentValue
currentSegmentValue = 0
i += 1
Expand All @@ -381,8 +393,12 @@ object DateTimeUtils {
}
j += 1
}
if (i == 0 && j != 4) {
// year should have exact four digits
return None
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if s is not empty, we will never get into this branch right?

To handle empty s, I think a better idea is changing the default value of segments to [-1, -1, -1]

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, if s is 123, then i = 0, j = 3

}
segments(i) = currentSegmentValue
if (segments(0) < 1000 || segments(0) > 9999 || segments(1) < 1 || segments(1) > 12 ||
if (segments(0) < 0 || segments(0) > 9999 || segments(1) < 1 || segments(1) > 12 ||
segments(2) < 1 || segments(2) > 31) {
return None
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ class DateTimeUtilsSuite extends SparkFunSuite {
c.set(Calendar.MILLISECOND, 0)
assert(stringToDate(UTF8String.fromString("2015")).get ===
millisToDays(c.getTimeInMillis))
c.set(1, 0, 1, 0, 0, 0)
c.set(Calendar.MILLISECOND, 0)
assert(stringToDate(UTF8String.fromString("0001")).get ===
millisToDays(c.getTimeInMillis))
c = Calendar.getInstance()
c.set(2015, 2, 1, 0, 0, 0)
c.set(Calendar.MILLISECOND, 0)
Expand All @@ -134,11 +138,15 @@ class DateTimeUtilsSuite extends SparkFunSuite {
assert(stringToDate(UTF8String.fromString("2015.03.18")).isEmpty)
assert(stringToDate(UTF8String.fromString("20150318")).isEmpty)
assert(stringToDate(UTF8String.fromString("2015-031-8")).isEmpty)
assert(stringToDate(UTF8String.fromString("02015-03-18")).isEmpty)
assert(stringToDate(UTF8String.fromString("015-03-18")).isEmpty)
assert(stringToDate(UTF8String.fromString("015")).isEmpty)
assert(stringToDate(UTF8String.fromString("02015")).isEmpty)
}

test("string to time") {
// Tests with UTC.
var c = Calendar.getInstance(TimeZone.getTimeZone("UTC"))
val c = Calendar.getInstance(TimeZone.getTimeZone("UTC"))
c.set(Calendar.MILLISECOND, 0)

c.set(1900, 0, 1, 0, 0, 0)
Expand Down Expand Up @@ -174,9 +182,9 @@ class DateTimeUtilsSuite extends SparkFunSuite {
c.set(Calendar.MILLISECOND, 0)
assert(stringToTimestamp(UTF8String.fromString("1969-12-31 16:00:00")).get ===
c.getTimeInMillis * 1000)
c.set(2015, 0, 1, 0, 0, 0)
c.set(1, 0, 1, 0, 0, 0)
c.set(Calendar.MILLISECOND, 0)
assert(stringToTimestamp(UTF8String.fromString("2015")).get ===
assert(stringToTimestamp(UTF8String.fromString("0001")).get ===
c.getTimeInMillis * 1000)
c = Calendar.getInstance()
c.set(2015, 2, 1, 0, 0, 0)
Expand Down Expand Up @@ -319,13 +327,16 @@ class DateTimeUtilsSuite extends SparkFunSuite {
UTF8String.fromString("2011-05-06 07:08:09.1000")).get === c.getTimeInMillis * 1000)

assert(stringToTimestamp(UTF8String.fromString("238")).isEmpty)
assert(stringToTimestamp(UTF8String.fromString("00238")).isEmpty)
assert(stringToTimestamp(UTF8String.fromString("2015-03-18 123142")).isEmpty)
assert(stringToTimestamp(UTF8String.fromString("2015-03-18T123123")).isEmpty)
assert(stringToTimestamp(UTF8String.fromString("2015-03-18X")).isEmpty)
assert(stringToTimestamp(UTF8String.fromString("2015/03/18")).isEmpty)
assert(stringToTimestamp(UTF8String.fromString("2015.03.18")).isEmpty)
assert(stringToTimestamp(UTF8String.fromString("20150318")).isEmpty)
assert(stringToTimestamp(UTF8String.fromString("2015-031-8")).isEmpty)
assert(stringToTimestamp(UTF8String.fromString("02015-01-18")).isEmpty)
assert(stringToTimestamp(UTF8String.fromString("015-01-18")).isEmpty)
assert(stringToTimestamp(
UTF8String.fromString("2015-03-18T12:03.17-20:0")).isEmpty)
assert(stringToTimestamp(
Expand Down