'use strict';

// ISO-8601 timestamp carrying the process's local UTC offset (e.g.
// "2026-07-05T08:49:04.096+07:00") instead of a "Z" UTC suffix. Sortable and
// unambiguous — the offset makes local wall-clock time correct across DST and
// machines, unlike a bare local timestamp.
function isoLocal(ms) {
  const d = ms === undefined ? new Date() : new Date(ms);
  const pad = (n, width = 2) => String(n).padStart(width, '0');
  const offsetMin = -d.getTimezoneOffset();
  const sign = offsetMin >= 0 ? '+' : '-';
  const absMin = Math.abs(offsetMin);
  return (
    `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())}` +
    `T${pad(d.getHours())}:${pad(d.getMinutes())}:${pad(d.getSeconds())}` +
    `.${pad(d.getMilliseconds(), 3)}` +
    `${sign}${pad(Math.floor(absMin / 60))}:${pad(absMin % 60)}`
  );
}

module.exports = { isoLocal };
