export function getDatabaseTime(t: string | number | Date) {
  return formatDate2String(new Date(t))
}

export function formatTimestamp(t: string) {
  return getDatabaseTime(1000 * parseInt(t))
}

// export function formatDate2String(d: Date) {
//   return d.toLocaleString().replace(/\//g, '-')
// }

// format timestamp to yyyy-mm-dd
export function formatTimestamp2Date(t: string) {
  return formatDate2String(new Date(1000 * parseInt(t)))
}
// format Date to yyyy-mm-dd 不足2位补0
export function formatDate2String(d: Date) {
  return `${d.getFullYear()}-${d.getMonth() + 1 < 10 ? '0' + (d.getMonth() + 1) : d.getMonth() + 1}-${d.getDate() < 10 ? '0' + d.getDate() : d.getDate()}`
}

// 将一个秒数转换为 00:00:00 格式
export function formatOffsetSec(s: number): string {
  const h = Math.floor(s / 3600)
  const m = Math.floor(s % 3600 / 60)
  const ss = Math.floor(s % 3600 % 60)
  return `${h < 10 ? '0' + h : h}:${m < 10 ? '0' + m : m}:${ss < 10 ? '0' + ss : ss}`
}

// 将LocaleTimeString 改为当天只显示 hh:mm:ss，之前的日期改为yyyy-mm-dd hh:mm:ss
export function formatTimeToShow(t: string | number) {
  const d = new Date(t)
  const now = new Date()
  if (d.getFullYear() === now.getFullYear() && d.getMonth() === now.getMonth() && d.getDate() === now.getDate()) {
    return d.toLocaleTimeString()
  } else {
    return d.toLocaleString().replace(/\//g, '-')
  }
}
