// 时间转换
function timeForMat (count) {
  let result = []
  for (let i = count; i > 0; i--) {
    const nowTime = new Date()
    nowTime.setTime(nowTime.getTime() - (24 * 60 * 60 * 1000 * i))
    const Y = nowTime.getFullYear()
    const M = (nowTime.getMonth() + 1).toString().padStart(2, '0')
    const D = (nowTime.getDate()).toString().padStart(2, '0')
    result = [...result, Y + '-' + M + '-' + D]
  }
  return result
}

function padStart (val) {
  return val * 1 < 10 ? `0${val}` : val
}

/**
 * 时间戳解析
 * @param ts
 * @param type
 * @returns {string}
 */
function timeFormat (ts, type) {
  let time = ts.toString().length >= 13 ? new Date(ts * 1) : new Date(ts * 1 * 1000)
  let Y = time.getFullYear()
  let M = time.getMonth() + 1
  let D = time.getDate()
  let h = time.getHours()
  let m = time.getMinutes()
  let s = time.getSeconds()

  if (['hh:mm'].includes(type)) {
    return `${padStart(h)}:${padStart(m)}`
  }
  if (['YY-MM-DD hh:mm:ss'].includes(type)) {
    return `${Y}-${padStart(M)}-${padStart(D)} ${padStart(h)}:${padStart(m)}:${padStart(s)}`
  }
  if (['hh:mm:ss'].includes(type)) {
    return `${padStart(h)}:${padStart(m)}:${padStart(s)}`
  }
  if (['YY年MM月DD日'].includes(type)) {
    return `${Y}年${padStart(M)}月${padStart(D)}日`
  }
  if (['MM-DD'].includes(type)) {
    return `${padStart(M)}-${padStart(D)}`
  }
}

function cutDownTime (ts = 0, type) {
  if (ts < 0) {
    return `00:00:00`
  }
  ts = parseInt(ts)
  let D = Math.floor(ts / 24 / 60 / 60)

  // 小时位
  let h = Math.floor((ts - D * 24 * 60 * 60) / 3600)
  // 分钟位
  let m = Math.floor((ts - D * 24 * 3600 - h * 3600) / 60)
  // 秒位
  let s = ts - D * 24 * 3600 - h * 3600 - m * 60
  if (['hh:mm:ss'].includes(type)) {
    return `${padStart(h)}:${padStart(m)}:${padStart(s)}`
  }
}

export {
  timeForMat,
  timeFormat,
  cutDownTime
}
