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 1 commit
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
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 nanoseconds") {
forAll { (i: Instant) =>
val ts = Timestamp.fromInstant(i).truncateNanos
val strippedInstant = Instant.ofEpochMilli(i.toEpochMilli)
val tsFromStrippedInstant = Timestamp.fromInstant(strippedInstant)

expect.same(ts, tsFromStrippedInstant)
}
}

property("Truncate milliseconds") {
forAll { (i: Instant) =>
val ts = Timestamp.fromInstant(i).truncateMillis
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 with a milisecond resolution
*/
def truncateNanos: Timestamp = copy(nano = (nano / 1000000) * 1000000)

/**
* @return a copy of this timestamp with a second resolution
*/
def truncateMillis: 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 with a milisecond resolution
*/
def truncateNanos: Timestamp = copy(nano = (nano / 1000000) * 1000000)
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm finding the naming pretty confusing. I think it should be truncateToSeconds and truncateToMillis to make it clearer. For instance truncateToSeconds would truncate anything more precise than seconds.

Copy link
Contributor

Choose a reason for hiding this comment

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

Alternatively : toSecondPrecision and toMillsecondPrecision ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

renamed as truncateToSeconds etc.


/**
* @return a copy of this timestamp with a second resolution
*/
def truncateMillis: Timestamp = copy(nano = 0)

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

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