UNPKG

@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.

78 lines (76 loc) • 4.16 kB
import { Temporal } from "../../internal/temporal.js"; import { isOptionsArgument } from "../../internal/isObject.js"; import { fiscalYearLabel, gregorianCycleShift } from "../../internal/fiscalCalendar.js"; import { zonelessCalendarDate } from "../../internal/zonelessCalendarDate.js"; import { isValidFiscalYearLabel } from "../validate/isValidFiscalYearLabel.js"; // src/calendar/calculate/getQuarter.ts /** Months in a quarter. */ const MONTHS_PER_QUARTER = 3; /** Months in a year. */ const MONTHS_PER_YEAR = 12; /** * Return the quarter `value` falls in, as a year and a quarter number. * * - With no options these are the calendar quarters: Q1 is January to March, and `year` is * the calendar year. * - With `options`, these are fiscal quarters. The fiscal year starts on day 1 of * `fiscalYearStartMonth`, and `yearLabel` names it: `"startYear"`, `"endYear"`, or * `"majorityYear"` (the calendar year holding the most of its days). Both are required, and * there is no default. Start months 1 to 7 put most days in the start year, and 8 to 12 put * most in the end year. * - `value` must be zoneless — an ISO date or datetime, as `isValidIsoDateLike` accepts. See * `getIsoWeekDate` for why a moment is not accepted here. * - Returns null on invalid input, including `options` without both fields, a * `fiscalYearStartMonth` that is not an integer from 1 to 12, or an unrecognised `yearLabel`. * * @param value zoneless ISO 8601 date or datetime string (e.g. "2024-06-15") * @param optionsArg The fiscal year's start month and the rule that names it * @returns { year, quarter }, or null on invalid input * * @example getQuarter("2024-06-15") // { year: 2024, quarter: 2 } * @example getQuarter("2024-10-01") // { year: 2024, quarter: 4 } * @example getQuarter("2024-04-01", { fiscalYearStartMonth: 4, yearLabel: "startYear" }) // { year: 2024, quarter: 1 } * @example getQuarter("2024-03-31", { fiscalYearStartMonth: 4, yearLabel: "startYear" }) // { year: 2023, quarter: 4 } (the previous fiscal year) * @example getQuarter("2024-03-31", { fiscalYearStartMonth: 4, yearLabel: "endYear" }) // { year: 2024, quarter: 4 } * @example getQuarter("2023-10-15", { fiscalYearStartMonth: 7, yearLabel: "endYear" }) // { year: 2024, quarter: 2 } * @example getQuarter("2023-10-15", { fiscalYearStartMonth: 7, yearLabel: "majorityYear" }) // { year: 2023, quarter: 2 } (July to December is 184 of its days) * @example getQuarter("2024-10-01", { fiscalYearStartMonth: 10, yearLabel: "majorityYear" }) // { year: 2025, quarter: 1 } * @example getQuarter("2024-06-15", { fiscalYearStartMonth: 4 }) // null (no yearLabel) * @example getQuarter("2024-06-15", { fiscalYearStartMonth: 13, yearLabel: "startYear" }) // null * @example getQuarter("2024-06-15T12:00:00Z") // null (a moment, not a calendar date) * @example getQuarter("invalid") // null */ function getQuarter(value, optionsArg) { try { if (optionsArg === void 0) { const date = zonelessCalendarDate(value); if (!date) return null; return { year: date.year, quarter: Math.floor((date.month - 1) / MONTHS_PER_QUARTER) + 1 }; } if (!isOptionsArgument(optionsArg)) return null; const fiscalYearStartMonth = optionsArg.fiscalYearStartMonth; const yearLabel = optionsArg.yearLabel; if (typeof fiscalYearStartMonth !== "number" || !Number.isInteger(fiscalYearStartMonth) || fiscalYearStartMonth < 1 || fiscalYearStartMonth > MONTHS_PER_YEAR || !isValidFiscalYearLabel(yearLabel)) return null; const date = zonelessCalendarDate(value); if (!date) return null; const startYear = date.month < fiscalYearStartMonth ? date.year - 1 : date.year; const shift = gregorianCycleShift(startYear); const start = Temporal.PlainDate.from({ year: startYear - shift, month: fiscalYearStartMonth, day: 1 }); const end = start.add({ years: 1 }).subtract({ days: 1 }); const monthsIntoYear = (date.month - fiscalYearStartMonth + MONTHS_PER_YEAR) % MONTHS_PER_YEAR; return { year: fiscalYearLabel(start, end, yearLabel) + shift, quarter: Math.floor(monthsIntoYear / MONTHS_PER_QUARTER) + 1 }; } catch { return null; } } export { getQuarter };