169 lines
5.7 KiB
Kotlin
169 lines
5.7 KiB
Kotlin
package com.focusbox.app.util
|
||
|
||
import java.text.SimpleDateFormat
|
||
import java.util.Calendar
|
||
import java.util.Date
|
||
import java.util.Locale
|
||
|
||
/** 时间格式化与"自然日"边界计算,全局只此一份,避免各处口径不一。 */
|
||
object Dates {
|
||
|
||
private val keyFmt = SimpleDateFormat("yyyy-MM-dd", Locale.US)
|
||
private val mdFmt = SimpleDateFormat("M/d", Locale.CHINA)
|
||
private val mdFmt2 = SimpleDateFormat("M月d日", Locale.CHINA)
|
||
private val hmFmt = SimpleDateFormat("HH:mm", Locale.CHINA)
|
||
private val hmsFmt = SimpleDateFormat("HH:mm:ss", Locale.CHINA)
|
||
|
||
fun dayKey(ms: Long): String = keyFmt.format(Date(ms))
|
||
|
||
fun dayStart(ms: Long): Long {
|
||
val c = Calendar.getInstance()
|
||
c.timeInMillis = ms
|
||
c.set(Calendar.HOUR_OF_DAY, 0)
|
||
c.set(Calendar.MINUTE, 0)
|
||
c.set(Calendar.SECOND, 0)
|
||
c.set(Calendar.MILLISECOND, 0)
|
||
return c.timeInMillis
|
||
}
|
||
|
||
fun dayStartOfKey(key: String): Long = keyFmt.parse(key)?.time ?: 0L
|
||
|
||
fun todayStart(): Long = dayStart(System.currentTimeMillis())
|
||
|
||
/** 本周一 00:00(周一为一周起点,符合国内习惯);传任意时刻则返回它所在周的周一 */
|
||
fun weekStart(ms: Long = System.currentTimeMillis()): Long {
|
||
val c = Calendar.getInstance()
|
||
c.timeInMillis = dayStart(ms)
|
||
val dow = c.get(Calendar.DAY_OF_WEEK) // 1=周日
|
||
val back = if (dow == Calendar.SUNDAY) 6 else dow - 2
|
||
c.add(Calendar.DAY_OF_MONTH, -back)
|
||
return c.timeInMillis
|
||
}
|
||
|
||
/** 所在月的 1 日 00:00;不传参就是本月 */
|
||
fun monthStart(ms: Long = System.currentTimeMillis()): Long {
|
||
val c = Calendar.getInstance()
|
||
c.timeInMillis = dayStart(ms)
|
||
c.set(Calendar.DAY_OF_MONTH, 1)
|
||
return c.timeInMillis
|
||
}
|
||
|
||
/** 所在年的 1 月 1 日 00:00;不传参就是今年 */
|
||
fun yearStart(ms: Long = System.currentTimeMillis()): Long {
|
||
val c = Calendar.getInstance()
|
||
c.timeInMillis = dayStart(ms)
|
||
c.set(Calendar.DAY_OF_MONTH, 1)
|
||
c.set(Calendar.MONTH, Calendar.JANUARY)
|
||
return c.timeInMillis
|
||
}
|
||
|
||
fun addYears(ms: Long, years: Int): Long {
|
||
val c = Calendar.getInstance()
|
||
c.timeInMillis = ms
|
||
c.add(Calendar.YEAR, years)
|
||
return c.timeInMillis
|
||
}
|
||
|
||
fun yearOf(ms: Long): Int {
|
||
val c = Calendar.getInstance()
|
||
c.timeInMillis = ms
|
||
return c.get(Calendar.YEAR)
|
||
}
|
||
|
||
fun monthOf(ms: Long): Int {
|
||
val c = Calendar.getInstance()
|
||
c.timeInMillis = ms
|
||
return c.get(Calendar.MONTH) + 1
|
||
}
|
||
|
||
fun addDays(ms: Long, days: Int): Long {
|
||
val c = Calendar.getInstance()
|
||
c.timeInMillis = ms
|
||
c.add(Calendar.DAY_OF_MONTH, days)
|
||
return c.timeInMillis
|
||
}
|
||
|
||
/** 月末会按当月最后一天收敛(1月31日 + 1月 = 2月28/29日),不会溢出到下个月 */
|
||
fun addMonths(ms: Long, months: Int): Long {
|
||
val c = Calendar.getInstance()
|
||
c.timeInMillis = ms
|
||
c.add(Calendar.MONTH, months)
|
||
return c.timeInMillis
|
||
}
|
||
|
||
/** 12:30 这种时钟显示;不足 10 分钟显示 09:30 两位分 */
|
||
fun clock(ms: Long): String {
|
||
val total = (ms / 1000).coerceAtLeast(0)
|
||
val m = total / 60
|
||
val s = total % 60
|
||
return String.format(Locale.US, "%02d:%02d", m, s)
|
||
}
|
||
|
||
/** 45m / 2h10m / 12.5h,统计里统一用这个口径 */
|
||
fun duration(seconds: Int): String {
|
||
if (seconds < 60) return "${seconds}秒"
|
||
val minutes = seconds / 60
|
||
if (minutes < 60) return "${minutes}分钟"
|
||
val h = minutes / 60
|
||
val m = minutes % 60
|
||
return if (m == 0) "${h}小时" else "${h}小时${m}分"
|
||
}
|
||
|
||
/**
|
||
* 环形图标注用的紧凑口径:20m / 1h 10m / 4h 3m。
|
||
* 参考图里贴着引线的数字要一眼读出分量,中文"小时/分"太长会把标注撑到环外。
|
||
*/
|
||
fun durationBrief(seconds: Int): String {
|
||
if (seconds <= 0) return "0m"
|
||
val minutes = seconds / 60
|
||
if (minutes <= 0) return "${seconds}s"
|
||
if (minutes < 60) return "${minutes}m"
|
||
val h = minutes / 60
|
||
val m = minutes % 60
|
||
return if (m == 0) "${h}h" else "${h}h ${m}m"
|
||
}
|
||
|
||
/** 图表坐标轴用的短格式:2h / 45m */
|
||
fun shortDuration(seconds: Int): String {
|
||
if (seconds <= 0) return "0"
|
||
val minutes = seconds / 60
|
||
if (minutes < 60) return "${minutes}m"
|
||
val h = minutes / 60f
|
||
return if (h >= 10) "${h.toInt()}h" else String.format(Locale.US, "%.1fh", h)
|
||
}
|
||
|
||
fun md(ms: Long): String = mdFmt.format(Date(ms))
|
||
fun mdLong(ms: Long): String = mdFmt2.format(Date(ms))
|
||
fun hm(ms: Long): String = hmFmt.format(Date(ms))
|
||
fun hms(ms: Long): String = hmsFmt.format(Date(ms))
|
||
|
||
/** 周五 / 周一 */
|
||
fun weekday(ms: Long): String {
|
||
val c = Calendar.getInstance()
|
||
c.timeInMillis = ms
|
||
return arrayOf("周日", "周一", "周二", "周三", "周四", "周五", "周六")[
|
||
(c.get(Calendar.DAY_OF_WEEK) - 1).coerceIn(0, 6)
|
||
]
|
||
}
|
||
|
||
/** 11月27日 周五 */
|
||
fun dateWithWeek(ms: Long): String = "${mdLong(ms)} ${weekday(ms)}"
|
||
|
||
/** 今天 / 明天 / 已逾期 / 11月30日 */
|
||
fun dueLabel(due: Long): String {
|
||
if (due <= 0) return ""
|
||
val today = todayStart()
|
||
val d = dayStart(due)
|
||
return when {
|
||
d == today -> "今天"
|
||
d == addDays(today, 1) -> "明天"
|
||
d == addDays(today, -1) -> "昨天"
|
||
d < today -> md(d)
|
||
else -> md(d)
|
||
}
|
||
}
|
||
|
||
fun weekdayShort(index: Int): String =
|
||
arrayOf("一", "二", "三", "四", "五", "六", "日")[index.coerceIn(0, 6)]
|
||
}
|