import dayjs, { Dayjs } from "dayjs";

export function timeToDate(time: number | undefined): Dayjs | undefined {
  if (!time) return undefined;
  return dayjs(new Date(time * 1000));
}

export function dateToTime(date: Dayjs | undefined): number | undefined {
  if (!date) return undefined;
  return Math.floor(date.toDate().getTime() / 1000);
}

/**
 * Get UNIX time
 *
 * @returns Number of seconds since Epoch
 */
export function time(): number {
  return Math.floor(new Date().getTime() / 1000);
}

/**
 * Format date from unix time (secondes since Epoch)
 */
export function fmtDateFromTime(time: number): string {
  const d = new Date(time * 1000);
  return `${d.getDate().toString().padStart(2, "0")}/${(d.getMonth() + 1)
    .toString()
    .padStart(2, "0")}/${d.getFullYear()}`;
}
