-
Notifications
You must be signed in to change notification settings - Fork 109
Feature Request: truncateTo for LocalTime #225
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
Comments
Yes, we received similar requests already and are interested in adding this feature once we understand it better. Could you please describe why you need this? |
The program has to track how long you've been "working" or "on break", and in this context, seconds and nanoseconds are negligible, and leaving only hours and minutes makes it trivial to format the time to the user (only have to do .toString() rather than formatting) If you'd like the full context, you can find the source code here |
I've taken a look at your code, and I think the whole approach is buggy in the presence of DST transitions. You use For example, semantically, Another, more serious example is the behavior of the So, I suggest rethinking the whole approach:
Still, the issue stands: it seems like you do want to disregard the sub-minute components when formatting. However, now it seems like your use case also calls for this functionality in |
I'm using this code for truncate Instant (for period >= 1 second) operator fun Instant.rem(period: Duration): Duration {
val periodMillis = period.inWholeMilliseconds
val thisMillis = toEpochMilliseconds()
return (thisMillis % periodMillis).milliseconds
}
fun Instant.floorBy(period: Duration): Instant = this - this % period I'm using it for make flow of current time for ui usage. Would like to see at least |
@demidenko, almost everything from #266 (comment) applies to your solution as well:
So, I'm not convinced it's worth introducing such a brittle operation just for truncation. |
Closing in favor of #325 |
When I was learning Kotlin, I made a JVM application that utilized the java.time.LocalTime class a lot, and it was convenient to use the truncateTo() method to only have hours and minutes. I'm now rewriting the application in Kotlin Native to get even more experience with the language, and I'm learning that to shave off seconds, I need to have an extra variable to truncate a LocalTime:
While this works, I'd much rather only have to add one more line, something like
.truncateTo(DateTimeUnit.SECOND)
.The text was updated successfully, but these errors were encountered: