/** 中国标准时间基准经度（UTC+8） */
export const CHINA_STANDARD_MERIDIAN = 120;

/**
 * 根据出生地经度校正真太阳时。
 * 每差 1 度经度，时间差约 4 分钟。
 */
export function adjustToTrueSolarTime(
  date: Date,
  longitude: number,
  standardMeridian = CHINA_STANDARD_MERIDIAN,
): Date {
  const offsetMinutes = (longitude - standardMeridian) * 4;
  return new Date(date.getTime() + offsetMinutes * 60 * 1000);
}

export function formatDateTime(date: Date): string {
  const pad = (n: number) => n.toString().padStart(2, "0");
  return `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())} ${pad(date.getHours())}:${pad(date.getMinutes())}`;
}

export function parseDateTime(
  dateStr: string,
  timeStr: string,
): Date {
  const [year, month, day] = dateStr.split("-").map(Number);
  const [hour, minute] = timeStr.split(":").map(Number);
  return new Date(year, month - 1, day, hour, minute, 0);
}
