This commit is contained in:
Li ZongYing
2024-02-06 23:03:36 +08:00
parent 0a7837c577
commit 960f890ddd
8 changed files with 911 additions and 24 deletions
+23 -9
View File
@@ -1,41 +1,55 @@
package com.lizongying.mytv
import android.content.res.Resources
import android.os.SystemClock
import android.util.Log
import android.util.TypedValue
import com.google.gson.Gson
import com.lizongying.mytv.api.TimeResponse
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.text.SimpleDateFormat
import java.util.*
import java.util.Date
import java.util.Locale
object Utils {
fun getDateFormat(format: String): String {
return SimpleDateFormat(format, Locale.CHINA).format(Date())
}
suspend fun getDateTimestamp(): Long {
return getTimestampFromServer() / 1000
fun getDateTimestamp(): Long {
return Date().time / 1000
}
init {
CoroutineScope(Dispatchers.Default).launch {
updateTimestampFromServer()
}
}
/**
* 从服务器获取时间戳
* @return Long 时间戳
*/
private suspend fun getTimestampFromServer(): Long {
return withContext(Dispatchers.IO) {
val client = okhttp3.OkHttpClient.Builder().connectTimeout(500, java.util.concurrent.TimeUnit.MILLISECONDS)
private suspend fun updateTimestampFromServer() {
val currentTimeMillis = withContext(Dispatchers.IO) {
val client = okhttp3.OkHttpClient.Builder()
.connectTimeout(500, java.util.concurrent.TimeUnit.MILLISECONDS)
.readTimeout(1, java.util.concurrent.TimeUnit.SECONDS).build()
client.newCall(
okhttp3.Request.Builder().url("https://api.m.taobao.com/rest/api3.do?api=mtop.common.getTimestamp")
okhttp3.Request.Builder()
.url("https://api.m.taobao.com/rest/api3.do?api=mtop.common.getTimestamp")
.build()
).execute().use { response ->
if (!response.isSuccessful) throw java.io.IOException("Unexpected code $response")
val body = response.body()
val string = body?.toString()
val string = response.body()?.string()
Gson().fromJson(string, TimeResponse::class.java).data.t.toLong()
}
}
SystemClock.setCurrentTimeMillis(currentTimeMillis)
}
fun dpToPx(dp: Float): Int {