From 1a73613cd126bf8b0992657f456e76691a87cc5e Mon Sep 17 00:00:00 2001 From: fankesyooni Date: Sat, 4 Jun 2022 02:59:05 +0800 Subject: [PATCH] Fix GithubReleaseTool to LocalTime --- .../notify/utils/tool/GithubReleaseTool.kt | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/com/fankes/coloros/notify/utils/tool/GithubReleaseTool.kt b/app/src/main/java/com/fankes/coloros/notify/utils/tool/GithubReleaseTool.kt index 32f0b16..4c2a0d2 100644 --- a/app/src/main/java/com/fankes/coloros/notify/utils/tool/GithubReleaseTool.kt +++ b/app/src/main/java/com/fankes/coloros/notify/utils/tool/GithubReleaseTool.kt @@ -24,11 +24,15 @@ package com.fankes.coloros.notify.utils.tool import android.app.Activity import android.content.Context +import android.icu.text.SimpleDateFormat +import android.icu.util.Calendar +import android.icu.util.TimeZone import com.fankes.coloros.notify.utils.factory.* import okhttp3.* import org.json.JSONObject import java.io.IOException import java.io.Serializable +import java.util.* /** * 获取 Github Release 最新版本工具类 @@ -62,7 +66,7 @@ object GithubReleaseTool { name = getString("name"), htmlUrl = getString("html_url"), content = getString("body"), - date = getString("published_at").replace(oldValue = "T", newValue = " ").replace(oldValue = "Z", newValue = "") + date = getString("published_at").localTime() ).apply { fun showUpdate() = context.showDialog { title = "最新版本 $name" @@ -112,6 +116,18 @@ object GithubReleaseTool { }) } + /** + * 格式化时间为本地时区 + * @return [String] 本地时区时间 + */ + private fun String.localTime() = replace(oldValue = "T", newValue = " ").replace(oldValue = "Z", newValue = "").let { + runCatching { + val local = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ROOT).apply { timeZone = Calendar.getInstance().timeZone } + val current = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ROOT).apply { timeZone = TimeZone.getTimeZone("GMT") } + local.format(current.parse(it)) + }.getOrNull() ?: it + } + /** * Github Release bean * @param name 版本名称