import { DateTimePatternFieldType, MetaZoneType } from '@phensley/cldr-types'; import { TimePeriod, TimePeriodField } from './interval'; import { ZoneInfo } from './timezone'; import { CalendarDateFields, CalendarType } from './types'; export type CalendarDateModFields = keyof Pick; /** * @public */ export interface TimeStringOptions { /** * Include timezone identifier. */ includeZoneId?: boolean; /** * Include timezone offset. */ includeZoneOffset?: boolean; /** * Only output milliseconds if non-zero. */ optionalMilliseconds?: boolean; } export interface DateDiffOptions { /** * Calculate weeks. */ includeWeeks?: boolean; /** * Rollup result into the given fields. */ fields?: TimePeriodField[]; /** * Rollup fractional fields. */ rollupFractional?: boolean; } /** * @internal */ export type CalendarFromUnixEpoch = (epoch: number, zoneId: string, firstDay: number, minDays: number) => T; /** * Base class for dates in supported calendars. * * @public */ export declare abstract class CalendarDate { protected readonly _type: CalendarType; protected readonly _firstDay: number; protected readonly _minDays: number; protected static _gregorian: (d: CalendarDate, utc: boolean, firstDate: number, minDays: number) => CalendarDate; protected _fields: number[]; protected _zoneInfo: ZoneInfo; /** * Minimal fields required to construct any calendar date. */ protected constructor(_type: CalendarType, _firstDay: number, _minDays: number); /** * Calendar type for this date, e.g. 'gregory' for Gregorian. * * @public */ type(): CalendarType; /** * Returns a formatted ISO-8601 string of the date in UTC. Note that this * always returns a date in the Gregorian calendar. * * @public */ toISOString(): string; /** * Returns a formatted ISO 8601 string of the date with local timezone offset. * Note that this always returns a date in the Gregorian calendar. * * @public */ toLocalISOString(): string; /** * Unix epoch with no timezone offset. * * @public */ unixEpoch(): number; /** * First day of week. * * @public */ firstDayOfWeek(): number; /** * Minimum days in the first week. * * @public */ minDaysInFirstWeek(): number; /** * Returns a floating point number representing the real Julian Day, UTC. * * @public */ julianDay(): number; /** * CLDR's modified Julian day used as the basis for all date calculations. * * @public */ modifiedJulianDay(): number; /** * Era * * @public */ era(): number; /** * Extended year. * * @public */ extendedYear(): number; /** * Year. * * @public */ year(): number; /** * Related year. * * @public */ relatedYear(): number; /** * Year of week of year. * * @public */ yearOfWeekOfYear(): number; /** * Week of year. * * @public */ weekOfYear(): number; /** * Year of week of year ISO. * * @public */ yearOfWeekOfYearISO(): number; /** * Week of year ISO. * * @public */ weekOfYearISO(): number; /** * Ordinal month, one-based, e.g. Gregorian JANUARY = 1. */ month(): number; /** * Returns the week of the month computed using the locale's 'first day * of week' and 'minimal days in first week' where applicable. * * For example, for the United States, weeks start on Sunday. * Saturday 9/1/2018 would be in week 1, and Sunday 9/2/2018 would * begin week 2. * * September * Su Mo Tu We Th Fr Sa * 1 * 2 3 4 5 6 7 8 * 9 10 11 12 13 14 15 * 16 17 18 19 20 21 22 * 23 24 25 26 27 28 29 * 30 */ weekOfMonth(): number; dayOfYear(): number; /** * Day of the week. 1 = SUNDAY, 2 = MONDAY, ..., 7 = SATURDAY */ dayOfWeek(): number; /** * Ordinal day of the week. 1 if this is the 1st day of the week, * 2 if the 2nd, etc. Depends on the local starting day of the week. */ ordinalDayOfWeek(): number; /** * Ordinal number indicating the day of the week in the current month. * The result of this method can be used to format messages like * "2nd Sunday in August". */ dayOfWeekInMonth(): number; /** * Day of month. * * @public */ dayOfMonth(): number; /** * Is AM. * * @public */ isAM(): boolean; /** * Indicates the hour of the morning or afternoon, used for the 12-hour * clock (0 - 11). Noon and midnight are 0, not 12. */ hour(): number; /** * Indicates the hour of the day, used for the 24-hour clock (0 - 23). * Noon is 12 and midnight is 0. */ hourOfDay(): number; /** * Indicates the minute of the hour (0 - 59). */ minute(): number; /** * Indicates the second of the minute (0 - 59). */ second(): number; /** * Milliseconds. * * @public */ milliseconds(): number; /** * Milliseconds in day. * * @public */ millisecondsInDay(): number; /** * Metazone (CLDR) identifier. * * @public */ metaZoneId(): MetaZoneType; /** * Timezone identifier. * * @public */ timeZoneId(): string; /** * Timezone stable identifier (CLDR) * * @public */ timeZoneStableId(): string; /** * Timezone offset * * @public */ timeZoneOffset(): number; /** * Is leap year. * * @public */ isLeapYear(): boolean; /** * Is daylight savings time. * * @public */ isDaylightSavings(): boolean; /** * Computes the field of visual difference between the two dates. * Note: This assumes the dates are of the same type and have the same * timezone offset. * * @public */ fieldOfVisualDifference(other: CalendarDate): DateTimePatternFieldType | undefined; /** * Compare two dates a and b, returning: * * ``` * a < b -> -1 * a = b -> 0 * a > b -> 1 * ``` * * @public */ compare(other: CalendarDate): number; /** * Calculate the relative time between two dates. If a field is specified * the time will be calculated in terms of that single field. Otherwise * the field will be auto-selected. * * @public */ relativeTime(other: CalendarDate, field?: TimePeriodField, options?: DateDiffOptions): [TimePeriodField, number]; /** * Calculate the period of time (years, months, days, ..) since the given date. * * In a normal call, "other is less-than-or-equal-to this". * If "this is less-than other" we invert the sign of the result. * * For example: * * "2024-03-03".since("2024-01-30") == "1 month 4 days" * "2024-01-30".since("2024-03-03") == "-1 month -3 days" * * The durations are not symmetrical due to calendar math, e.g. end-of-month alignment * with months of different lengths. * * @public */ since(other: CalendarDate, options?: DateDiffOptions): TimePeriod; /** * Calculate the period of time (years, months, days, ..) until the given date. * * In a normal call, "this is less-than-or-equal-to other". * If "other is less-than this" we invert the sign of the result. * * For example: * * "2024-01-30".until("2024-03-30") == "1 month 3 days" * "2024-03-03".until("2024-01-30") == "-1 month -4 days" * * The duration are not symmetrical due to calendar math, e.g. end-of-month alignment * with months of different lengths. * * @public */ until(other: CalendarDate, options?: DateDiffOptions): TimePeriod; /** * Calculate the time period between two dates. Note this returns the * absolute value of the difference. * * @public */ difference(other: CalendarDate, options?: DateDiffOptions): TimePeriod; /** * Calculate the time period between two dates. If 'other' is before this date, * the time period fields will be negative. * * @public */ differenceSigned(other: CalendarDate, options?: DateDiffOptions): TimePeriod; /** * Return all of the date and time field values. * * @public */ fields(): CalendarDateFields; /** * Return a JavaScript Date object with the same date and time. * * @public */ asJSDate(): Date; /** * Return a string containing the date. */ toDateString(): string; /** * Return a string containing the time, with optional timezone. */ toTimeString(options?: TimeStringOptions): string; /** * Return a compact string containing just date and time, with optional timezone. */ toDateTimeString(options?: TimeStringOptions): string; /** * Start of date, e.g. `date.startOf('day')` * * @public */ startOf(field: CalendarDateModFields): CalendarDate; /** * End of date, e.g. `date.endOf('day')` * * @public */ endOf(field: CalendarDateModFields): CalendarDate; /** * Set one or more fields on this date explicitly, and return a new date. * * Note: when setting the 'year' field you must use the "extended year". * For example, the extended year 0 is 1 B.C in the Gregorian calendar. * * @public */ abstract set(fields: Partial): CalendarDate; /** * Add the fields to this date, returning a new date. * * @public */ abstract add(fields: Partial): CalendarDate; /** * Subtract the fields from this date, returning a new date. * * @public */ abstract subtract(fields: Partial): CalendarDate; /** * Change the timezone for this date, returning a new date. * * @public */ abstract withZone(zoneId: string): CalendarDate; protected abstract _new(): CalendarDate; protected abstract monthCount(): number; protected abstract daysInMonth(y: number, m: number): number; protected abstract daysInYear(y: number): number; protected abstract monthStart(eyear: number, month: number, useMonth: boolean): number; protected _toISOString(d: CalendarDate, utc: boolean): string; /** * Rollup just the time fields into number of milliseconds. This is internal * and assumes all time fields are defined. */ protected _timeToMs(f: Partial): number; protected _negatePeriod(fields: Partial): TimePeriod; protected initFromUnixEpoch(ms: number, zoneId: string): void; protected initFromJD(jd: number, msDay: number, zoneId: string): void; protected _toString(type: string): string; /** * Compute WEEK_OF_YEAR and YEAR_WOY on demand. */ protected computeWeekFields(): void; protected _computeWeekFields(woyfield: number, ywoyfield: number, firstDay: number, minDays: number, dow: number, _dom: number, doy: number): void; protected yearLength(y: number): number; protected weekNumber(firstDay: number, minDays: number, desiredDay: number, dayOfPeriod: number, dayOfWeek: number): number; } /** * Compare two dates a and b, returning: * * ``` * a < b -> -1 * a = b -> 0 * a > b -> 1 * ``` */ export declare const compare: (a: CalendarDate, b: CalendarDate) => number;