Skip to content

Commit

Permalink
fix: time not displayed correctly on Samsung devices [WPB-10598] 🍒 (#…
Browse files Browse the repository at this point in the history
…3361)

Co-authored-by: Mohamad Jaara <[email protected]>
Co-authored-by: Yamil Medina <[email protected]>
  • Loading branch information
3 people authored Aug 26, 2024
1 parent ef29e32 commit ea42d47
Showing 1 changed file with 15 additions and 8 deletions.
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
}
}
}

0 comments on commit ea42d47

Please sign in to comment.