2024-08-20 19:48:12 +08:00
|
|
|
package com.aiosman.riderpro.exp
|
|
|
|
|
|
2024-09-01 20:11:58 +08:00
|
|
|
import android.content.Context
|
2024-08-20 19:48:12 +08:00
|
|
|
import android.icu.text.SimpleDateFormat
|
2024-08-24 21:59:16 +08:00
|
|
|
import android.icu.util.Calendar
|
2024-09-01 20:11:58 +08:00
|
|
|
import androidx.compose.ui.res.stringResource
|
|
|
|
|
import com.aiosman.riderpro.R
|
2024-08-20 19:48:12 +08:00
|
|
|
import com.aiosman.riderpro.data.api.ApiClient
|
|
|
|
|
import java.util.Date
|
|
|
|
|
import java.util.Locale
|
|
|
|
|
|
2024-08-24 23:11:20 +08:00
|
|
|
/**
|
|
|
|
|
* 格式化时间为 xx 前
|
|
|
|
|
*/
|
2024-09-01 20:11:58 +08:00
|
|
|
fun Date.timeAgo(context: Context): String {
|
2024-08-20 19:48:12 +08:00
|
|
|
val now = Date()
|
|
|
|
|
val diffInMillis = now.time - this.time
|
|
|
|
|
|
|
|
|
|
val seconds = diffInMillis / 1000
|
|
|
|
|
val minutes = seconds / 60
|
|
|
|
|
val hours = minutes / 60
|
|
|
|
|
val days = hours / 24
|
|
|
|
|
val years = days / 365
|
|
|
|
|
|
|
|
|
|
return when {
|
2024-09-01 20:11:58 +08:00
|
|
|
seconds < 60 -> context.getString(R.string.second_ago, seconds)
|
|
|
|
|
minutes < 60 -> context.getString(R.string.minute_ago, minutes)
|
|
|
|
|
hours < 24 -> context.getString(R.string.hour_ago, hours)
|
|
|
|
|
days < 365 -> context.getString(R.string.days_ago, days)
|
|
|
|
|
else -> SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()).format(this)
|
2024-08-20 19:48:12 +08:00
|
|
|
}
|
2024-08-24 21:59:16 +08:00
|
|
|
}
|
|
|
|
|
|
2024-08-24 23:11:20 +08:00
|
|
|
/**
|
|
|
|
|
* 格式化时间为 xx-xx
|
|
|
|
|
*/
|
2024-08-24 21:59:16 +08:00
|
|
|
fun Date.formatPostTime(): String {
|
|
|
|
|
val now = Calendar.getInstance()
|
|
|
|
|
val calendar = Calendar.getInstance()
|
|
|
|
|
calendar.time = this
|
|
|
|
|
val year = calendar.get(Calendar.YEAR)
|
|
|
|
|
var nowYear = now.get(Calendar.YEAR)
|
|
|
|
|
val dateFormat = if (year == nowYear) {
|
|
|
|
|
SimpleDateFormat("MM-dd", Locale.getDefault())
|
|
|
|
|
} else {
|
|
|
|
|
SimpleDateFormat("yyyy-MM-dd", Locale.getDefault())
|
|
|
|
|
}
|
|
|
|
|
return dateFormat.format(this)
|
2024-08-20 19:48:12 +08:00
|
|
|
}
|