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 12 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 8 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
2 changes: 2 additions & 0 deletions core/api/kotlinx-datetime.api
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,7 @@ public final class kotlinx/datetime/LocalDateTime$Companion {
public final class kotlinx/datetime/LocalDateTime$Formats {
public static final field INSTANCE Lkotlinx/datetime/LocalDateTime$Formats;
public final fun getISO ()Lkotlinx/datetime/format/DateTimeFormat;
public final fun getISO_BASIC ()Lkotlinx/datetime/format/DateTimeFormat;
}

public final class kotlinx/datetime/LocalDateTimeKt {
Expand Down Expand Up @@ -494,6 +495,7 @@ public final class kotlinx/datetime/LocalTime$Companion {
public final class kotlinx/datetime/LocalTime$Formats {
public static final field INSTANCE Lkotlinx/datetime/LocalTime$Formats;
public final fun getISO ()Lkotlinx/datetime/format/DateTimeFormat;
public final fun getISO_BASIC ()Lkotlinx/datetime/format/DateTimeFormat;
}

public final class kotlinx/datetime/LocalTimeKt {
Expand Down
4 changes: 4 additions & 0 deletions core/api/kotlinx-datetime.klib.api
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,8 @@ final class kotlinx.datetime/LocalDateTime : kotlin/Comparable<kotlinx.datetime/
final object Formats { // kotlinx.datetime/LocalDateTime.Formats|null[0]
final val ISO // kotlinx.datetime/LocalDateTime.Formats.ISO|{}ISO[0]
final fun <get-ISO>(): kotlinx.datetime.format/DateTimeFormat<kotlinx.datetime/LocalDateTime> // kotlinx.datetime/LocalDateTime.Formats.ISO.<get-ISO>|<get-ISO>(){}[0]
final val ISO_BASIC // kotlinx.datetime/LocalDateTime.Formats.ISO_BASIC|{}ISO_BASIC[0]
final fun <get-ISO_BASIC>(): kotlinx.datetime.format/DateTimeFormat<kotlinx.datetime/LocalDateTime> // kotlinx.datetime/LocalDateTime.Formats.ISO_BASIC.<get-ISO_BASIC>|<get-ISO_BASIC>(){}[0]
}
}

Expand Down Expand Up @@ -480,6 +482,8 @@ final class kotlinx.datetime/LocalTime : kotlin/Comparable<kotlinx.datetime/Loca
final object Formats { // kotlinx.datetime/LocalTime.Formats|null[0]
final val ISO // kotlinx.datetime/LocalTime.Formats.ISO|{}ISO[0]
final fun <get-ISO>(): kotlinx.datetime.format/DateTimeFormat<kotlinx.datetime/LocalTime> // kotlinx.datetime/LocalTime.Formats.ISO.<get-ISO>|<get-ISO>(){}[0]
final val ISO_BASIC // kotlinx.datetime/LocalTime.Formats.ISO_BASIC|{}ISO_BASIC[0]
final fun <get-ISO_BASIC>(): kotlinx.datetime.format/DateTimeFormat<kotlinx.datetime/LocalTime> // kotlinx.datetime/LocalTime.Formats.ISO_BASIC.<get-ISO_BASIC>|<get-ISO_BASIC>(){}[0]
}
}

Expand Down
19 changes: 19 additions & 0 deletions core/common/src/LocalDateTime.kt
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,25 @@ public expect class LocalDateTime : Comparable<LocalDateTime> {
* @sample kotlinx.datetime.test.samples.LocalDateTimeSamples.Formats.iso
*/
public val ISO: DateTimeFormat<LocalDateTime>

/**
* ISO 8601 basic format.
*
* Examples of datetime in ISO 8601 format:
* - `20200830T1843`
* - `+120200830T184300`
* - `00000830T184300.5`
* - `-00010830T184300.123456789`
*
* When formatting, seconds are included, only if they are non-zero.
* Fractional parts of the second are included if non-zero.
*
* See ISO-8601-1:2019, 5.4.2.1b), the version without the offset, together with
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
* See ISO-8601-1:2019, 5.4.2.1b), the version without the offset, together with
* See ISO-8601-1:2019, 5.4.2.1a), the version without the offset, together with

b) is the extended format.

* [LocalDate.Formats.ISO_BASIC] and [LocalTime.Formats.ISO_BASIC].
*
* @sample kotlinx.datetime.test.samples.LocalDateTimeSamples.Formats.basicIso
*/
public val ISO_BASIC: DateTimeFormat<LocalDateTime>
}

/**
Expand Down
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: `T1234`, `T123456`, `T123456.789`, `T123456.1234`.
*
* When formatting, seconds are always included, even if they are zero.
* Fractional parts of the second are included if non-zero.
*
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think LocalDateTime.ISO_BASIC is also worth mentioning (given that you personally were interested in LocalDateTime.Formats.ISO_BASIC, for example).

* @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
7 changes: 7 additions & 0 deletions core/common/src/format/LocalDateTimeFormat.kt
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,11 @@ internal val ISO_DATETIME by lazy {
}
}

internal val ISO_DATETIME_BASIC by lazy {
LocalDateTimeFormat.build {
date(ISO_DATE_BASIC)
time(ISO_TIME_BASIC)
}
}

private val emptyIncompleteLocalDateTime = IncompleteLocalDateTime()
19 changes: 19 additions & 0 deletions core/common/src/format/LocalTimeFormat.kt
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,25 @@ 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 {
alternativeParsing({ char('t') }) { char('T') }
hour()
minute()
alternativeParsing({
// intentionally empty
}) {
optional {
second()
}
optional {
char('.')
secondFraction(1, 9)
}
}
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
optional {
second()
}
optional {
char('.')
secondFraction(1, 9)
}
}
optional {
second()
optional {
char('.')
secondFraction(1, 9)
}
}
}

As the tests show, otherwise, 12:30:00.123 gets formatted as T1230.123, which is not valid: fractions of a second are only emitted together with the second value itself, even if it is zero.

}
}

internal val ISO_TIME by lazy {
LocalTimeFormat.build {
hour()
Expand Down
35 changes: 35 additions & 0 deletions core/common/test/format/LocalDateTimeFormatTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,41 @@ class LocalDateTimeFormatTest {
test(dateTimes, LocalDateTime.Formats.ISO)
}

@Test
fun testBasicIso() {
val dateTimes = buildMap<LocalDateTime, Pair<String, Set<String>>> {
put(LocalDateTime(2008, 7, 5, 0, 0, 0, 0), ("20080705T0000" to setOf("20080705T0000")))
Copy link
Collaborator

Choose a reason for hiding this comment

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

Here, the same parsable representation is listed twice.

put(LocalDateTime(2007, 12, 31, 1, 0, 0, 0), ("20071231T0100" to setOf("20071231t010000")))
put(LocalDateTime(999, 11, 30, 23, 0, 0, 0), ("09991130T2300" to setOf()))
put(LocalDateTime(-1, 1, 2, 0, 1, 0, 0), ("-00010102T0001" to setOf()))
put(LocalDateTime(9999, 10, 31, 12, 30, 0, 0), ("99991031T1230" to setOf()))
put(LocalDateTime(-9999, 9, 30, 23, 59, 0, 0), ("-99990930T2359" to setOf()))
put(LocalDateTime(10000, 8, 1, 0, 0, 1, 0), ("+100000801T000001" to setOf()))
put(LocalDateTime(-10000, 7, 1, 0, 0, 59, 0), ("-100000701T000059" to setOf()))
put(LocalDateTime(123456, 6, 1, 13, 44, 0, 0), ("+1234560601T1344" to setOf()))
put(LocalDateTime(-123456, 5, 1, 13, 44, 0, 0), ("-1234560501T1344" to setOf()))
put(LocalDateTime(123456, 6, 1, 0, 0, 0, 100000000), ("+1234560601T0000.1" to setOf("+1234560601T000000.1", "+1234560601T000000.10", "+1234560601T000000.100")))
put(LocalDateTime(-123456, 5, 1, 0, 0, 0, 10000000), ("-1234560501T0000.01" to setOf("-1234560501T000000.01")))
put(LocalDateTime(2022, 1, 2, 0, 0, 0, 1000000), ("20220102T0000.001" to setOf()))
put(LocalDateTime(2022, 1, 2, 0, 0, 0, 100000), ("20220102T0000.0001" to setOf()))
put(LocalDateTime(2022, 1, 2, 0, 0, 0, 10000), ("20220102T0000.00001" to setOf()))
put(LocalDateTime(2022, 1, 2, 0, 0, 0, 1000), ("20220102T0000.000001" to setOf()))
put(LocalDateTime(2022, 1, 2, 0, 0, 0, 100), ("20220102T0000.0000001" to setOf()))
put(LocalDateTime(2022, 1, 2, 0, 0, 0, 10), ("20220102T0000.00000001" to setOf()))
put(LocalDateTime(2022, 1, 2, 0, 0, 0, 1), ("20220102T0000.000000001" to setOf()))
put(LocalDateTime(2022, 1, 2, 0, 0, 0, 999999999), ("20220102T0000.999999999" to setOf()))
put(LocalDateTime(2022, 1, 2, 0, 0, 0, 99999999), ("20220102T0000.099999999" to setOf()))
put(LocalDateTime(2022, 1, 2, 0, 0, 0, 9999999), ("20220102T0000.009999999" to setOf()))
put(LocalDateTime(2022, 1, 2, 0, 0, 0, 999999), ("20220102T0000.000999999" to setOf()))
put(LocalDateTime(2022, 1, 2, 0, 0, 0, 99999), ("20220102T0000.000099999" to setOf()))
put(LocalDateTime(2022, 1, 2, 0, 0, 0, 9999), ("20220102T0000.000009999" to setOf()))
put(LocalDateTime(2022, 1, 2, 0, 0, 0, 999), ("20220102T0000.000000999" to setOf()))
put(LocalDateTime(2022, 1, 2, 0, 0, 0, 99), ("20220102T0000.000000099" to setOf()))
put(LocalDateTime(2022, 1, 2, 0, 0, 0, 9), ("20220102T0000.000000009" to setOf()))
}
test(dateTimes, LocalDateTime.Formats.ISO_BASIC)
}

@Test
fun testDoc() {
val dateTime = LocalDateTime(2020, 8, 30, 18, 43, 13, 0)
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) == "T0830")
}

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

public actual object Formats {
public actual val ISO: DateTimeFormat<LocalDateTime> = ISO_DATETIME
public actual val ISO_BASIC: DateTimeFormat<LocalDateTime> = ISO_DATETIME_BASIC
}

public actual constructor(year: Int, month: Int, day: Int, hour: Int, minute: Int, second: Int, nanosecond: Int) :
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/LocalDateTimeJvm.kt
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ public actual class LocalDateTime internal constructor(

public actual object Formats {
public actual val ISO: DateTimeFormat<LocalDateTime> = ISO_DATETIME
public actual val ISO_BASIC: DateTimeFormat<LocalDateTime> = ISO_DATETIME_BASIC
}

private fun writeReplace(): Any = Ser(Ser.DATE_TIME_TAG, this)
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