I miss some function which would tell me if year i...
# kotlinx-datetime
j
I miss some function which would tell me if year is leap. Did I miss something or it is really missing?
👀 1
i
There's no direct way so far, but you can construct the dates Mar 1 and Feb 1 of that year, find the number of days between them and decide whether the year is leap based on that number. What is your use case, though?
j
I have a date and I want to know number of days in that month. date.month.length() requires to pass that boolean.
i
In that case you can construct the first day of that month, add one month to it, and then find the number of days between these two dates:
Copy code
val d1 = LocalDate(date.year, date.month, 1)
val d2 = d1.plus(1, DateTimeUnit.MONTH)
val days = d1.until(d2, DateTimeUnit.DAY)
j
Thanks, I did it somehow similarly (also restructured the code to not need it directly this way). But the main issue here IMO the current API which asks for something I don't have. Either I'd like a) to not provide the isYearLeap. (
date.month.length
) (I know, it's enum, so maybe something like
date.monthLength
). b) or provide easily (
date.month.length(date.isYearLeap)
or
date.month.length(isYearLeap(date))
)