Loe
08/14/2023, 3:52 PMkotlinx.datetime.LocalDate
?hho
08/14/2023, 5:37 PMLocalDate
, because what's the first day of a week depends on the Locale. For example, a week in en-GB
starts on monday, while a week in en-US
starts on sunday. AFAIK kotlinx.datetime
has no support for Locales, and therefore also no support for calendar weeks.
For first and last day of the month you can use something like this:
val startOfMonth = LocalDate(yourLocalDate.year, yourLocalDate.monthNumber, 1)
val endOfMonth = LocalDate(yourLocalDate.year, yourLocalDate.monthNumber, yourLocalDate.month.length(yourLocalDate.year.isLeapYear()))
fun Int.isLeapYear() = this % 4 == 0 && (this % 100 != 0 || this % 400 == 0)
Loe
08/14/2023, 5:40 PMfun dateRangeTest() {
val today = Clock.System.now().toLocalDateTime(TimeZone.currentSystemDefault()).date
val firstWeekDay = today.daysShift(-DayOfWeek.values().indexOf(today.dayOfWeek))
val lastWeekDay = today.daysShift(DayOfWeek.SUNDAY.ordinal - today.dayOfWeek.ordinal)
print("++dateRangeTest $firstWeekDay")
print("++dateRangeTest $lastWeekDay")
val firstMonthDay = today.minus(DateTimeUnit.DayBased(days =today.dayOfMonth - 1))
val lastMonthDay = firstMonthDay.plus(DatePeriod(months = 1)).minus(DatePeriod(days = 1))
print("++dateRangeTest $firstMonthDay")
print("++dateRangeTest $lastMonthDay")
}
Loe
08/14/2023, 5:41 PMLoe
08/14/2023, 5:42 PMfirstWeekDay = 2023-08-14
lastWeekDay = 2023-08-20
firstMonthDay = 2023-08-01
lastMonthDay = 2023-08-31
Loe
08/14/2023, 5:44 PMlength
? and I missing some kind of import?hho
08/14/2023, 5:48 PMhho
08/14/2023, 5:49 PMLoe
11/03/2023, 8:14 PM11-01-2023
😿 with this exception:
Uncaught Kotlin exception: kotlin.IllegalArgumentException: Unit duration must be positive, but was 0 days.
This fixed it:
/**
* Get the first and last day of the month.
* adapted from [Getting days of current week using kotlinx-datetime KMM](<https://stackoverflow.com/a/71396000/7234479>)
*/
fun firstAndLastDayOfTheMonth(): Pair<LocalDate, LocalDate> {
val today = currentDate()
val dayOfTheWeekValues = DayOfWeek.values()
val firstWeekDay = today.daysShift(-dayOfTheWeekValues.indexOf(today.dayOfWeek))
val lastWeekDay = firstWeekDay.daysShift(dayOfTheWeekValues.count() - 1)
return Pair(firstWeekDay, lastWeekDay)
}
Marco Pierucci
02/14/2024, 4:17 PMfun Int.isLeapYear() = this % 4 == 0 && (this % 100 != 0 || this % 400 == 0)
This seems to make sense, but how come the one provided by java.Time differs a bit?
public static boolean isLeap(long year) {
return ((year & 3) == 0) && ((year % 100) != 0 || (year % 400) == 0);
}
Also assuming the suggested version works fine, looks like a simple addition, is there any reason this is not shipped in the library by default?
Cheers!hho
02/14/2024, 4:57 PM0
(by doing a binary and
with binary 11
(= decimal 3
) and checking for a result of 0
).
This achieves the exact same thing: If the least two bits (for 2
and 1
) aren't set, it means that the number has to be a clean multiple of 4.
As to why it's not in the library: It is! … but internal
and the Team has already decided to keep it like that. 😞Marco Pierucci
02/14/2024, 5:19 PM