From d01b767d8c1911bee49736dc890bfa472939b8ff Mon Sep 17 00:00:00 2001 From: ghostbuster91 Date: Mon, 7 Oct 2024 16:33:32 +0200 Subject: [PATCH 1/2] Add convince truncate method to Timestamp type --- .../test/src-jvm/smithy4s/TimestampSpec.scala | 19 +++++++++++++++++++ modules/core/src-js/smithy4s/Timestamp.scala | 10 ++++++++++ .../src-jvm-native/smithy4s/Timestamp.scala | 10 ++++++++++ 3 files changed, 39 insertions(+) diff --git a/modules/bootstrapped/test/src-jvm/smithy4s/TimestampSpec.scala b/modules/bootstrapped/test/src-jvm/smithy4s/TimestampSpec.scala index f2b8e40bc..98fb647ab 100644 --- a/modules/bootstrapped/test/src-jvm/smithy4s/TimestampSpec.scala +++ b/modules/bootstrapped/test/src-jvm/smithy4s/TimestampSpec.scala @@ -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) + } + } } diff --git a/modules/core/src-js/smithy4s/Timestamp.scala b/modules/core/src-js/smithy4s/Timestamp.scala index e8e695c02..becc0abe4 100644 --- a/modules/core/src-js/smithy4s/Timestamp.scala +++ b/modules/core/src-js/smithy4s/Timestamp.scala @@ -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 = { diff --git a/modules/core/src-jvm-native/smithy4s/Timestamp.scala b/modules/core/src-jvm-native/smithy4s/Timestamp.scala index ce3664e5c..99237f1fc 100644 --- a/modules/core/src-jvm-native/smithy4s/Timestamp.scala +++ b/modules/core/src-jvm-native/smithy4s/Timestamp.scala @@ -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) + + /** + * @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 = { From f8e54fdc60f5aa3ecbd64fa7a4a67c8fd0145a94 Mon Sep 17 00:00:00 2001 From: ghostbuster91 Date: Tue, 8 Oct 2024 11:08:48 +0200 Subject: [PATCH 2/2] Rename truncate methods to a different convention --- .../test/src-js/smithy4s/TimestampSpec.scala | 26 +++++++++++++++++++ .../test/src-jvm/smithy4s/TimestampSpec.scala | 8 +++--- modules/core/src-js/smithy4s/Timestamp.scala | 8 +++--- .../src-jvm-native/smithy4s/Timestamp.scala | 8 +++--- 4 files changed, 38 insertions(+), 12 deletions(-) diff --git a/modules/bootstrapped/test/src-js/smithy4s/TimestampSpec.scala b/modules/bootstrapped/test/src-js/smithy4s/TimestampSpec.scala index 0ee6c9cf9..010041363 100644 --- a/modules/bootstrapped/test/src-js/smithy4s/TimestampSpec.scala +++ b/modules/bootstrapped/test/src-js/smithy4s/TimestampSpec.scala @@ -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) + } + } } diff --git a/modules/bootstrapped/test/src-jvm/smithy4s/TimestampSpec.scala b/modules/bootstrapped/test/src-jvm/smithy4s/TimestampSpec.scala index 98fb647ab..f0b928627 100644 --- a/modules/bootstrapped/test/src-jvm/smithy4s/TimestampSpec.scala +++ b/modules/bootstrapped/test/src-jvm/smithy4s/TimestampSpec.scala @@ -225,9 +225,9 @@ class TimestampSpec() extends munit.FunSuite with munit.ScalaCheckSuite { } } - property("Truncate nanoseconds") { + property("Truncate to milliseconds precision") { forAll { (i: Instant) => - val ts = Timestamp.fromInstant(i).truncateNanos + val ts = Timestamp.fromInstant(i).truncateToMillis val strippedInstant = Instant.ofEpochMilli(i.toEpochMilli) val tsFromStrippedInstant = Timestamp.fromInstant(strippedInstant) @@ -235,9 +235,9 @@ class TimestampSpec() extends munit.FunSuite with munit.ScalaCheckSuite { } } - property("Truncate milliseconds") { + property("Truncate to seconds precision") { forAll { (i: Instant) => - val ts = Timestamp.fromInstant(i).truncateMillis + val ts = Timestamp.fromInstant(i).truncateToSeconds val tsFromStrippedInstant = Timestamp.fromEpochSecond(i.getEpochSecond) expect.same(ts, tsFromStrippedInstant) diff --git a/modules/core/src-js/smithy4s/Timestamp.scala b/modules/core/src-js/smithy4s/Timestamp.scala index becc0abe4..d20e83d36 100644 --- a/modules/core/src-js/smithy4s/Timestamp.scala +++ b/modules/core/src-js/smithy4s/Timestamp.scala @@ -48,14 +48,14 @@ case class Timestamp private (epochSecond: Long, nano: Int) { } /** - * @return a copy of this timestamp with a milisecond resolution + * @return a copy of this timestamp truncated to a miliseconds precision */ - def truncateNanos: Timestamp = copy(nano = (nano / 1000000) * 1000000) + def truncateToMillis: Timestamp = copy(nano = (nano / 1000000) * 1000000) /** - * @return a copy of this timestamp with a second resolution + * @return a copy of this timestamp truncated to a seconds resolution */ - def truncateMillis: Timestamp = copy(nano = 0) + def truncateToSeconds: Timestamp = copy(nano = 0) override def toString: String = format(TimestampFormat.DATE_TIME) diff --git a/modules/core/src-jvm-native/smithy4s/Timestamp.scala b/modules/core/src-jvm-native/smithy4s/Timestamp.scala index 99237f1fc..0e71dac44 100644 --- a/modules/core/src-jvm-native/smithy4s/Timestamp.scala +++ b/modules/core/src-jvm-native/smithy4s/Timestamp.scala @@ -40,14 +40,14 @@ case class Timestamp private (epochSecond: Long, nano: Int) def conciseDate: String = formatToString(2) /** - * @return a copy of this timestamp with a milisecond resolution + * @return a copy of this timestamp truncated to a miliseconds precision */ - def truncateNanos: Timestamp = copy(nano = (nano / 1000000) * 1000000) + def truncateToMillis: Timestamp = copy(nano = (nano / 1000000) * 1000000) /** - * @return a copy of this timestamp with a second resolution + * @return a copy of this timestamp truncated to a seconds resolution */ - def truncateMillis: Timestamp = copy(nano = 0) + def truncateToSeconds: Timestamp = copy(nano = 0) override def toString: String = format(TimestampFormat.DATE_TIME)