Skip to content

Commit

Permalink
[SPARK-8199] fixed bug in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tarekbecker committed Jul 19, 2015
1 parent bb567b6 commit f7b4c8c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class DateFunctionsSuite extends SparkFunSuite with ExpressionEvalHelper {
(0 to 5).foreach { i =>
val c = Calendar.getInstance()
c.set(y, m, 28, 0, 0, 0)
c.add(Calendar.DATE, i)
c.add(Calendar.DATE, 1)
checkEvaluation(DayInYear(Cast(Literal(new Date(c.getTimeInMillis)), DateType)),
sdfDay.format(c.getTime).toInt)
}
Expand All @@ -62,7 +62,7 @@ class DateFunctionsSuite extends SparkFunSuite with ExpressionEvalHelper {
(0 to 5).foreach { i =>
val c = Calendar.getInstance()
c.set(y, m, 28, 0, 0, 0)
c.add(Calendar.DATE, i)
c.add(Calendar.DATE, 1)
checkEvaluation(DayInYear(Cast(Literal(new Date(c.getTimeInMillis)), DateType)),
sdfDay.format(c.getTime).toInt)
}
Expand All @@ -74,7 +74,7 @@ class DateFunctionsSuite extends SparkFunSuite with ExpressionEvalHelper {
(0 to 5).foreach { i =>
val c = Calendar.getInstance()
c.set(y, m, 28, 0, 0, 0)
c.add(Calendar.DATE, i)
c.add(Calendar.DATE, 1)
checkEvaluation(DayInYear(Cast(Literal(new Date(c.getTimeInMillis)), DateType)),
sdfDay.format(c.getTime).toInt)
}
Expand All @@ -86,7 +86,7 @@ class DateFunctionsSuite extends SparkFunSuite with ExpressionEvalHelper {
(0 to 5).foreach { i =>
val c = Calendar.getInstance()
c.set(y, m, 28, 0, 0, 0)
c.add(Calendar.DATE, i)
c.add(Calendar.DATE, 1)
checkEvaluation(DayInYear(Cast(Literal(new Date(c.getTimeInMillis)), DateType)),
sdfDay.format(c.getTime).toInt)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ import org.apache.spark.unsafe.types.UTF8String

class DateTimeUtilsSuite extends SparkFunSuite {

private[this] def getInUTCDays(timestamp: Long): Int = {
val tz = TimeZone.getDefault
((timestamp + tz.getOffset(timestamp)) / DateTimeUtils.MILLIS_PER_DAY).toInt
}

test("timestamp and us") {
val now = new Timestamp(System.currentTimeMillis())
now.setNanos(1000)
Expand Down Expand Up @@ -276,28 +281,6 @@ class DateTimeUtilsSuite extends SparkFunSuite {
assert(DateTimeUtils.stringToTimestamp(
UTF8String.fromString("2011-05-06 07:08:09.1000")).get === c.getTimeInMillis * 1000)

val defaultTimeZone = TimeZone.getDefault
TimeZone.setDefault(TimeZone.getTimeZone("America/Los_Angeles"))

c = Calendar.getInstance()
c.set(2015, 2, 8, 2, 0, 0)
c.set(Calendar.MILLISECOND, 0)
assert(DateTimeUtils.stringToTimestamp(
UTF8String.fromString("2015-3-8 2:0:0")).get === c.getTimeInMillis * 1000)
c.add(Calendar.MINUTE, 30)
assert(DateTimeUtils.stringToTimestamp(
UTF8String.fromString("2015-3-8 3:30:0")).get === c.getTimeInMillis * 1000)
assert(DateTimeUtils.stringToTimestamp(
UTF8String.fromString("2015-3-8 2:30:0")).get === c.getTimeInMillis * 1000)

c = Calendar.getInstance()
c.set(2015, 10, 1, 1, 59, 0)
c.set(Calendar.MILLISECOND, 0)
c.add(Calendar.MINUTE, 31)
assert(DateTimeUtils.stringToTimestamp(
UTF8String.fromString("2015-11-1 2:30:0")).get === c.getTimeInMillis * 1000)
TimeZone.setDefault(defaultTimeZone)

assert(DateTimeUtils.stringToTimestamp(UTF8String.fromString("238")).isEmpty)
assert(DateTimeUtils.stringToTimestamp(UTF8String.fromString("2015-03-18 123142")).isEmpty)
assert(DateTimeUtils.stringToTimestamp(UTF8String.fromString("2015-03-18T123123")).isEmpty)
Expand Down Expand Up @@ -340,51 +323,41 @@ class DateTimeUtilsSuite extends SparkFunSuite {

test("get day in year") {
val c = Calendar.getInstance()
c.set(2015, 2, 18)
assert(DateTimeUtils.getDayInYear((c.getTimeInMillis / DateTimeUtils.MILLIS_PER_DAY).toInt) ===
77)
c.set(2012, 2, 18)
assert(DateTimeUtils.getDayInYear((c.getTimeInMillis / DateTimeUtils.MILLIS_PER_DAY).toInt) ===
78)
c.set(2015, 2, 18, 0, 0, 0)
assert(DateTimeUtils.getDayInYear(getInUTCDays(c.getTimeInMillis)) === 77)
c.set(2012, 2, 18, 0, 0, 0)
assert(DateTimeUtils.getDayInYear(getInUTCDays(c.getTimeInMillis)) === 78)
}

test("get year") {
val c = Calendar.getInstance()
c.set(2015, 2, 18)
assert(DateTimeUtils.getYear((c.getTimeInMillis / DateTimeUtils.MILLIS_PER_DAY).toInt) ===
2015)
c.set(2012, 2, 18)
assert(DateTimeUtils.getYear((c.getTimeInMillis / DateTimeUtils.MILLIS_PER_DAY).toInt) ===
2012)
c.set(2015, 2, 18, 0, 0, 0)
assert(DateTimeUtils.getYear(getInUTCDays(c.getTimeInMillis)) === 2015)
c.set(2012, 2, 18, 0, 0, 0)
assert(DateTimeUtils.getYear(getInUTCDays(c.getTimeInMillis)) === 2012)
}

test("get quarter") {
val c = Calendar.getInstance()
c.set(2015, 2, 18)
assert(DateTimeUtils.getQuarter((c.getTimeInMillis / DateTimeUtils.MILLIS_PER_DAY).toInt) ===
1)
c.set(2012, 11, 18)
assert(DateTimeUtils.getQuarter((c.getTimeInMillis / DateTimeUtils.MILLIS_PER_DAY).toInt) ===
4)
c.set(2015, 2, 18, 0, 0, 0)
assert(DateTimeUtils.getQuarter(getInUTCDays(c.getTimeInMillis)) === 1)
c.set(2012, 11, 18, 0, 0, 0)
assert(DateTimeUtils.getQuarter(getInUTCDays(c.getTimeInMillis)) === 4)
}

test("get month") {
val c = Calendar.getInstance()
c.set(2015, 2, 18)
assert(DateTimeUtils.getMonth((c.getTimeInMillis / DateTimeUtils.MILLIS_PER_DAY).toInt) ===
3)
c.set(2012, 11, 18)
assert(DateTimeUtils.getMonth((c.getTimeInMillis / DateTimeUtils.MILLIS_PER_DAY).toInt) ===
12)
c.set(2015, 2, 18, 0, 0, 0)
assert(DateTimeUtils.getMonth(getInUTCDays(c.getTimeInMillis)) === 3)
c.set(2012, 11, 18, 0, 0, 0)
assert(DateTimeUtils.getMonth(getInUTCDays(c.getTimeInMillis)) === 12)
}

test("get day of month") {
val c = Calendar.getInstance()
c.set(2015, 2, 18)
assert(DateTimeUtils.getDayOfMonth((c.getTimeInMillis / DateTimeUtils.MILLIS_PER_DAY).toInt) ===
18)
c.set(2012, 11, 24)
assert(DateTimeUtils.getDayOfMonth((c.getTimeInMillis / DateTimeUtils.MILLIS_PER_DAY).toInt) ===
24)
c.set(2015, 2, 18, 0, 0, 0)
assert(DateTimeUtils.getDayOfMonth(getInUTCDays(c.getTimeInMillis)) === 18)
c.set(2012, 11, 24, 0, 0, 0)
assert(DateTimeUtils.getDayOfMonth(getInUTCDays(c.getTimeInMillis)) === 24)
}
}

0 comments on commit f7b4c8c

Please sign in to comment.