import dayjs, { Dayjs } from 'dayjs';

export function to2digitNum(num: number) {
  return num < 10 ? `0${num}` : num;
}


/**
 * 当天秒数格式化
 * @param secs 当天的秒数
 * @returns
 */
export function todaySecFormat(secs: number) {
  const sec = secs % 60;
  const hour = Math.floor(secs / 3600);
  const min = Math.floor(secs % 3600 / 60);
  return `${to2digitNum(hour)}:${to2digitNum(min)}:${to2digitNum(sec)}`;
}

export function toDisplayNum(num: number) {
  if (num >= 10000) {
    return `${(num / 10000).toFixed(1)}W`;
  }
  if (num >= 1000) {
    return `${(num / 1000).toFixed(1)}K`;
  }
  return num;
}

/**
 * 获取当天的秒数
 * @param date 日期
 * @returns
 */
export function getTodaySec(date: Dayjs) {
  return date.second() + (date.minute() * 60) + (date.hour() * 3600);
}

/**
 * 当天秒数转换成dayjs
 * @param secs 当天的秒数
 * @returns
 */
export function todaySecToDayjs(secs: number) {
  const sec = secs % 60;
  const hour = Math.floor(secs / 3600);
  const min = Math.floor(secs % 3600 / 60);
  const res = dayjs();
  return res.hour(hour).minute(min)
    .second(sec);
}
