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

LocalTime#toSecondOfDay & LocalTime#ofSecondOfDay. #204

Merged
merged 3 commits into from
May 30, 2022

Conversation

vanniktech
Copy link
Contributor

Motivation: I'm storing my LocalTime using second of day to leverage sql sorting

Copy link
Member

@ilya-g ilya-g left a comment

Choose a reason for hiding this comment

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

Indeed, it seems convenient to have such conversion functions.
Two points though:

  • the underlying value is a number of nanoseconds since the beginning of day, so it would be consistent to be able to get both nanoseconds of day and seconds of day;
  • naming should be discussed further, so it is consistent with other functions/properties.
    • for example, additional factory methods in Instant are called fromSomething(...).
    • the opposite conversion methods can be either functions, like toNanosecondsOfDay(), or properties, like nanosecondsOfDay.
    • singular or plural form of seconds/nanoseconds

/**
* Returns a LocalTime with the specified [secondOfDay]. The nanosecond field will be set to zero.
*/
public fun ofSecondOfDay(secondOfDay: Int): LocalTime
Copy link
Member

Choose a reason for hiding this comment

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

It should be documented when and what this function throws with the @throws documentation block.
I presume it should be an IllegalArgumentException.
There should be a test checking that such exception or its inheritor is indeed thrown in case of invalid input.

@vanniktech vanniktech force-pushed the local-time-to-second-of-day branch from 7da608e to 5163e27 Compare May 18, 2022 09:04
@vanniktech vanniktech requested a review from ilya-g May 18, 2022 09:05
Copy link
Collaborator

@dkhalanskyjb dkhalanskyjb left a comment

Choose a reason for hiding this comment

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

After an internal discussion, we decided to have fromSecondOfDay/toSecondOfDay and fromNanosecondOfDay/toNanosecondOfDay pairs (that is, the accessors shouldn't be properties).

We also decided that we want [to/from]MillisecondOfDay as well, but if you don't feel like adding it, we'll do it on our own. Also, there's native-only ThreeTenBpLocalTimeTest that has tests for the new functions, and some of them could be ported to common code, but this is also something that could be easier for us to do.

/** Returns the time as a second of a day, from 0 to 24 * 60 * 60 - 1. */
public val secondOfDay: Int

/** Returns the time as a nanosecond of a day, from 0 to 24 * 60 * 60 * 1000 - 1. */
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
/** Returns the time as a nanosecond of a day, from 0 to 24 * 60 * 60 * 1000 - 1. */
/** Returns the time as a nanosecond of a day, from 0 to 24 * 60 * 60 * 1_000_000_000 - 1. */

0L to LocalTime(0, 0),
5000000001L to LocalTime(0, 0, 5, 1),
44105000000100L to LocalTime(12, 15, 5, 100),
86399000000999L to LocalTime(23, 59, 59, 999),
Copy link
Collaborator

Choose a reason for hiding this comment

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

All of these have the millisecond and microsecond triads zeroed. I'd add a test with 123456789 nanoseconds or something like that.

core/common/test/LocalTimeTest.kt Outdated Show resolved Hide resolved
fun fromNanosecondOfDayInvalid() {
LocalTime.fromNanosecondOfDay(0)
assertFailsWith<IllegalArgumentException> { LocalTime.fromNanosecondOfDay(-1) }
assertFailsWith<IllegalArgumentException> { LocalTime.fromNanosecondOfDay(Long.MAX_VALUE) }
Copy link
Collaborator

Choose a reason for hiding this comment

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

A good idea is to check the boundaries. For example, in the test above, in addition to (or instead of) testing 86399000000999L, 86399999999999L, the maximum admissible value, could be tested, and here we could test 86400000000000L, the first non-admissible value.

core/common/test/LocalTimeTest.kt Show resolved Hide resolved
@vanniktech
Copy link
Contributor Author

Regarding ThreeTenBpLocalTimeTest & [to/from]MillisecondOfDay it's probably easiest if you do it yourself since you know what exactly you want.

@vanniktech vanniktech requested a review from dkhalanskyjb May 30, 2022 13:17
Copy link
Collaborator

@dkhalanskyjb dkhalanskyjb left a comment

Choose a reason for hiding this comment

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

The build fails as is, due to exceptions having the wrong type.

/**
* Returns a LocalTime with the specified [secondOfDay]. The nanosecond field will be set to zero.
*
* @throws IllegalArgumentException if the boundaries of [secondOfDay] are exceeded.
Copy link
Collaborator

Choose a reason for hiding this comment

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

The reference is broken now.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's referencing the parameter. Also works for me.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ah, ok, my bad.

core/common/src/LocalTime.kt Show resolved Hide resolved
core/js/src/LocalTime.kt Outdated Show resolved Hide resolved
Copy link
Collaborator

@dkhalanskyjb dkhalanskyjb left a comment

Choose a reason for hiding this comment

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

Good work, thanks!

@dkhalanskyjb dkhalanskyjb merged commit d686d81 into Kotlin:master May 30, 2022
@vanniktech vanniktech deleted the local-time-to-second-of-day branch May 31, 2022 09:58
@ilya-g ilya-g added this to the 0.4.0 milestone Jun 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants