-
Notifications
You must be signed in to change notification settings - Fork 110
LocalTime#toSecondOfDay & LocalTime#ofSecondOfDay. #204
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -40,6 +40,20 @@ public expect class LocalTime : Comparable<LocalTime> { | |||||
*/ | ||||||
public fun parse(isoString: String): LocalTime | ||||||
|
||||||
/** | ||||||
* Returns a LocalTime with the specified [secondOfDay]. The nanosecond field will be set to zero. | ||||||
* | ||||||
* @throws IllegalArgumentException if the boundaries of [secondOfDay] are exceeded. | ||||||
*/ | ||||||
public fun fromSecondOfDay(secondOfDay: Int): LocalTime | ||||||
|
||||||
/** | ||||||
* Returns a LocalTime with the specified [nanosecondOfDay]. | ||||||
* | ||||||
* @throws IllegalArgumentException if the boundaries of [nanosecondOfDay] are exceeded. | ||||||
vanniktech marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
*/ | ||||||
public fun fromNanosecondOfDay(nanosecondOfDay: Long): LocalTime | ||||||
|
||||||
internal val MIN: LocalTime | ||||||
internal val MAX: LocalTime | ||||||
} | ||||||
|
@@ -66,6 +80,12 @@ public expect class LocalTime : Comparable<LocalTime> { | |||||
/** Returns the nanosecond-of-second time component of this time value. */ | ||||||
public val nanosecond: Int | ||||||
|
||||||
/** 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. */ | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
public val nanosecondOfDay: Long | ||||||
|
||||||
/** | ||||||
* Compares `this` time value with the [other] time value. | ||||||
* Returns zero if this value is equal to the other, a negative number if this value occurs earlier | ||||||
|
@@ -110,4 +130,4 @@ public fun LocalTime.atDate(year: Int, month: Month, dayOfMonth: Int = 0): Local | |||||
/** | ||||||
* Combines this time's components with the specified [LocalDate] components into a [LocalDateTime] value. | ||||||
*/ | ||||||
public fun LocalTime.atDate(date: LocalDate): LocalDateTime = LocalDateTime(date, this) | ||||||
public fun LocalTime.atDate(date: LocalDate): LocalDateTime = LocalDateTime(date, this) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,8 +6,6 @@ | |
package kotlinx.datetime.test | ||
|
||
import kotlinx.datetime.* | ||
import kotlinx.datetime.Clock | ||
import kotlin.math.min | ||
import kotlin.test.* | ||
|
||
class LocalTimeTest { | ||
|
@@ -85,6 +83,58 @@ class LocalTimeTest { | |
assertFailsWith<IllegalArgumentException> { LocalTime(0, 0, 0, 1_000_000_000) } | ||
} | ||
|
||
@Test | ||
fun fromNanosecondOfDay() { | ||
val data = mapOf( | ||
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), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
) | ||
|
||
data.forEach { (nanosecondOfDay, localTime) -> | ||
assertEquals(nanosecondOfDay, localTime.nanosecondOfDay) | ||
assertEquals(localTime, LocalTime.fromNanosecondOfDay(nanosecondOfDay)) | ||
} | ||
} | ||
|
||
@Test | ||
fun fromNanosecondOfDayInvalid() { | ||
LocalTime.fromNanosecondOfDay(0) | ||
vanniktech marked this conversation as resolved.
Show resolved
Hide resolved
|
||
assertFailsWith<IllegalArgumentException> { LocalTime.fromNanosecondOfDay(-1) } | ||
assertFailsWith<IllegalArgumentException> { LocalTime.fromNanosecondOfDay(Long.MAX_VALUE) } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
} | ||
|
||
@Test | ||
fun fromSecondOfDay() { | ||
val data = mapOf( | ||
0 to LocalTime(0, 0), | ||
5 to LocalTime(0, 0, 5), | ||
44105 to LocalTime(12, 15, 5), | ||
86399 to LocalTime(23, 59, 59), | ||
) | ||
|
||
data.forEach { (secondOfDay, localTime) -> | ||
assertEquals(secondOfDay, localTime.secondOfDay) | ||
assertEquals(localTime, LocalTime.fromSecondOfDay(secondOfDay)) | ||
} | ||
} | ||
|
||
@Test | ||
fun fromSecondOfDayInvalid() { | ||
LocalTime.fromSecondOfDay(0) | ||
assertFailsWith<IllegalArgumentException> { LocalTime.fromSecondOfDay(-1) } | ||
assertFailsWith<IllegalArgumentException> { LocalTime.fromSecondOfDay(Int.MAX_VALUE) } | ||
vanniktech marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
@Test | ||
fun fromSecondOfDayIgnoresNanosecond() { | ||
assertEquals( | ||
0, | ||
LocalTime(0, 0, 0, 100).secondOfDay, | ||
) | ||
} | ||
|
||
@Test | ||
fun atDate() { | ||
val time = LocalTime(12, 1, 59) | ||
|
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, ok, my bad.