Skip to content

Commit

Permalink
Fix simple date format
Browse files Browse the repository at this point in the history
  • Loading branch information
sumeet-db committed Aug 1, 2024
1 parent 379e40e commit f46b4dd
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,10 @@ trait SparkDateTimeUtils {
def toJavaTimestamp(micros: Long): Timestamp =
toJavaTimestampNoRebase(rebaseGregorianToJulianMicros(micros))

def toJavaTimestamp(timeZoneId: String, micros: Long): Timestamp =
toJavaTimestampNoRebase(rebaseGregorianToJulianMicros(timeZoneId, micros))


/**
* Converts microseconds since the epoch to an instance of `java.sql.Timestamp`.
*
Expand Down Expand Up @@ -287,6 +291,10 @@ trait SparkDateTimeUtils {
def fromJavaTimestamp(t: Timestamp): Long =
rebaseJulianToGregorianMicros(fromJavaTimestampNoRebase(t))

def fromJavaTimestamp(timeZoneId: String, t: Timestamp): Long =
rebaseJulianToGregorianMicros(timeZoneId, fromJavaTimestampNoRebase(t))


/**
* Converts an instance of `java.sql.Timestamp` to the number of microseconds since
* 1970-01-01T00:00:00.000000Z.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ class LegacyFastTimestampFormatter(
if (ts.getNanos == 0) {
fastDateFormat.format(ts)
} else {
format(fromJavaTimestamp(ts))
format(fromJavaTimestamp(zoneId.getId, ts))
}
}

Expand All @@ -473,20 +473,20 @@ class LegacySimpleTimestampFormatter(
}

override def parse(s: String): Long = {
fromJavaTimestamp(new Timestamp(sdf.parse(s).getTime))
fromJavaTimestamp(zoneId.getId, new Timestamp(sdf.parse(s).getTime))
}

override def parseOptional(s: String): Option[Long] = {
val date = sdf.parse(s, new ParsePosition(0))
if (date == null) {
None
} else {
Some(fromJavaTimestamp(new Timestamp(date.getTime)))
Some(fromJavaTimestamp(zoneId.getId, new Timestamp(date.getTime)))
}
}

override def format(us: Long): String = {
sdf.format(toJavaTimestamp(us))
sdf.format(toJavaTimestamp(zoneId.getId, us))
}

override def format(ts: Timestamp): String = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -315,23 +315,16 @@ class TimestampFormatterSuite extends DatetimeFormatterSuite {
isParsing = false)
} :+ TimestampFormatter.getFractionFormatter(zoneId)
formatters.foreach { formatter =>
assert(
microsToInstant(formatter.parse("1000-01-01 01:02:03"))
.atZone(zoneId)
.toLocalDateTime === LocalDateTime.of(1000, 1, 1, 1, 2, 3))

assert(
formatter.format(
LocalDateTime.of(1000, 1, 1, 1, 2, 3).atZone(zoneId).toInstant) ===
"1000-01-01 01:02:03")
assert(
formatter.format(
instantToMicros(LocalDateTime
.of(1000, 1, 1, 1, 2, 3)
.atZone(zoneId)
.toInstant)) === "1000-01-01 01:02:03")
assert(formatter.format(java.sql.Timestamp.valueOf("1000-01-01 01:02:03")) ===
assert(microsToInstant(formatter.parse("1000-01-01 01:02:03"))
.atZone(zoneId)
.toLocalDateTime === LocalDateTime.of(1000, 1, 1, 1, 2, 3))

assert(formatter.format(
LocalDateTime.of(1000, 1, 1, 1, 2, 3).atZone(zoneId).toInstant) ===
"1000-01-01 01:02:03")
assert(formatter.format(instantToMicros(
LocalDateTime.of(1000, 1, 1, 1, 2, 3)
.atZone(zoneId).toInstant)) === "1000-01-01 01:02:03")
}
}
}
Expand Down

0 comments on commit f46b4dd

Please sign in to comment.