Skip to content
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

Provide custom formats for DateTimePeriod #349

Open
dkhalanskyjb opened this issue Feb 29, 2024 · 5 comments
Open

Provide custom formats for DateTimePeriod #349

dkhalanskyjb opened this issue Feb 29, 2024 · 5 comments
Labels
formatters Related to parsing and formatting

Comments

@dkhalanskyjb
Copy link
Collaborator

Currently, DateTimePeriod supports toString and parse using the ISO 8601 format. With the recent introduction of the parsing and formatting machinery, we can add support for arbitrary formats.

@dkhalanskyjb dkhalanskyjb added the formatters Related to parsing and formatting label Feb 29, 2024
@jfontsaballs
Copy link

kotlinx.datetime.format.DateTimeFormat implementations should be public to enable custom formats to be created.

@dkhalanskyjb
Copy link
Collaborator Author

@jfontsaballs, please provide an example of what you can't currently express with the API we provide. What if it's already possible?

@jfontsaballs
Copy link

jfontsaballs commented Oct 30, 2024

I need to parse data received from a third party that does not conform to ISO standards.

LocalTime.parse("9:16")

Throws:

kotlinx.datetime.DateTimeFormatException: java.time.format.DateTimeParseException: Text '9:16' could not be parsed at index 0
	at kotlinx.datetime.LocalTime$Companion.parse(LocalTime.kt:53)
	at kotlinx.datetime.LocalTime$Companion.parse$default(LocalTime.kt:48)
	at Line_3.<init>(Line_3.kts:3)
Caused by: java.time.format.DateTimeParseException: Text '9:16' could not be parsed at index 0
	at java.base/java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:2052)
	at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1954)
	at java.base/java.time.LocalTime.parse(LocalTime.java:465)
	at java.base/java.time.LocalTime.parse(LocalTime.java:450)
	at kotlinx.datetime.LocalTime$Companion.parse(LocalTime.kt:51)

Currently I'm using the Java API:

import java.time.LocalTime
import java.time.format.DateTimeFormatterBuilder
import java.time.temporal.ChronoField

private val formatter = DateTimeFormatterBuilder()
  .appendValue(ChronoField.HOUR_OF_DAY)
  .appendLiteral(':')
  .appendValue(ChronoField.MINUTE_OF_HOUR)
  .toFormatter()
val JsonElement.localTime get() = LocalTime.parse(this.string, formatter).toKotlinLocalTime()

I would like to write the following code but those APIs are internal.

    LocalTimeFormat.build {
        hour(Padding.NONE)
        char(':')
        minute(Padding.NONE)
    }

@dkhalanskyjb
Copy link
Collaborator Author

@jfontsaballs, it's available from a different entry point:

import kotlinx.datetime.*
import kotlinx.datetime.format.*

fun main() {
    val format = LocalTime.Format {
        hour(Padding.NONE)
        char(':')
        minute(Padding.NONE)
    }
    println("The time is ${format.parse("9:16")}")
}

Runnable: https://pl.kotl.in/znkv3rbiZ

Please also take a look at https://github.com/Kotlin/kotlinx-datetime?tab=readme-ov-file#working-with-other-string-formats

@jfontsaballs
Copy link

Great, thank you!

I'd like to request kotlinx.datetime.format.ISO_TIME (and other similar constants) be changed to use the public API, in order to improve discoverability of it when inspecting the code without having read the documentation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
formatters Related to parsing and formatting
Projects
None yet
Development

No branches or pull requests

2 participants