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

fix: time not displayed correctly on Samsung devices [WPB-10598] #3359

Merged
merged 1 commit into from
Aug 23, 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
23 changes: 15 additions & 8 deletions app/src/main/kotlin/com/wire/android/util/DateAndTimeParsers.kt
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,12 @@ class DateAndTimeParsers private constructor() {

companion object {
private val dateTimeFormatter = DateTimeFormatter.ISO_DATE_TIME.withZone(ZoneId.of("UTC"))
private val longDateShortTimeFormat = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG, FormatStyle.SHORT)
.withZone(ZoneId.systemDefault()).withLocale(Locale.getDefault())

private val longDateShortTimeFormat =
java.text.DateFormat.getDateTimeInstance(java.text.DateFormat.LONG, java.text.DateFormat.SHORT, Locale.getDefault()).apply {
this.timeZone = java.util.TimeZone.getDefault()
}

private val mediumDateTimeFormat = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM, FormatStyle.MEDIUM)
.withZone(ZoneId.systemDefault()).withLocale(Locale.getDefault())
private val fullDateShortTimeFormatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.FULL, FormatStyle.SHORT)
Expand All @@ -83,8 +87,9 @@ class DateAndTimeParsers private constructor() {
DateTimeFormatter.ofPattern("MMM dd yyyy, hh:mm a", Locale.getDefault()).withZone(ZoneId.systemDefault())
private val mediumOnlyDateTimeFormat =
DateTimeFormatter.ofPattern("MMM dd, yyyy", Locale.getDefault()).withZone(ZoneId.systemDefault())
private val messageTimeFormatter = DateTimeFormatter.ofLocalizedTime(FormatStyle.SHORT)
.withZone(ZoneId.systemDefault()).withLocale(Locale.getDefault())
private val messageTimeFormatter = java.text.DateFormat.getTimeInstance(java.text.DateFormat.SHORT, Locale.getDefault()).apply {
this.timeZone = java.util.TimeZone.getDefault()
}

@Deprecated("Date String parsing is discouraged and will be removed soon for direct Instant/DateTime versions")
fun serverDate(stringDate: String): Date? {
Expand All @@ -98,7 +103,7 @@ class DateAndTimeParsers private constructor() {

@Deprecated("Date String parsing is discouraged and will be removed soon for direct Instant/DateTime versions")
fun deviceDateTimeFormat(stringDate: String): String? = try {
stringDate.serverDate()?.let { longDateShortTimeFormat.format(it.toInstant()) }
longDateShortTimeFormat.format(Date.from(java.time.Instant.parse(stringDate)))
} catch (e: Exception) {
null
}
Expand Down Expand Up @@ -126,9 +131,11 @@ class DateAndTimeParsers private constructor() {
fun toMediumOnlyDateTime(date: Date): String = mediumOnlyDateTimeFormat.format(date.toInstant())

@Deprecated("Date String parsing is discouraged and will be removed soon for direct Instant/DateTime versions")
fun uiMessageDateTime(stringDate: String): String? = stringDate
.serverDate()?.let { serverDate ->
messageTimeFormatter.format(serverDate.toInstant())
fun uiMessageDateTime(stringDate: String): String? =
try {
messageTimeFormatter.format(Date.from(java.time.Instant.parse(stringDate)))
} catch (e: Exception) {
null
}
}
}
Loading