Skip to content
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

Add convenience truncate method to Timestamp type #1607

Merged
Merged
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
26 changes: 26 additions & 0 deletions modules/bootstrapped/test/src-js/smithy4s/TimestampSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -216,4 +216,30 @@ class TimestampSpec() extends munit.FunSuite with munit.ScalaCheckSuite {
expect.same(tsFromEpochMilli, ts)
}
}

property("Truncate to milliseconds precision") {
forAll { (d: Date) =>
val epochMilli = d.valueOf().toLong
val ts = Timestamp.fromDate(d).truncateToMillis

val strippedDate = new Date(0)
strippedDate.setUTCMilliseconds(epochMilli)
val tsFromStrippedDate = Timestamp.fromDate(strippedDate)

expect.same(ts, tsFromStrippedDate)
}
}

property("Truncate to seconds precision") {
forAll { (d: Date) =>
val epochSecond = (d.valueOf() / 1000).toLong
val ts = Timestamp.fromDate(d).truncateToMillis

val strippedDate = new Date(0)
strippedDate.setUTCSeconds(epochSecond)
val tsFromStrippedDate = Timestamp.fromDate(strippedDate)

expect.same(ts, tsFromStrippedDate)
}
}
}
19 changes: 19 additions & 0 deletions modules/bootstrapped/test/src-jvm/smithy4s/TimestampSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -224,4 +224,23 @@ class TimestampSpec() extends munit.FunSuite with munit.ScalaCheckSuite {
expect.same(tsFromEpochMilli, tsFromStrippedInstant)
}
}

property("Truncate to milliseconds precision") {
forAll { (i: Instant) =>
val ts = Timestamp.fromInstant(i).truncateToMillis
val strippedInstant = Instant.ofEpochMilli(i.toEpochMilli)
val tsFromStrippedInstant = Timestamp.fromInstant(strippedInstant)

expect.same(ts, tsFromStrippedInstant)
}
}

property("Truncate to seconds precision") {
forAll { (i: Instant) =>
val ts = Timestamp.fromInstant(i).truncateToSeconds
val tsFromStrippedInstant = Timestamp.fromEpochSecond(i.getEpochSecond)

expect.same(ts, tsFromStrippedInstant)
}
}
}
10 changes: 10 additions & 0 deletions modules/core/src-js/smithy4s/Timestamp.scala
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,16 @@ case class Timestamp private (epochSecond: Long, nano: Int) {
date
}

/**
* @return a copy of this timestamp truncated to a miliseconds precision
*/
def truncateToMillis: Timestamp = copy(nano = (nano / 1000000) * 1000000)

/**
* @return a copy of this timestamp truncated to a seconds resolution
*/
def truncateToSeconds: Timestamp = copy(nano = 0)

override def toString: String = format(TimestampFormat.DATE_TIME)

private[this] def formatToString(internalFormat: Int): String = {
Expand Down
10 changes: 10 additions & 0 deletions modules/core/src-jvm-native/smithy4s/Timestamp.scala
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ case class Timestamp private (epochSecond: Long, nano: Int)

def conciseDate: String = formatToString(2)

/**
* @return a copy of this timestamp truncated to a miliseconds precision
*/
def truncateToMillis: Timestamp = copy(nano = (nano / 1000000) * 1000000)

/**
* @return a copy of this timestamp truncated to a seconds resolution
*/
def truncateToSeconds: Timestamp = copy(nano = 0)

override def toString: String = format(TimestampFormat.DATE_TIME)

private[this] def formatToString(internalFormat: Int): String = {
Expand Down