@@ -25,7 +25,7 @@ import org.scalatest.Matchers
2525import org .apache .spark .{SparkFunSuite , SparkUpgradeException }
2626import org .apache .spark .sql .catalyst .plans .SQLHelper
2727import org .apache .spark .sql .catalyst .util .{DateTimeTestUtils , DateTimeUtils , LegacyDateFormats , TimestampFormatter }
28- import org .apache .spark .sql .catalyst .util .DateTimeTestUtils .{ CET , PST , UTC }
28+ import org .apache .spark .sql .catalyst .util .DateTimeTestUtils ._
2929import org .apache .spark .sql .catalyst .util .DateTimeUtils ._
3030import org .apache .spark .sql .internal .SQLConf
3131import org .apache .spark .sql .internal .SQLConf .LegacyBehaviorPolicy
@@ -120,8 +120,7 @@ class TimestampFormatterSuite extends SparkFunSuite with SQLHelper with Matchers
120120 test(" case insensitive parsing of am and pm" ) {
121121 val formatter = TimestampFormatter (" yyyy MMM dd hh:mm:ss a" , ZoneOffset .UTC )
122122 val micros = formatter.parse(" 2009 Mar 20 11:30:01 am" )
123- assert(micros === TimeUnit .SECONDS .toMicros(
124- LocalDateTime .of(2009 , 3 , 20 , 11 , 30 , 1 ).toEpochSecond(ZoneOffset .UTC )))
123+ assert(micros === date(2009 , 3 , 20 , 11 , 30 , 1 ))
125124 }
126125
127126 test(" format fraction of second" ) {
@@ -241,8 +240,7 @@ class TimestampFormatterSuite extends SparkFunSuite with SQLHelper with Matchers
241240
242241 test(" SPARK-30958: parse timestamp with negative year" ) {
243242 val formatter1 = TimestampFormatter (" yyyy-MM-dd HH:mm:ss" , ZoneOffset .UTC , true )
244- assert(formatter1.parse(" -1234-02-22 02:22:22" ) === instantToMicros(
245- LocalDateTime .of(- 1234 , 2 , 22 , 2 , 22 , 22 ).toInstant(ZoneOffset .UTC )))
243+ assert(formatter1.parse(" -1234-02-22 02:22:22" ) === date(- 1234 , 2 , 22 , 2 , 22 , 22 ))
246244
247245 def assertParsingError (f : => Unit ): Unit = {
248246 intercept[Exception ](f) match {
@@ -258,10 +256,8 @@ class TimestampFormatterSuite extends SparkFunSuite with SQLHelper with Matchers
258256 assertParsingError(formatter2.parse(" BC -1234-02-22 02:22:22" ))
259257 assertParsingError(formatter2.parse(" AC 0000-02-22 02:22:22" ))
260258
261- assert(formatter2.parse(" BC 1234-02-22 02:22:22" ) === instantToMicros(
262- LocalDateTime .of(- 1233 , 2 , 22 , 2 , 22 , 22 ).toInstant(ZoneOffset .UTC )))
263- assert(formatter2.parse(" AD 1234-02-22 02:22:22" ) === instantToMicros(
264- LocalDateTime .of(1234 , 2 , 22 , 2 , 22 , 22 ).toInstant(ZoneOffset .UTC )))
259+ assert(formatter2.parse(" BC 1234-02-22 02:22:22" ) === date(- 1233 , 2 , 22 , 2 , 22 , 22 ))
260+ assert(formatter2.parse(" AD 1234-02-22 02:22:22" ) === date(1234 , 2 , 22 , 2 , 22 , 22 ))
265261 }
266262
267263 test(" SPARK-31557: rebasing in legacy formatters/parsers" ) {
@@ -281,9 +277,8 @@ class TimestampFormatterSuite extends SparkFunSuite with SQLHelper with Matchers
281277 .atZone(zoneId)
282278 .toLocalDateTime === LocalDateTime .of(1000 , 1 , 1 , 1 , 2 , 3 ))
283279
284- assert(formatter.format(instantToMicros(
285- LocalDateTime .of(1000 , 1 , 1 , 1 , 2 , 3 )
286- .atZone(zoneId).toInstant)) === " 1000-01-01 01:02:03" )
280+ assert(formatter.format(date(1000 , 1 , 1 , 1 , 2 , 3 , zid = zoneId)) ===
281+ " 1000-01-01 01:02:03" )
287282 }
288283 }
289284 }
@@ -295,19 +290,17 @@ class TimestampFormatterSuite extends SparkFunSuite with SQLHelper with Matchers
295290 test(" parsing hour with various patterns" ) {
296291 def createFormatter (pattern : String ): TimestampFormatter = {
297292 // Use `SIMPLE_DATE_FORMAT`, so that the legacy parser also fails with invalid value range.
298- TimestampFormatter (pattern, ZoneOffset . UTC , LegacyDateFormats .SIMPLE_DATE_FORMAT , false )
293+ TimestampFormatter (pattern, UTC , LegacyDateFormats .SIMPLE_DATE_FORMAT , false )
299294 }
300295
301296 withClue(" HH" ) {
302297 val formatter = createFormatter(" yyyy-MM-dd HH" )
303298
304299 val micros1 = formatter.parse(" 2009-12-12 00" )
305- assert(micros1 === TimeUnit .SECONDS .toMicros(
306- LocalDateTime .of(2009 , 12 , 12 , 0 , 0 , 0 ).toEpochSecond(ZoneOffset .UTC )))
300+ assert(micros1 === date(2009 , 12 , 12 ))
307301
308302 val micros2 = formatter.parse(" 2009-12-12 15" )
309- assert(micros2 === TimeUnit .SECONDS .toMicros(
310- LocalDateTime .of(2009 , 12 , 12 , 15 , 0 , 0 ).toEpochSecond(ZoneOffset .UTC )))
303+ assert(micros2 === date(2009 , 12 , 12 , 15 ))
311304
312305 intercept[DateTimeException ](formatter.parse(" 2009-12-12 24" ))
313306 }
@@ -318,29 +311,24 @@ class TimestampFormatterSuite extends SparkFunSuite with SQLHelper with Matchers
318311 intercept[DateTimeException ](formatter.parse(" 2009-12-12 00" ))
319312
320313 val micros1 = formatter.parse(" 2009-12-12 15" )
321- assert(micros1 === TimeUnit .SECONDS .toMicros(
322- LocalDateTime .of(2009 , 12 , 12 , 15 , 0 , 0 ).toEpochSecond(ZoneOffset .UTC )))
314+ assert(micros1 === date(2009 , 12 , 12 , 15 ))
323315
324316 val micros2 = formatter.parse(" 2009-12-12 24" )
325- assert(micros2 === TimeUnit .SECONDS .toMicros(
326- LocalDateTime .of(2009 , 12 , 12 , 0 , 0 , 0 ).toEpochSecond(ZoneOffset .UTC )))
317+ assert(micros2 === date(2009 , 12 , 12 ))
327318 }
328319
329320 withClue(" KK" ) {
330321 val formatter = createFormatter(" yyyy-MM-dd KK a" )
331322
332323 val micros1 = formatter.parse(" 2009-12-12 00 am" )
333- assert(micros1 === TimeUnit .SECONDS .toMicros(
334- LocalDateTime .of(2009 , 12 , 12 , 0 , 0 , 0 ).toEpochSecond(ZoneOffset .UTC )))
324+ assert(micros1 === date(2009 , 12 , 12 ))
335325
336326 // For `KK`, "12:00:00 am" is the same as "00:00:00 pm".
337327 val micros2 = formatter.parse(" 2009-12-12 12 am" )
338- assert(micros2 === TimeUnit .SECONDS .toMicros(
339- LocalDateTime .of(2009 , 12 , 12 , 12 , 0 , 0 ).toEpochSecond(ZoneOffset .UTC )))
328+ assert(micros2 === date(2009 , 12 , 12 , 12 ))
340329
341330 val micros3 = formatter.parse(" 2009-12-12 00 pm" )
342- assert(micros3 === TimeUnit .SECONDS .toMicros(
343- LocalDateTime .of(2009 , 12 , 12 , 12 , 0 , 0 ).toEpochSecond(ZoneOffset .UTC )))
331+ assert(micros3 === date(2009 , 12 , 12 , 12 ))
344332
345333 intercept[DateTimeException ](formatter.parse(" 2009-12-12 12 pm" ))
346334 }
@@ -351,35 +339,34 @@ class TimestampFormatterSuite extends SparkFunSuite with SQLHelper with Matchers
351339 intercept[DateTimeException ](formatter.parse(" 2009-12-12 00 am" ))
352340
353341 val micros1 = formatter.parse(" 2009-12-12 12 am" )
354- assert(micros1 === TimeUnit .SECONDS .toMicros(
355- LocalDateTime .of(2009 , 12 , 12 , 0 , 0 , 0 ).toEpochSecond(ZoneOffset .UTC )))
342+ assert(micros1 === date(2009 , 12 , 12 ))
356343
357344 intercept[DateTimeException ](formatter.parse(" 2009-12-12 00 pm" ))
358345
359346 val micros2 = formatter.parse(" 2009-12-12 12 pm" )
360- assert(micros2 === TimeUnit .SECONDS .toMicros(
361- LocalDateTime .of(2009 , 12 , 12 , 12 , 0 , 0 ).toEpochSecond(ZoneOffset .UTC )))
347+ assert(micros2 === date(2009 , 12 , 12 , 12 ))
362348 }
363349 }
364350
365351 test(" missing date fields" ) {
366- val formatter = TimestampFormatter (" HH:mm:ss" , ZoneOffset .UTC )
367- val micros = formatter.parse(" 11:30:01" )
368- assert(micros === TimeUnit .SECONDS .toMicros(
369- LocalDateTime .of(1970 , 1 , 1 , 11 , 30 , 1 ).toEpochSecond(ZoneOffset .UTC )))
352+ // by default we don't allow missing year field.
353+ intercept[SparkUpgradeException ](TimestampFormatter (" HH:mm:ss" , UTC ).parse(" 11:30:01" ))
354+ withSQLConf(SQLConf .LEGACY_ALLOW_MISSING_YEAR_DURING_PARSING .key -> " true" ) {
355+ val formatter = TimestampFormatter (" HH:mm:ss" , UTC )
356+ val micros = formatter.parse(" 11:30:01" )
357+ assert(micros === date(1970 , 1 , 1 , 11 , 30 , 1 ))
358+ }
370359 }
371360
372361 test(" missing am/pm field" ) {
373- val formatter = TimestampFormatter (" yyyy hh:mm:ss" , ZoneOffset . UTC )
362+ val formatter = TimestampFormatter (" yyyy hh:mm:ss" , UTC )
374363 val micros = formatter.parse(" 2009 11:30:01" )
375- assert(micros === TimeUnit .SECONDS .toMicros(
376- LocalDateTime .of(2009 , 1 , 1 , 11 , 30 , 1 ).toEpochSecond(ZoneOffset .UTC )))
364+ assert(micros === date(2009 , 1 , 1 , 11 , 30 , 1 ))
377365 }
378366
379367 test(" missing time fields" ) {
380- val formatter = TimestampFormatter (" yyyy HH" , ZoneOffset . UTC )
368+ val formatter = TimestampFormatter (" yyyy HH" , UTC )
381369 val micros = formatter.parse(" 2009 11" )
382- assert(micros === TimeUnit .SECONDS .toMicros(
383- LocalDateTime .of(2009 , 1 , 1 , 11 , 0 , 0 ).toEpochSecond(ZoneOffset .UTC )))
370+ assert(micros === date(2009 , 1 , 1 , 11 ))
384371 }
385372}
0 commit comments