Skip to content

Added LocalTime.Formats.ISO_BASIC (ISO 8601 basic format) #518

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
12 changes: 12 additions & 0 deletions core/common/src/LocalTime.kt
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,18 @@ public expect class LocalTime : Comparable<LocalTime> {
* [kotlinx.datetime.format.DateTimeFormat] for [LocalTime] values.
*/
public object Formats {
/**
* ISO 8601 basic format.
*
* Examples: `1234`, `123456`, `123456.789`, `123456.1234`.
Copy link
Collaborator

Choose a reason for hiding this comment

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

These examples are not valid for this format.

*
* When formatting, seconds are always included, even if they are zero.
* Fractional parts of the second are included if non-zero.
*
* @sample kotlinx.datetime.test.samples.LocalTimeSamples.Formats.isoBasic
*/
public val ISO_BASIC: DateTimeFormat<LocalTime>
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please also add tests. Examples: LocalDateTimeFormatTest#testIso, LocalDateFormatTest#testBasicIso, etc.


/**
* ISO 8601 extended format.
*
Expand Down
17 changes: 17 additions & 0 deletions core/common/src/format/LocalTimeFormat.kt
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,23 @@ internal class LocalTimeFormat(override val actualFormat: CachedFormatStructure<
}

// these are constants so that the formats are not recreated every time they are used
internal val ISO_TIME_BASIC by lazy {
LocalTimeFormat.build {
char('T')
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please see internal val ISO_DATETIME: our format is case-insensitive.

hour()
minute()
alternativeParsing({
// intentionally empty
}) {
second()
optional {
char('.')
secondFraction(1, 9)
}
}
}
}

internal val ISO_TIME by lazy {
LocalTimeFormat.build {
hour()
Expand Down
14 changes: 14 additions & 0 deletions core/common/test/samples/LocalTimeSamples.kt
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,20 @@ class LocalTimeSamples {
}

class Formats {
@Test
fun isoBasic() {
// Parsing and formatting LocalTime values using the ISO_BASIC format
val timeWithNanoseconds = LocalTime(hour = 8, minute = 30, second = 15, nanosecond = 160_000_000)
val timeWithSeconds = LocalTime(hour = 8, minute = 30, second = 15)
val timeWithoutSeconds = LocalTime(hour = 8, minute = 30)
check(LocalTime.Formats.ISO_BASIC.parse("T083015.16") == timeWithNanoseconds)
check(LocalTime.Formats.ISO_BASIC.parse("T083015") == timeWithSeconds)
check(LocalTime.Formats.ISO_BASIC.parse("T0830") == timeWithoutSeconds)
check(LocalTime.Formats.ISO_BASIC.format(timeWithNanoseconds) == "T083015.16")
check(LocalTime.Formats.ISO_BASIC.format(timeWithSeconds) == "T083015")
check(LocalTime.Formats.ISO_BASIC.format(timeWithoutSeconds) == "T083000")
}

@Test
fun iso() {
// Parsing and formatting LocalTime values using the ISO format
Expand Down
1 change: 1 addition & 0 deletions core/commonKotlin/src/LocalTime.kt
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ public actual class LocalTime actual constructor(
}

public actual object Formats {
public actual val ISO_BASIC: DateTimeFormat<LocalTime> get() = ISO_TIME_BASIC
public actual val ISO: DateTimeFormat<LocalTime> get() = ISO_TIME
}

Expand Down
1 change: 1 addition & 0 deletions core/jvm/src/LocalTimeJvm.kt
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ public actual class LocalTime internal constructor(
}

public actual object Formats {
public actual val ISO_BASIC: DateTimeFormat<LocalTime> get() = ISO_TIME_BASIC
public actual val ISO: DateTimeFormat<LocalTime> get() = ISO_TIME

}
Expand Down