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

Locale-aware date formatting for desktop #1159

Merged
merged 4 commits into from
Mar 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,14 @@ package androidx.compose.material3
import java.text.DateFormat
import java.text.SimpleDateFormat
import java.time.DayOfWeek
import java.time.Instant
import java.time.ZoneOffset
import java.time.format.DateTimeFormatter
import java.time.format.FormatStyle
import java.time.format.TextStyle


@OptIn(ExperimentalMaterial3Api::class)
internal actual class PlatformDateFormat actual constructor(private val locale: CalendarLocale) {
private val delegate = LegacyCalendarModelImpl(locale)

Expand All @@ -40,18 +45,26 @@ internal actual class PlatformDateFormat actual constructor(private val locale:
skeleton: String
): String {
// Note: there is no equivalent in Java for Android's DateFormat.getBestDateTimePattern.
// The JDK SimpleDateFormat expects a pattern, so the results will be "2023Jan7",
// The JDK SimpleDateFormat expects a pattern, so the results for unrecognized skeletons will be "2023Jan7",
// "2023January", etc. in case a skeleton holds an actual ICU skeleton and not a pattern.

// TODO: support ICU skeleton on JVM
// Maybe it will be supported in kotlinx.datetime in the future.
// See https://github.com/Kotlin/kotlinx-datetime/pull/251

// stub: not localized but at least readable variant
val pattern = when(skeleton){
DatePickerDefaults.YearMonthSkeleton -> "MMMM yyyy"
DatePickerDefaults.YearAbbrMonthDaySkeleton -> "MMM d, yyyy"
DatePickerDefaults.YearMonthWeekdayDaySkeleton -> "EEEE, MMMM d, yyyy"
DatePickerDefaults.YearAbbrMonthDaySkeleton -> {
return DateTimeFormatter
.ofLocalizedDate(FormatStyle.MEDIUM)
.localizedBy(locale)
.format(Instant.ofEpochMilli(utcTimeMillis).atOffset(ZoneOffset.UTC))
}
DatePickerDefaults.YearMonthWeekdayDaySkeleton -> {
return DateTimeFormatter
.ofLocalizedDate(FormatStyle.FULL)
.localizedBy(locale)
.format(Instant.ofEpochMilli(utcTimeMillis).atOffset(ZoneOffset.UTC))
}
DatePickerDefaults.YearMonthSkeleton -> "LLLL yyyy" // L is a pattern for standalone month (without day)
else -> skeleton
}
return formatWithPattern(utcTimeMillis, pattern)
Expand Down
Loading