-
Notifications
You must be signed in to change notification settings - Fork 122
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
base: master
Are you sure you want to change the base?
Changes from 8 commits
c8e2757
cca454f
8d17ebb
8cddb49
76a093a
bae2c6e
8075aa0
8177fe6
a403817
deefb8d
e2e0455
9b19750
2d57be0
6847e0e
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 |
---|---|---|
|
@@ -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 | ||
* [LocalDate.Formats.ISO_BASIC] and [LocalTime.Formats.ISO_BASIC]. | ||
* | ||
* @sample kotlinx.datetime.test.samples.LocalDateTimeSamples.Formats.basicIso | ||
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.
|
||
*/ | ||
public val ISO_BASIC: DateTimeFormat<LocalDateTime> | ||
} | ||
|
||
/** | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
* | ||
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. I think |
||
* @sample kotlinx.datetime.test.samples.LocalTimeSamples.Formats.isoBasic | ||
*/ | ||
public val ISO_BASIC: DateTimeFormat<LocalTime> | ||
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. Please also add tests. Examples: |
||
|
||
/** | ||
* ISO 8601 extended format. | ||
* | ||
|
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -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) | ||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||
|
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.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -224,6 +224,41 @@ class LocalDateTimeFormatTest { | |
test(dateTimes, LocalDateTime.Formats.ISO) | ||
} | ||
|
||
@Test | ||
fun testBasicIso() { | ||
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. Please also add the corresponding test for |
||
val dateTimes = buildMap<LocalDateTime, Pair<String, Set<String>>> { | ||
put(LocalDateTime(2008, 7, 5, 0, 0, 0, 0), ("20080705T0000" to setOf("20080705T0000"))) | ||
|
||
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) | ||
|
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.
b)
is the extended format.