Skip to content

Kotlin datetime format#1777

Open
Jolanrensen wants to merge 14 commits intomasterfrom
kotlin-datetime-format
Open

Kotlin datetime format#1777
Jolanrensen wants to merge 14 commits intomasterfrom
kotlin-datetime-format

Conversation

@Jolanrensen
Copy link
Copy Markdown
Collaborator

@Jolanrensen Jolanrensen commented Mar 30, 2026

Fixes #876

Changes parsing behavior for kotlin and java date-time, allowing both to be parsed in a more explicit and predictable way.

API changes:

  • ParserOptions has changed and gained DateTimeParserOptions. GlobalParserOptions have also changed.
  • col.convertTo<>()/df.convert {}.to<>() can now always take ParserOptions, and we gained a handful of new shortcuts and ones that had java arguments but returned kotlin types were deprecated.

See new kdocs and commit messages for specifics :)

Needs to be followed up by #1798

@Jolanrensen Jolanrensen force-pushed the kotlin-datetime-format branch 4 times, most recently from 696323a to 6e06b95 Compare April 2, 2026 18:59
@Jolanrensen
Copy link
Copy Markdown
Collaborator Author

Jolanrensen commented Apr 2, 2026

Uhm okay, so the basic new logic is as follows:

We've got three "modes": JAVA_TIME (only parse to java-time types), KOTLIN_DATETIME (only parse to kotlin(x)-datetime types), null, just try them all, but prefer kotlin types.

You can add formats (by type) to customGlobalDateTimeFormats or customGlobalJavaFormatters. If they are detected, they will be attempted first when parsing.

After both those have been checked, only then do we check the defaultDateTimeFormats and defaultJavaFormatters when parsing.

(Locale will now always be taken into account when parsing to java types)

If the user specifies their own custom format(s) or patterns using ParserOptions, all default and global formats will be ignored and only those from the provided ParserOptions will be used. This automatically limits it to either KOTLIN_DATETIME or JAVA_TIME mode.

Conversions from String to any DateTime type will be Kotlin only for now, allowing you to specify a DateTimeFormat with the correct type (or a pattern as shorcut).

TODO: Find a better way to instantiate a correct ParserOptions. I now have about a dozen constructors which is hard to maintain or work with. maybe a subclass? ParserOptions(dateOptions = KotlinDateOptions()) or JavaDateOptions() maybe?

Edit: slightly outdated

@Jolanrensen Jolanrensen force-pushed the kotlin-datetime-format branch 3 times, most recently from ff402cd to f301693 Compare April 9, 2026 11:00
@Jolanrensen Jolanrensen force-pushed the kotlin-datetime-format branch 2 times, most recently from 6802cbb to 8e045f0 Compare April 10, 2026 14:57
@Jolanrensen Jolanrensen force-pushed the kotlin-datetime-format branch 4 times, most recently from 0b7359d to d172cff Compare April 13, 2026 17:13
 - removed `coveredBy` as it was barely used anymore and generally confusing. Parsers can now return SKIP_PARSER to get skipped.
 - parseBy {} shortcut function
 - Parsers can now have a `name` which makes debugging a lot easier
 - parsers can have fallbackParserForConverter, you'll see in the next commits why this is useful
…s as converter logic, taking multiple parsers into account, running them all in order with their fallback mechanisms.
- global dateTimeLibrary setting, allowing skipping all java- or kotlin types
- global/default date-time formats are now typed and stored in a LazyMap. New ones can be provided as DateTimeFormat(ter) or pattern.
- parsing order is now:
  - if parserOptions.dateTime has custom formats, run those, skip all the others
  - run custom global kotlin/java date-time parsers
  - run default ones
- when converting String -> kotlinx datetime, we have a String -> DateTimeComponents -> kotlinx datetime fallback now.
- Locale is now always applied to java date-time if provided by the user
- We can now parse to Month and DayOfWeek
New parser date-time API!
- DateTimeParserOptions.Java/Kotlin
- Updated date-time functions in GlobalParserOptions
- Deprecated old overloads
- updated tests and samples
- See new KDocs for more information
…interesting case, because Duration is a value class)
…we cannot always know the source column is a String column.
- Extra convert duration overloads
- Extra convert instant overloads
- Extra convert overloads from DateTimeComponents to all possible targets
- new convertTo kotlin date-time functions with pattern/format arguments.
- new convertTo java date-time functions with (pattern/formatter)+locale arguments
- tests
@Jolanrensen Jolanrensen force-pushed the kotlin-datetime-format branch from d172cff to c490782 Compare April 13, 2026 18:46
@Jolanrensen Jolanrensen marked this pull request as ready for review April 13, 2026 18:47
@Jolanrensen Jolanrensen requested a review from zaleslaw April 13, 2026 18:48
@Jolanrensen Jolanrensen added this to the 1.0.0-Beta5 milestone Apr 13, 2026
internal fun AnyCol.convertToTypeImpl(to: KType, parserOptions: ParserOptions?): AnyCol {
val from = type

if (parserOptions != null && from.withNullability(false) != typeOf<String>()) {
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I probably should remove this check... It would make:

df.convert { javaDateCol and stringCol }.to<LocalDate>(parserOptions)

crash

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Migrate java.time.format.DateTimeFormatter to kotlinx.datetime.format.DateTimeFormat

1 participant