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 getDateFormat method #379

Merged
merged 3 commits into from
Feb 25, 2024
Merged
Show file tree
Hide file tree
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: 4 additions & 19 deletions composeApp/src/commonMain/kotlin/com/clipevery/utils/DateUtils.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.clipevery.utils

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 +48,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()
}

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)
fun getDateText(date: LocalDateTime, pattern: String, locale: Locale): String {
val formatter: DateTimeFormatter = Memoize.memoize(pattern, locale) {
DateTimeFormatter.ofPattern(pattern, locale)
}()
return formatter.format(date)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,15 @@ class OnceFunction<T>(private val function: () -> T) {
}
return result!!
}
}
}

object Memoize {
fun <R, T> memoize(vararg inputs: T, function: () -> R): () -> R {
val cache = mutableMapOf<List<T>, R>()
return {
val key = inputs.toList()
cache.getOrPut(key) { function() }
}
}

}
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
Loading