Skip to content

Commit

Permalink
🐛 Fix getDateFormat method
Browse files Browse the repository at this point in the history
  • Loading branch information
guiyanakuang committed Feb 25, 2024
1 parent e740fbd commit 73ad70a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 19 deletions.
22 changes: 4 additions & 18 deletions composeApp/src/commonMain/kotlin/com/clipevery/utils/DateUtils.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.clipevery.utils

import androidx.compose.runtime.remember
import io.realm.kotlin.types.RealmInstant
import java.text.SimpleDateFormat
import java.time.Instant
import java.time.LocalDateTime
import java.time.ZoneId
Expand Down Expand Up @@ -49,24 +49,10 @@ object DateUtils {
return dateFormatter.format(LocalDateTime.now())
}

fun getDateFormat(date: LocalDateTime, language: String): String {
val locale = when (language) {
"zh" -> Locale.SIMPLIFIED_CHINESE
"en" -> Locale.US
"jp" -> Locale.JAPAN
"es" -> Locale("es", "ES")
else -> Locale.getDefault()
fun getDateText(date: LocalDateTime, pattern: String, locale: Locale): String {
val formatter = remember(pattern, locale) {
DateTimeFormatter.ofPattern(pattern, locale)
}

val pattern = when (language) {
"en" -> "MM/dd/yyyy"
"es" -> "dd/MM/yyyy"
"jp" -> "yyyy/MM/dd"
"zh" -> "yyyy年MM月dd日"
else -> "MM/dd/yyyy"
}

val formatter = SimpleDateFormat(pattern, locale)
return formatter.format(date)
}

Expand Down
18 changes: 17 additions & 1 deletion composeApp/src/desktopMain/kotlin/com/clipevery/i18n/I18n.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import java.io.FileNotFoundException
import java.io.InputStreamReader
import java.nio.charset.StandardCharsets
import java.time.LocalDateTime
import java.util.Locale
import java.util.Properties
import java.util.concurrent.ConcurrentHashMap

Expand Down Expand Up @@ -102,7 +103,22 @@ class CopywriterImpl(private val language: String) : Copywriter {
}

override fun getDate(date: LocalDateTime): String {
return DateUtils.getDateFormat(date, language)
val locale = when (language) {
"zh" -> Locale.SIMPLIFIED_CHINESE
"en" -> Locale.US
"jp" -> Locale.JAPAN
"es" -> Locale("es", "ES")
else -> Locale.getDefault()
}

val pattern = when (language) {
"en" -> "MM/dd/yyyy"
"es" -> "dd/MM/yyyy"
"jp" -> "yyyy/MM/dd"
"zh" -> "yyyy年MM月dd日"
else -> "MM/dd/yyyy"
}
return DateUtils.getDateText(date, pattern, locale)
}

override fun getAbridge(): String {
Expand Down

0 comments on commit 73ad70a

Please sign in to comment.