@northguild/gmt
Version:
Date and time library for JavaScript and TypeScript, built on TC39 Temporal: time zones, DST, business days, ISO 8601 strings in and out. Never throws.
25 lines (24 loc) • 1.06 kB
JavaScript
// src/internal/twoDigitYear.ts
/**
* Resolve a two-digit year inside a caller-chosen hundred-year window: the one year in
* `[windowStart, windowStart + 99]` whose last two digits are `yy`.
*
* Pure arithmetic: the function holds no window of its own and reads no clock. The window comes
* from the caller's `TwoDigitYearOptions.yearWindow`, resolved by `readYearWindowStart`
* (`internal/yearWindowStart.ts`); no standard says which century a two-digit year belongs to,
* so the library never supplies one.
*
* @param yy the two-digit year, 0–99, as the grammar read it
* @param windowStart the first year of the hundred-year window
* @returns the four-digit year in the window ending in `yy`
*
* @example twoDigitYear(24, 2000) // 2024
* @example twoDigitYear(99, 1950) // 1999
* @example twoDigitYear(49, 1950) // 2049
* @example twoDigitYear(0, 1976) // 2000
*/
function twoDigitYear(yy, windowStart) {
const candidate = Math.floor(windowStart / 100) * 100 + yy;
return candidate < windowStart ? candidate + 100 : candidate;
}
export { twoDigitYear };