kotlinx-datetime icon indicating copy to clipboard operation
kotlinx-datetime copied to clipboard

Add a dayOfYear option

Open tylerwilson opened this issue 1 year ago • 3 comments

I need to parse / construct a Julian date, which requires the day of year value. It would fall in nicely with the dayOfMonth and dayOfWeek values. :-)

Or, is there a way I can construct my own WithDate extension that would allow me to add it on my end?

tylerwilson avatar Jul 22 '24 16:07 tylerwilson

I need to parse / construct a Julian date

Which one is it? You can construct the value (though the API isn't nice) today:

LocalDate(year, 1, 1).plus(dayOfYear - 1, DateTimeUnit.DAY)

Parsing from strings, on the other hand, is only available via dirty hacks.

dkhalanskyjb avatar Jul 23 '24 06:07 dkhalanskyjb

It is actually for parsing. I have a date time in the format of "yyDDDHHmm". I have been using the format helpers to do most of my parsing thus far. Perhaps the string based version might handle this?

Is this - dayOfYear - something we think could make it in a future version?

tylerwilson avatar Jul 23 '24 14:07 tylerwilson

Okay, I did try to use the fallback of

byUnicodePattern("yyDDDHHmm")

but it then gives an assert with message:

java.lang.UnsupportedOperationException: kotlinx.datetime formatting does not support the day-of-year field. Please report your use case to https://github.com/Kotlin/kotlinx-datetime/issues

So I think it would be useful and a nice way to round out the parsers. :-)

tylerwilson avatar Jul 24 '24 13:07 tylerwilson