Kotlin / kotlinx-datetime

KotlinX multiplatform date/time library
Apache License 2.0
2.39k stars 99 forks source link

Feature: optional padding #365

Open kunyavskiy opened 7 months ago

kunyavskiy commented 7 months ago

Padding class now suggests only strict options. You either have NONE or ZERO or SPACE. Same for formating and parsing.

It looks like a common usecase to have a flexible parser but strict formatted.

This can be implemented now as

alternativeParsing({ year(Padding.NONE) }) { year(Padding.ZERO) }

But this is quite verbose and probably suboptimal. I suggest to add Padding.OPTIONAL_ZERO and maybe something else similar. to fix it.

dkhalanskyjb commented 6 months ago

Fair. We should either provide a better way to do it or at least make this code behave optimally (which is doable).

Some arguments against adding OPTIONAL_ZERO:

Let's collect more feedback and see how widespread the problem is. Everyone, please don't hesitate to share the ugly code that you're forced to write. Maybe we'll help you improve it, or maybe we'll see that OPTIONAL_ZERO is an absolute necessity.

kunyavskiy commented 6 months ago

Will it parse something like 2024-3-07? I'm not sure if Padding.NONE allows having padding.

But I agree, that reusing for both formatting and parsing is not a common case.

dkhalanskyjb commented 6 months ago

Will it parse something like 2024-3-07?

Yes.

I'm not sure if Padding.NONE allows having padding.

It does. We'll include this in the documentation, this information is missing.