
import dayjs from 'dayjs'

/**
 * 获取近七天的开始和结束时间
 * @param format 日期格式，默认 'YYYY-MM-DD HH:mm:ss'
 *   YYYY - 年, MM - 月, DD - 日, HH - 时, mm - 分, ss - 秒
 * @returns { start: string, end: string }
 */
export const getRecentSevenDays = (format = 'YYYY-MM-DD HH:mm:ss'):{ start: string, end: string } => {
  const now = new Date()
  const sevenDaysAgo = new Date(now)
  sevenDaysAgo.setDate(now.getDate() - 7)

  const formatDateTime = (date: Date) => {
    const tokens: Record<string, string> = {
      YYYY: String(date.getFullYear()),
      MM: String(date.getMonth() + 1).padStart(2, '0'),
      DD: String(date.getDate()).padStart(2, '0'),
      HH: String(date.getHours()).padStart(2, '0'),
      mm: String(date.getMinutes()).padStart(2, '0'),
      ss: String(date.getSeconds()).padStart(2, '0'),
    }
    let result = format
    for (const [token, value] of Object.entries(tokens)) {
      result = result.replace(token, value)
    }
    return result
  }

  return {
    start: formatDateTime(sevenDaysAgo),
    end: formatDateTime(now),
  }
}

 /**
 * 将 UTC 时间字符串 (YYYY-MM-DD HH:mm:ss) 转换为 UTC+8 时间字符串
 * @param {string} utcStr - UTC 时间字符串，格式：YYYY-MM-DD HH:mm:ss
 * @returns {string} UTC+8 时间字符串，格式：YYYY-MM-DD HH:mm:ss
 */
 export function utcToUtc8(utcStr: string): string {
  // // 将字符串解析为 UTC 时间
  // const utcDate = new Date(utcStr.replace(' ', 'T') + 'Z')

  // // 转换为 UTC+8（东八区）
  // const utc8Date = new Date(utcDate.getTime() + 8 * 60 * 60 * 1000)

  // // 格式化为 YYYY-MM-DD HH:mm:ss
  // const pad = (n) => String(n).padStart(2, '0')

  // const year = utc8Date.getUTCFullYear()
  // const month = pad(utc8Date.getUTCMonth() + 1)
  // const day = pad(utc8Date.getUTCDate())
  // const hour = pad(utc8Date.getUTCHours())
  // const minute = pad(utc8Date.getUTCMinutes())
  // const second = pad(utc8Date.getUTCSeconds())

  return dayjs(utcStr).format('YYYY-MM-DD HH:mm:ss')
}

