export function display(timestamp){
    if (timestamp == null) return null;
    const date = new Date(timestamp * 1000);
    const year = date.getFullYear();
    const month = String(date.getMonth() + 1).padStart(2, '0');
    const day = String(date.getDate()).padStart(2, '0');
    const hours = String(date.getHours()).padStart(2, '0');
    const minutes = String(date.getMinutes()).padStart(2, '0');
    return `${year}-${month}-${day} ${hours}:${minutes}`;
}

function padTo2Digits(num) {
    return num.toString().padStart(2, '0');
}

export function msToTime(ms) {
  const totalSeconds = Math.floor(ms / 1000);
  const days    = Math.floor(totalSeconds / 86400);
  const hours   = Math.floor((totalSeconds % 86400) / 3600);
  const minutes = Math.floor((totalSeconds % 3600)  / 60);
  const seconds = totalSeconds % 60;

  let timestring = '';
  if (days)        timestring += days.toString().padStart(2, '0') + ":";
  if (days||hours) timestring += hours.toString().padStart(2, '0') + ":";
  return timestring + minutes.toString().padStart(2, '0') + ':' + seconds.toString().padStart(2, '0');
}

export function now(){
    return Math.floor(Date.now()/1000);
}

export function to_secs(datetime){
    return Math.floor(new Date(datetime).getTime()/1000);
}