Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DatePicker. Fix today circle #782

Merged
merged 12 commits into from
Sep 5, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,26 @@ import kotlinx.datetime.LocalDate
import kotlinx.datetime.Month
import kotlinx.datetime.TimeZone
import kotlinx.datetime.atStartOfDayIn
import kotlinx.datetime.atTime
import kotlinx.datetime.isoDayNumber
import kotlinx.datetime.plus
import kotlinx.datetime.toInstant
import kotlinx.datetime.toLocalDateTime

internal class KotlinxDatetimeCalendarModel : CalendarModel {

override val today: CalendarDate
get() = Clock.System.now()
.toLocalDateTime(systemTZ)
.date
.atStartOfDayIn(TimeZone.UTC)
.toCalendarDate(TimeZone.UTC)
get() {
val localDate = Clock.System.now().toLocalDateTime(systemTZ)
return CalendarDate(
year = localDate.year,
month = localDate.monthNumber,
dayOfMonth = localDate.dayOfMonth,
utcTimeMillis = localDate.date
.atStartOfDayIn(TimeZone.UTC)
Copy link
Member

Choose a reason for hiding this comment

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

https://kotlinlang.org/api/kotlinx-datetime/kotlinx-datetime/kotlinx.datetime/at-start-of-day-in.html says atStartOfDay isn't necessarily at midnight, and we do want midnight here because of how isToday is computed:

val dateInMillis = month.startUtcTimeMillis + (dayNumber * MillisecondsIn24Hours)
val isToday = dateInMillis == today.utcTimeMillis

that's why I suggested to use atTime(0,0).

Copy link
Member

Choose a reason for hiding this comment

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

Or maybe both will be wrong, that is neither will match dateInMillis? I'm not sure. The way isToday is computed looks bad to me.

But Android uses

utcTimeMillis = systemLocalDate.atTime(LocalTime.MIDNIGHT)
                    .atZone(utcTimeZoneId).toInstant().toEpochMilli()

so maybe it's better to stick with that, unless you're sure atStartOfDayIn is correct.

Copy link
Author

@alexzhirkevich alexzhirkevich Aug 31, 2023

Choose a reason for hiding this comment

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

Hmm, this is what CalendarModel interface comments (from canonical date method) says about it:

* The returned date will hold milliseconds value that represent the start of the day, which may
* be different than the one provided to this function.

So it does not exactly say it needs to be 00:00, but start of day 🤔

Maybe today method should behave differently


Newer android calendar model takes it at midnight, lets do the same
(but canonical date it takes at start of day 🥲🔫)

Copy link
Author

Choose a reason for hiding this comment

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

Aligned all methods with the Android implementation

.toEpochMilliseconds()
)
}

override val firstDayOfWeek: Int
get() = PlatformDateFormat.firstDayOfWeek
Expand Down