@dereekb/date 13.0.7 → 13.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.cjs.js +3526 -993
- package/index.esm.js +3515 -983
- package/package.json +5 -6
- package/src/lib/date/date.calendar.d.ts +63 -9
- package/src/lib/date/date.cell.d.ts +203 -104
- package/src/lib/date/date.cell.factory.d.ts +319 -86
- package/src/lib/date/date.cell.filter.d.ts +104 -2
- package/src/lib/date/date.cell.index.d.ts +202 -92
- package/src/lib/date/date.cell.schedule.d.ts +285 -102
- package/src/lib/date/date.cell.schedule.day.d.ts +13 -3
- package/src/lib/date/date.cell.validator.d.ts +6 -8
- package/src/lib/date/date.cell.week.d.ts +24 -3
- package/src/lib/date/date.d.ts +481 -54
- package/src/lib/date/date.day.d.ts +139 -49
- package/src/lib/date/date.duration.d.ts +49 -11
- package/src/lib/date/date.format.d.ts +355 -36
- package/src/lib/date/date.hashset.d.ts +11 -0
- package/src/lib/date/date.logical.d.ts +61 -3
- package/src/lib/date/date.range.d.ts +355 -77
- package/src/lib/date/date.range.string.d.ts +39 -0
- package/src/lib/date/date.range.timezone.d.ts +18 -6
- package/src/lib/date/date.round.d.ts +46 -1
- package/src/lib/date/date.rxjs.d.ts +29 -7
- package/src/lib/date/date.sort.d.ts +36 -9
- package/src/lib/date/date.time.d.ts +197 -26
- package/src/lib/date/date.time.limit.d.ts +67 -4
- package/src/lib/date/date.time.minute.d.ts +269 -30
- package/src/lib/date/date.timezone.d.ts +286 -70
- package/src/lib/date/date.unix.d.ts +3 -0
- package/src/lib/date/date.week.d.ts +115 -51
- package/src/lib/query/query.builder.d.ts +91 -0
- package/src/lib/query/query.builder.mongo.d.ts +50 -0
- package/src/lib/query/query.filter.d.ts +29 -2
- package/src/lib/query/query.request.d.ts +16 -0
- package/src/lib/rrule/date.recurrence.d.ts +66 -22
- package/src/lib/rrule/date.rrule.d.ts +131 -8
- package/src/lib/rrule/date.rrule.extension.d.ts +50 -9
- package/src/lib/rrule/date.rrule.parse.d.ts +85 -2
- package/src/lib/timezone/timezone.d.ts +102 -2
- package/src/lib/timezone/timezone.validator.d.ts +9 -4
|
@@ -1,16 +1,55 @@
|
|
|
1
1
|
import { type ISO8601DayString, type DateOrDayString } from '@dereekb/util';
|
|
2
2
|
import { type DateRange } from './date.range';
|
|
3
|
+
/**
|
|
4
|
+
* A start boundary expressed as an ISO 8601 day string (e.g. "2024-01-15").
|
|
5
|
+
*/
|
|
3
6
|
export interface ISO8601DayStringStart {
|
|
4
7
|
start: ISO8601DayString;
|
|
5
8
|
}
|
|
9
|
+
/**
|
|
10
|
+
* A date range expressed as ISO 8601 day strings for both start and end.
|
|
11
|
+
*/
|
|
6
12
|
export interface ISO8601DayStringRange extends ISO8601DayStringStart {
|
|
7
13
|
end: ISO8601DayString;
|
|
8
14
|
}
|
|
15
|
+
/**
|
|
16
|
+
* A start boundary that accepts either a Date object or an ISO 8601 day string.
|
|
17
|
+
*/
|
|
9
18
|
export interface DateOrDayStringStart {
|
|
10
19
|
start: DateOrDayString;
|
|
11
20
|
}
|
|
21
|
+
/**
|
|
22
|
+
* A date range where start and end can each be a Date or an ISO 8601 day string.
|
|
23
|
+
*/
|
|
12
24
|
export interface DateOrDayStringRange extends DateOrDayStringStart {
|
|
13
25
|
end: DateOrDayString;
|
|
14
26
|
}
|
|
27
|
+
/**
|
|
28
|
+
* Converts a {@link DateOrDayStringRange} to a {@link DateRange} by parsing any string values to the start of their respective days.
|
|
29
|
+
*
|
|
30
|
+
* @param range - the range with Date or string values
|
|
31
|
+
* @returns a DateRange with concrete Date objects
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* ```ts
|
|
35
|
+
* const range = dateOrDayStringRangeToDateRange({ start: '2024-01-01', end: '2024-01-31' });
|
|
36
|
+
* // range.start and range.end are Date objects at start of day
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
15
39
|
export declare function dateOrDayStringRangeToDateRange(range: DateOrDayStringRange): DateRange;
|
|
40
|
+
/**
|
|
41
|
+
* Converts a {@link DateOrDayStringRange} to an {@link ISO8601DayStringRange} by formatting any Date values as ISO 8601 day strings using the system timezone.
|
|
42
|
+
*
|
|
43
|
+
* @param range - the range with Date or string values
|
|
44
|
+
* @returns a range with both start and end as ISO 8601 day strings
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* ```ts
|
|
48
|
+
* const stringRange = dateOrDayStringRangeToISO8601DayStringRange({
|
|
49
|
+
* start: new Date('2024-01-01T10:00:00Z'),
|
|
50
|
+
* end: '2024-01-31'
|
|
51
|
+
* });
|
|
52
|
+
* // stringRange.start === '2024-01-01', stringRange.end === '2024-01-31'
|
|
53
|
+
* ```
|
|
54
|
+
*/
|
|
16
55
|
export declare function dateOrDayStringRangeToISO8601DayStringRange(range: DateOrDayStringRange): ISO8601DayStringRange;
|
|
@@ -9,16 +9,28 @@ export type FitDateRangeToDayPeriodFunction = (<T extends DateRange>(dateRange:
|
|
|
9
9
|
readonly _timezoneInstance: DateTimezoneUtcNormalInstance;
|
|
10
10
|
};
|
|
11
11
|
/**
|
|
12
|
-
* Creates a FitDateRangeToDayPeriodFunction
|
|
12
|
+
* Creates a {@link FitDateRangeToDayPeriodFunction} that collapses a multi-day date range into a single-day period within the given timezone.
|
|
13
13
|
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
14
|
+
* The function first normalizes the range into the target timezone, fits it to a 24-hour period, then converts back.
|
|
15
|
+
*
|
|
16
|
+
* @param timezone - the timezone to use for the day period calculation
|
|
17
|
+
* @returns a function that fits any date range to a single day period
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* ```ts
|
|
21
|
+
* const fit = fitDateRangeToDayPeriodFunction('America/Chicago');
|
|
22
|
+
* const range = { start: new Date('2024-01-01T10:00:00Z'), end: new Date('2024-01-03T14:00:00Z') };
|
|
23
|
+
* const fitted = fit(range);
|
|
24
|
+
* // fitted spans from 10:00 to 14:00 on the same day (4 hours)
|
|
25
|
+
* ```
|
|
16
26
|
*/
|
|
17
27
|
export declare function fitDateRangeToDayPeriodFunction(timezone: DateTimezoneUtcNormalFunctionInput): FitDateRangeToDayPeriodFunction;
|
|
18
28
|
/**
|
|
19
|
-
* Fits the DateRange to a single "day" period from the start time.
|
|
29
|
+
* Fits the DateRange to a single "day" period from the start time within the given timezone.
|
|
30
|
+
*
|
|
31
|
+
* Convenience wrapper around {@link fitDateRangeToDayPeriodFunction}.
|
|
20
32
|
*
|
|
21
|
-
* @param dateRange
|
|
22
|
-
* @param timezone
|
|
33
|
+
* @param dateRange - the range to fit
|
|
34
|
+
* @param timezone - the timezone for day boundary calculation
|
|
23
35
|
*/
|
|
24
36
|
export declare function fitDateRangeToDayPeriod<T extends DateRange = DateRange>(dateRange: T, timezone: DateTimezoneUtcNormalFunctionInput): T;
|
|
@@ -1,14 +1,59 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Options for rounding a date's time components down.
|
|
3
|
+
*/
|
|
1
4
|
export interface RoundTimeDown {
|
|
2
5
|
roundDownToDay?: boolean;
|
|
3
6
|
roundDownToMinute?: boolean;
|
|
4
7
|
}
|
|
8
|
+
/**
|
|
9
|
+
* Options for rounding a date to configured minute steps, then optionally rounding time components down.
|
|
10
|
+
*/
|
|
5
11
|
export interface StepRoundDateTimeDown extends RoundTimeDown {
|
|
6
12
|
step?: number;
|
|
7
13
|
roundToSteps?: boolean;
|
|
8
14
|
}
|
|
15
|
+
/**
|
|
16
|
+
* Rounds the date's minutes to the nearest step, then rounds remaining time components down.
|
|
17
|
+
*
|
|
18
|
+
* @param date - the date to round
|
|
19
|
+
* @param round - step and rounding configuration
|
|
20
|
+
* @returns the rounded date
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* ```ts
|
|
24
|
+
* // Round to 15-minute steps and clear seconds
|
|
25
|
+
* const rounded = roundDateTimeDownToSteps(new Date('2024-01-01T10:07:30Z'), { step: 15 });
|
|
26
|
+
* // rounded minutes will be 15 (rounded up from 7), seconds cleared
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
9
29
|
export declare function roundDateTimeDownToSteps(date: Date, round: StepRoundDateTimeDown): Date;
|
|
30
|
+
/**
|
|
31
|
+
* Clears time components of the date based on rounding options (e.g. seconds/milliseconds, or hours/minutes for day rounding).
|
|
32
|
+
*
|
|
33
|
+
* @param date - the date to round
|
|
34
|
+
* @param round - which components to clear
|
|
35
|
+
* @returns the rounded date
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
* ```ts
|
|
39
|
+
* const result = roundDateTimeDown(new Date('2024-01-01T10:07:30.500Z'), { roundDownToMinute: true });
|
|
40
|
+
* // seconds and milliseconds are set to 0
|
|
41
|
+
* ```
|
|
42
|
+
*/
|
|
10
43
|
export declare function roundDateTimeDown(date: Date, round: RoundTimeDown): Date;
|
|
11
44
|
/**
|
|
12
|
-
* Rounds the
|
|
45
|
+
* Rounds the date's minutes up to the nearest multiple of the given step.
|
|
46
|
+
*
|
|
47
|
+
* If the step is 1 or less, the date is returned unchanged. If rounding pushes minutes to 60, the hour is incremented.
|
|
48
|
+
*
|
|
49
|
+
* @param date - the date to round
|
|
50
|
+
* @param step - the minute step to round to
|
|
51
|
+
* @returns the date with minutes rounded to the nearest step
|
|
52
|
+
*
|
|
53
|
+
* @example
|
|
54
|
+
* ```ts
|
|
55
|
+
* const result = roundToMinuteSteps(new Date('2024-01-01T10:07:00Z'), 15);
|
|
56
|
+
* // result minutes will be 15 (next step above 7)
|
|
57
|
+
* ```
|
|
13
58
|
*/
|
|
14
59
|
export declare function roundToMinuteSteps(date: Date, step: number): Date;
|
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
import { type FactoryWithRequiredInput, type IndexNumber, type Maybe, type Milliseconds } from '@dereekb/util';
|
|
2
2
|
import { type Observable, type SchedulerLike } from 'rxjs';
|
|
3
3
|
import { type LogicalDateStringCode } from './date.logical';
|
|
4
|
+
/**
|
|
5
|
+
* Configuration for creating a periodic date-emitting Observable.
|
|
6
|
+
*/
|
|
4
7
|
export interface DateIntervalConfig {
|
|
5
8
|
/**
|
|
6
9
|
* How often to emit the date.
|
|
@@ -9,15 +12,15 @@ export interface DateIntervalConfig {
|
|
|
9
12
|
*/
|
|
10
13
|
readonly period?: Maybe<Milliseconds>;
|
|
11
14
|
/**
|
|
12
|
-
* Emits the given logical each interval.
|
|
15
|
+
* Emits the given logical date each interval (e.g. 'now', 'today_start').
|
|
13
16
|
*/
|
|
14
17
|
readonly logicalDate?: Maybe<LogicalDateStringCode>;
|
|
15
18
|
/**
|
|
16
|
-
*
|
|
19
|
+
* Custom factory to use for producing each date value.
|
|
17
20
|
*/
|
|
18
21
|
readonly factory?: FactoryWithRequiredInput<Date, IndexNumber>;
|
|
19
22
|
/**
|
|
20
|
-
* Whether
|
|
23
|
+
* Whether to emit every interval tick regardless of whether the date changed. False by default.
|
|
21
24
|
*/
|
|
22
25
|
readonly emitAll?: boolean;
|
|
23
26
|
/**
|
|
@@ -26,13 +29,32 @@ export interface DateIntervalConfig {
|
|
|
26
29
|
readonly scheduler?: SchedulerLike | undefined;
|
|
27
30
|
}
|
|
28
31
|
/**
|
|
29
|
-
* Creates an Observable that emits a
|
|
32
|
+
* Creates an Observable that emits a Date at regular intervals based on the given configuration.
|
|
33
|
+
*
|
|
34
|
+
* By default emits the current time every second and deduplicates consecutive equal dates.
|
|
35
|
+
*
|
|
36
|
+
* @param config - interval and date generation configuration
|
|
37
|
+
* @returns an Observable that emits Date values
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* ```ts
|
|
41
|
+
* // Emit start-of-today every 5 seconds
|
|
42
|
+
* const today$ = dateInterval({ logicalDate: 'today_start', period: 5000 });
|
|
43
|
+
* ```
|
|
30
44
|
*/
|
|
31
45
|
export declare function dateInterval(config: DateIntervalConfig): Observable<Date>;
|
|
32
46
|
/**
|
|
33
|
-
* Convenience function for dateInterval that
|
|
47
|
+
* Convenience function for {@link dateInterval} that emits the current time at each interval tick.
|
|
48
|
+
*
|
|
49
|
+
* Unlike the default dateInterval, this always emits (no deduplication) since each "now" is unique.
|
|
50
|
+
*
|
|
51
|
+
* @param period - optional emission interval in milliseconds (defaults to 1 second)
|
|
52
|
+
* @returns an Observable that emits the current Date
|
|
34
53
|
*
|
|
35
|
-
* @
|
|
36
|
-
*
|
|
54
|
+
* @example
|
|
55
|
+
* ```ts
|
|
56
|
+
* const now$ = nowInterval(1000);
|
|
57
|
+
* now$.subscribe((date) => console.log('Current time:', date));
|
|
58
|
+
* ```
|
|
37
59
|
*/
|
|
38
60
|
export declare function nowInterval(period?: Maybe<Milliseconds>): Observable<Date>;
|
|
@@ -1,27 +1,54 @@
|
|
|
1
1
|
import { type SortingOrder, type SortCompareFunction } from '@dereekb/util';
|
|
2
2
|
import { type ReadDateFunction, type ReadISO8601DateStringUTCFullFunction } from './date';
|
|
3
3
|
/**
|
|
4
|
-
* SortCompareFunction by Date
|
|
4
|
+
* A {@link SortCompareFunction} that sorts items by a Date value.
|
|
5
5
|
*/
|
|
6
6
|
export type SortByDateFunction<T> = SortCompareFunction<T>;
|
|
7
7
|
/**
|
|
8
|
-
* Creates a
|
|
8
|
+
* Creates a sort comparison function that orders items in ascending date order using the provided reader.
|
|
9
|
+
*
|
|
10
|
+
* @param readDateFn - extracts a Date from each item
|
|
11
|
+
* @returns a comparison function for use with Array.sort()
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```ts
|
|
15
|
+
* const events = [{ at: new Date('2024-03-01') }, { at: new Date('2024-01-01') }];
|
|
16
|
+
* events.sort(sortByDateFunction((e) => e.at));
|
|
17
|
+
* // events[0].at === '2024-01-01'
|
|
18
|
+
* ```
|
|
9
19
|
*/
|
|
10
20
|
export declare function sortByDateFunction<T>(readDateFn: ReadDateFunction<T>): SortByDateFunction<T>;
|
|
11
21
|
/**
|
|
12
|
-
* SortCompareFunction by
|
|
22
|
+
* A {@link SortCompareFunction} that sorts items by an ISO 8601 date string.
|
|
13
23
|
*/
|
|
14
24
|
export type SortByISO8601DateStringFunction<T> = SortCompareFunction<T>;
|
|
15
25
|
/**
|
|
16
|
-
* Creates a
|
|
26
|
+
* Creates a sort comparison function that orders items in ascending order by lexicographic comparison of ISO 8601 date strings.
|
|
27
|
+
*
|
|
28
|
+
* @param readDateFn - extracts an ISO 8601 date string from each item
|
|
29
|
+
* @returns a comparison function for use with Array.sort()
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* ```ts
|
|
33
|
+
* const items = [{ d: '2024-03-01T00:00:00Z' }, { d: '2024-01-01T00:00:00Z' }];
|
|
34
|
+
* items.sort(sortByISO8601DateStringFunction((x) => x.d));
|
|
35
|
+
* // items[0].d === '2024-01-01T00:00:00Z'
|
|
36
|
+
* ```
|
|
17
37
|
*/
|
|
18
38
|
export declare function sortByISO8601DateStringFunction<T>(readDateFn: ReadISO8601DateStringUTCFullFunction<T>): SortByISO8601DateStringFunction<T>;
|
|
19
39
|
/**
|
|
20
|
-
*
|
|
40
|
+
* Returns a sorted copy of the input array, ordered by ISO 8601 date strings extracted from each item.
|
|
41
|
+
*
|
|
42
|
+
* @param values - the items to sort
|
|
43
|
+
* @param readDate - extracts an ISO 8601 date string from each item
|
|
44
|
+
* @param order - optional sorting order ('asc' or 'desc', defaults to ascending)
|
|
45
|
+
* @returns a new sorted array (does not mutate the original)
|
|
21
46
|
*
|
|
22
|
-
* @
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
-
*
|
|
47
|
+
* @example
|
|
48
|
+
* ```ts
|
|
49
|
+
* const items = [{ d: '2024-03-01T00:00:00Z' }, { d: '2024-01-01T00:00:00Z' }];
|
|
50
|
+
* const sorted = sortByISO8601DateStrings(items, (x) => x.d);
|
|
51
|
+
* // sorted[0].d === '2024-01-01T00:00:00Z'
|
|
52
|
+
* ```
|
|
26
53
|
*/
|
|
27
54
|
export declare function sortByISO8601DateStrings<T>(values: T[], readDate: ReadISO8601DateStringUTCFullFunction<T>, order?: SortingOrder): T[];
|
|
@@ -1,43 +1,55 @@
|
|
|
1
1
|
import { type LogicalDateStringCode, type Maybe, type ReadableTimeString, TimeAM, type TimezoneString } from '@dereekb/util';
|
|
2
2
|
import { type LimitDateTimeConfig, LimitDateTimeInstance } from './date.time.limit';
|
|
3
3
|
import { type DateTimezoneConversionConfig, DateTimezoneUtcNormalInstance } from './date.timezone';
|
|
4
|
+
/**
|
|
5
|
+
* Result of parsing a time string, containing both the raw UTC and timezone-adjusted dates
|
|
6
|
+
* along with computed time-of-day information.
|
|
7
|
+
*/
|
|
4
8
|
export interface ParsedTimeString {
|
|
5
9
|
/**
|
|
6
|
-
*
|
|
10
|
+
* The parsed date normalized to UTC, preserving the wall-clock time.
|
|
11
|
+
* For example, parsing "1:00PM" yields 1:00PM UTC regardless of the source timezone.
|
|
7
12
|
*/
|
|
8
13
|
utc: Date;
|
|
9
14
|
/**
|
|
10
|
-
*
|
|
15
|
+
* The timezone-adjusted date representing the actual moment in time the input refers to.
|
|
11
16
|
*/
|
|
12
17
|
date: Date;
|
|
13
18
|
/**
|
|
14
|
-
*
|
|
19
|
+
* Number of minutes elapsed since midnight, used for time-of-day comparisons.
|
|
15
20
|
*/
|
|
16
21
|
minutesSinceStartOfDay: number;
|
|
17
22
|
/**
|
|
18
|
-
* AM or PM
|
|
23
|
+
* Whether the parsed time falls in the AM or PM half of the day.
|
|
19
24
|
*/
|
|
20
25
|
am: TimeAM;
|
|
21
26
|
}
|
|
27
|
+
/**
|
|
28
|
+
* Configuration for parsing a time string relative to a specific date and timezone.
|
|
29
|
+
*/
|
|
22
30
|
export interface ParseTimeString extends DateTimezoneConversionConfig {
|
|
23
31
|
/**
|
|
24
|
-
*
|
|
32
|
+
* The reference date used as the base day for the parsed time.
|
|
33
|
+
* Defaults to the current date/time if not provided.
|
|
25
34
|
*/
|
|
26
35
|
readonly date?: Date;
|
|
27
36
|
}
|
|
37
|
+
/**
|
|
38
|
+
* Result of converting a time string to a date, containing both the raw UTC interpretation
|
|
39
|
+
* and the timezone-adjusted result.
|
|
40
|
+
*/
|
|
28
41
|
export interface DateFromTimestringResult {
|
|
29
42
|
/**
|
|
30
|
-
* The
|
|
43
|
+
* The parsed date normalized to UTC wall-clock time. For instance, "1:00PM" in any timezone
|
|
44
|
+
* yields 1:00PM UTC, useful for timezone-independent time-of-day comparisons.
|
|
31
45
|
*/
|
|
32
46
|
raw?: Maybe<Date>;
|
|
33
47
|
/**
|
|
34
|
-
* The
|
|
48
|
+
* The actual moment in time after applying timezone conversion from the raw value.
|
|
35
49
|
*/
|
|
36
50
|
result?: Maybe<Date>;
|
|
37
51
|
/**
|
|
38
|
-
* Whether
|
|
39
|
-
*
|
|
40
|
-
* If not value, raw and result may be undefined.
|
|
52
|
+
* Whether the input was successfully parsed. When false, `raw` and `result` may be undefined.
|
|
41
53
|
*/
|
|
42
54
|
valid: boolean;
|
|
43
55
|
}
|
|
@@ -46,53 +58,212 @@ export interface ValidDateFromTimestringResult extends Required<DateFromTimestri
|
|
|
46
58
|
raw: Date;
|
|
47
59
|
valid: true;
|
|
48
60
|
}
|
|
61
|
+
/**
|
|
62
|
+
* Type guard that narrows a {@link DateFromTimestringResult} to {@link ValidDateFromTimestringResult},
|
|
63
|
+
* guaranteeing that `raw` and `result` are defined.
|
|
64
|
+
*
|
|
65
|
+
* @param result - The parse result to check.
|
|
66
|
+
*
|
|
67
|
+
* @example
|
|
68
|
+
* ```ts
|
|
69
|
+
* const parseResult = instance.timeStringToDateResult('1:30PM');
|
|
70
|
+
* if (isValidDateFromTimestringResult(parseResult)) {
|
|
71
|
+
* console.log(parseResult.result); // Date is guaranteed
|
|
72
|
+
* }
|
|
73
|
+
* ```
|
|
74
|
+
*/
|
|
49
75
|
export declare function isValidDateFromTimestringResult(result: ValidDateFromTimestringResult | DateFromTimestringResult): result is ValidDateFromTimestringResult;
|
|
50
76
|
/**
|
|
51
|
-
*
|
|
77
|
+
* Stateful utility for parsing and formatting time strings relative to a configured timezone.
|
|
78
|
+
*
|
|
79
|
+
* Handles the complexity of converting between wall-clock time representations and actual
|
|
80
|
+
* moments in time across timezones. Supports multiple input formats including "1:30PM",
|
|
81
|
+
* "13:30", "1PM", and logical date string codes.
|
|
82
|
+
*
|
|
83
|
+
* @example
|
|
84
|
+
* ```ts
|
|
85
|
+
* const instance = new DateTimeUtilityInstance('America/New_York');
|
|
86
|
+
* const date = instance.timeStringToDate('1:30PM');
|
|
87
|
+
* const timeStr = instance.toTimeString(new Date());
|
|
88
|
+
* ```
|
|
52
89
|
*/
|
|
53
90
|
export declare class DateTimeUtilityInstance {
|
|
54
91
|
readonly normalInstance: DateTimezoneUtcNormalInstance;
|
|
55
92
|
/**
|
|
56
|
-
* The default timezone all
|
|
57
|
-
*
|
|
58
|
-
* If the timezone is not defined, it defaults to UTC.
|
|
93
|
+
* @param timezone - The default timezone for all operations. Defaults to UTC if not provided.
|
|
59
94
|
*/
|
|
60
95
|
constructor(timezone?: Maybe<TimezoneString>);
|
|
61
96
|
get timezone(): TimezoneString;
|
|
97
|
+
/**
|
|
98
|
+
* Determines whether the given date falls in the AM or PM period for the specified timezone.
|
|
99
|
+
*
|
|
100
|
+
* @param date - The date to check. Defaults to now.
|
|
101
|
+
* @param timezone - Overrides the instance's configured timezone for this call.
|
|
102
|
+
*
|
|
103
|
+
* @example
|
|
104
|
+
* ```ts
|
|
105
|
+
* const instance = new DateTimeUtilityInstance('America/Chicago');
|
|
106
|
+
* const amPm = instance.getTimeAM(new Date()); // TimeAM.AM or TimeAM.PM
|
|
107
|
+
* ```
|
|
108
|
+
*/
|
|
62
109
|
getTimeAM(date?: Date, timezone?: TimezoneString): TimeAM;
|
|
110
|
+
/**
|
|
111
|
+
* Formats a date as a human-readable time string (e.g., "1:30PM") in the given timezone.
|
|
112
|
+
*
|
|
113
|
+
* @param date - The date to format.
|
|
114
|
+
* @param timezone - Overrides the instance's configured timezone for this call.
|
|
115
|
+
*
|
|
116
|
+
* @example
|
|
117
|
+
* ```ts
|
|
118
|
+
* const instance = new DateTimeUtilityInstance('UTC');
|
|
119
|
+
* instance.toTimeString(new Date('2024-01-15T13:30:00Z')); // '1:30PM'
|
|
120
|
+
* ```
|
|
121
|
+
*/
|
|
63
122
|
toTimeString(date: Date, timezone?: TimezoneString): ReadableTimeString;
|
|
123
|
+
/**
|
|
124
|
+
* Parses a readable time string into a {@link ParsedTimeString} with both UTC and
|
|
125
|
+
* timezone-adjusted dates, plus time-of-day metadata.
|
|
126
|
+
*
|
|
127
|
+
* @param input - A time string such as "1:30PM", "13:30", or "1PM".
|
|
128
|
+
* @param config - Optional parsing configuration with reference date and timezone overrides.
|
|
129
|
+
* @returns The parsed result, or undefined if the input could not be parsed.
|
|
130
|
+
*
|
|
131
|
+
* @example
|
|
132
|
+
* ```ts
|
|
133
|
+
* const instance = new DateTimeUtilityInstance('America/New_York');
|
|
134
|
+
* const parsed = instance.parseTimeString('2:30PM');
|
|
135
|
+
* if (parsed) {
|
|
136
|
+
* console.log(parsed.minutesSinceStartOfDay); // 870
|
|
137
|
+
* console.log(parsed.am); // TimeAM.PM
|
|
138
|
+
* }
|
|
139
|
+
* ```
|
|
140
|
+
*/
|
|
64
141
|
parseTimeString(input: ReadableTimeString, config?: ParseTimeString): Maybe<ParsedTimeString>;
|
|
65
142
|
/**
|
|
66
|
-
*
|
|
143
|
+
* Converts a time string or logical date code into an actual Date, applying timezone conversion.
|
|
67
144
|
*
|
|
68
|
-
*
|
|
69
|
-
*
|
|
70
|
-
*
|
|
145
|
+
* Supports many input formats: "1:30PM", "1:30 PM", "1PM", "1AM", "13:30", "1330", and
|
|
146
|
+
* logical date string codes.
|
|
147
|
+
*
|
|
148
|
+
* @param input - The time string or logical date code to parse.
|
|
149
|
+
* @param config - Optional parsing configuration with reference date and timezone overrides.
|
|
150
|
+
* @returns The resolved date, or undefined if the input could not be parsed.
|
|
151
|
+
*
|
|
152
|
+
* @example
|
|
153
|
+
* ```ts
|
|
154
|
+
* const instance = new DateTimeUtilityInstance('America/Denver');
|
|
155
|
+
* const date = instance.timeStringToDate('3:00PM');
|
|
156
|
+
* const dateWithRef = instance.timeStringToDate('3:00PM', { date: new Date('2024-06-15') });
|
|
157
|
+
* ```
|
|
71
158
|
*/
|
|
72
159
|
timeStringToDate(input: ReadableTimeString | LogicalDateStringCode, config?: ParseTimeString): Maybe<Date>;
|
|
73
160
|
_timeStringToDate(input: ReadableTimeString | LogicalDateStringCode, config?: ParseTimeString): DateFromTimestringResult | ValidDateFromTimestringResult;
|
|
74
161
|
private _normalizeInstanceForConfig;
|
|
75
162
|
}
|
|
163
|
+
/**
|
|
164
|
+
* Creates a {@link DateTimeUtilityInstance} configured for UTC.
|
|
165
|
+
*
|
|
166
|
+
* @example
|
|
167
|
+
* ```ts
|
|
168
|
+
* const utcInstance = dateTimeInstanceUtc();
|
|
169
|
+
* const timeStr = utcInstance.toTimeString(new Date()); // e.g., '5:30PM'
|
|
170
|
+
* ```
|
|
171
|
+
*/
|
|
76
172
|
export declare function dateTimeInstanceUtc(): DateTimeUtilityInstance;
|
|
173
|
+
/**
|
|
174
|
+
* Factory function that creates a {@link DateTimeUtilityInstance} for the given timezone.
|
|
175
|
+
* Defaults to UTC when no timezone is provided.
|
|
176
|
+
*
|
|
177
|
+
* @param timezone - The IANA timezone identifier (e.g., 'America/New_York').
|
|
178
|
+
*
|
|
179
|
+
* @example
|
|
180
|
+
* ```ts
|
|
181
|
+
* const nyInstance = dateTimeInstance('America/New_York');
|
|
182
|
+
* const date = nyInstance.timeStringToDate('9:00AM');
|
|
183
|
+
* ```
|
|
184
|
+
*/
|
|
77
185
|
export declare function dateTimeInstance(timezone?: Maybe<TimezoneString>): DateTimeUtilityInstance;
|
|
186
|
+
/**
|
|
187
|
+
* Determines whether the given date falls in the AM or PM period for the specified timezone.
|
|
188
|
+
*
|
|
189
|
+
* @param date - The date to check. Defaults to now.
|
|
190
|
+
* @param timezone - The IANA timezone to evaluate in. Defaults to UTC.
|
|
191
|
+
*
|
|
192
|
+
* @example
|
|
193
|
+
* ```ts
|
|
194
|
+
* const amPm = getTimeAM(new Date(), 'America/Chicago');
|
|
195
|
+
* if (amPm === TimeAM.AM) {
|
|
196
|
+
* console.log('Morning in Chicago');
|
|
197
|
+
* }
|
|
198
|
+
* ```
|
|
199
|
+
*/
|
|
78
200
|
export declare function getTimeAM(date?: Date, timezone?: Maybe<TimezoneString>): TimeAM;
|
|
79
201
|
/**
|
|
80
|
-
*
|
|
202
|
+
* Formats a date as a readable time string (e.g., "1:30PM") using the system's local timezone,
|
|
203
|
+
* unlike {@link toReadableTimeString} which defaults to UTC.
|
|
81
204
|
*
|
|
82
|
-
* @param date
|
|
83
|
-
*
|
|
205
|
+
* @param date - The date to format.
|
|
206
|
+
*
|
|
207
|
+
* @example
|
|
208
|
+
* ```ts
|
|
209
|
+
* const localTime = toLocalReadableTimeString(new Date()); // e.g., '9:45AM' in your local tz
|
|
210
|
+
* ```
|
|
84
211
|
*/
|
|
85
212
|
export declare function toLocalReadableTimeString(date: Date): ReadableTimeString;
|
|
213
|
+
/**
|
|
214
|
+
* Formats a date as a human-readable time string (e.g., "1:30PM") in the given timezone.
|
|
215
|
+
*
|
|
216
|
+
* @param date - The date to format.
|
|
217
|
+
* @param timezone - The IANA timezone to format in. Defaults to UTC.
|
|
218
|
+
*
|
|
219
|
+
* @example
|
|
220
|
+
* ```ts
|
|
221
|
+
* const time = toReadableTimeString(new Date(), 'America/New_York'); // e.g., '10:30AM'
|
|
222
|
+
* const utcTime = toReadableTimeString(new Date()); // UTC time
|
|
223
|
+
* ```
|
|
224
|
+
*/
|
|
86
225
|
export declare function toReadableTimeString(date: Date, timezone?: Maybe<TimezoneString>): ReadableTimeString;
|
|
87
226
|
/**
|
|
88
|
-
* Parses
|
|
227
|
+
* Parses a readable time string into a {@link ParsedTimeString} relative to the configured timezone.
|
|
228
|
+
* Falls back to UTC when no timezone is provided in the config.
|
|
89
229
|
*
|
|
90
|
-
*
|
|
230
|
+
* @param input - A time string such as "1:30PM", "13:30", or "1PM".
|
|
231
|
+
* @param config - Optional configuration specifying the timezone and reference date.
|
|
91
232
|
*
|
|
92
|
-
* @
|
|
93
|
-
*
|
|
94
|
-
*
|
|
233
|
+
* @example
|
|
234
|
+
* ```ts
|
|
235
|
+
* const parsed = parseReadableTimeString('2:00PM', { timezone: 'America/Denver' });
|
|
236
|
+
* if (parsed) {
|
|
237
|
+
* console.log(parsed.date); // 2:00PM Denver time as a Date
|
|
238
|
+
* console.log(parsed.am); // TimeAM.PM
|
|
239
|
+
* }
|
|
240
|
+
* ```
|
|
95
241
|
*/
|
|
96
242
|
export declare function parseReadableTimeString(input: ReadableTimeString, config?: ParseTimeString): Maybe<ParsedTimeString>;
|
|
243
|
+
/**
|
|
244
|
+
* Convenience function that parses a readable time string directly into a Date.
|
|
245
|
+
* Combines {@link dateTimeInstance} creation and {@link DateTimeUtilityInstance.timeStringToDate} in one call.
|
|
246
|
+
*
|
|
247
|
+
* @param input - A time string such as "1:30PM" or "13:30".
|
|
248
|
+
* @param config - Optional configuration specifying the timezone and reference date.
|
|
249
|
+
*
|
|
250
|
+
* @example
|
|
251
|
+
* ```ts
|
|
252
|
+
* const date = readableTimeStringToDate('9:00AM', { timezone: 'Europe/London' });
|
|
253
|
+
* ```
|
|
254
|
+
*/
|
|
97
255
|
export declare function readableTimeStringToDate(input: ReadableTimeString, config?: ParseTimeString): Maybe<Date>;
|
|
256
|
+
/**
|
|
257
|
+
* Creates a {@link LimitDateTimeInstance} for clamping and constraining dates to a configured range.
|
|
258
|
+
*
|
|
259
|
+
* @param config - The limit configuration specifying min/max bounds, future requirements, etc.
|
|
260
|
+
*
|
|
261
|
+
* @example
|
|
262
|
+
* ```ts
|
|
263
|
+
* const limiter = limitDateTime({
|
|
264
|
+
* limits: { isFuture: true, future: { hours: 1 } }
|
|
265
|
+
* });
|
|
266
|
+
* const clamped = limiter.clamp(someDate); // ensures date is at least 1 hour in the future
|
|
267
|
+
* ```
|
|
268
|
+
*/
|
|
98
269
|
export declare function limitDateTime(config: LimitDateTimeConfig): LimitDateTimeInstance;
|
|
@@ -45,7 +45,21 @@ export interface LimitDateTimeConfig {
|
|
|
45
45
|
};
|
|
46
46
|
}
|
|
47
47
|
/**
|
|
48
|
-
*
|
|
48
|
+
* Derives min/max date boundaries from a {@link LimitDateTimeConfig} and provides clamping
|
|
49
|
+
* utilities for constraining dates and date ranges within those boundaries.
|
|
50
|
+
*
|
|
51
|
+
* Supports dynamic limits based on the current time (e.g., "must be in the future",
|
|
52
|
+
* "must be at least N minutes from now") as well as static min/max bounds.
|
|
53
|
+
*
|
|
54
|
+
* @example
|
|
55
|
+
* ```ts
|
|
56
|
+
* const limiter = new LimitDateTimeInstance({
|
|
57
|
+
* limits: { isFuture: true, future: { hours: 2 } }
|
|
58
|
+
* });
|
|
59
|
+
*
|
|
60
|
+
* const range = limiter.dateRange(); // { start: <2 hours from now>, end: undefined }
|
|
61
|
+
* const clamped = limiter.clamp(someDate);
|
|
62
|
+
* ```
|
|
49
63
|
*/
|
|
50
64
|
export declare class LimitDateTimeInstance {
|
|
51
65
|
private readonly _config;
|
|
@@ -56,19 +70,68 @@ export declare class LimitDateTimeInstance {
|
|
|
56
70
|
get min(): Maybe<LogicalDate>;
|
|
57
71
|
get max(): Maybe<LogicalDate>;
|
|
58
72
|
/**
|
|
59
|
-
*
|
|
73
|
+
* Computes the allowed date range based on the configured limits, evaluated at the
|
|
74
|
+
* config's instant (or now if not set).
|
|
60
75
|
*
|
|
61
|
-
* @
|
|
76
|
+
* @example
|
|
77
|
+
* ```ts
|
|
78
|
+
* const limiter = new LimitDateTimeInstance({ limits: { isFuture: true } });
|
|
79
|
+
* const range = limiter.dateRange(); // { start: <now>, end: undefined }
|
|
80
|
+
* ```
|
|
62
81
|
*/
|
|
63
82
|
dateRange(): Partial<DateRange>;
|
|
83
|
+
/**
|
|
84
|
+
* Computes the allowed date range evaluated at a specific instant, allowing limits
|
|
85
|
+
* like "now" or relative future offsets to resolve against the given moment.
|
|
86
|
+
*
|
|
87
|
+
* @param instant - The reference point in time for resolving dynamic limits.
|
|
88
|
+
*/
|
|
64
89
|
dateRangeForInstant(instant: Date): {
|
|
65
90
|
start: Date | undefined;
|
|
66
91
|
end: Date | undefined;
|
|
67
92
|
};
|
|
68
93
|
/**
|
|
69
|
-
* Clamps the input date to the
|
|
94
|
+
* Clamps the input date to the allowed range, optionally applying `takeNextUpcomingTime`
|
|
95
|
+
* (copies time to today, advancing to tomorrow if already past) or `roundDownToMinute`.
|
|
96
|
+
*
|
|
97
|
+
* @param date - The date to constrain.
|
|
98
|
+
*
|
|
99
|
+
* @example
|
|
100
|
+
* ```ts
|
|
101
|
+
* const limiter = new LimitDateTimeInstance({
|
|
102
|
+
* limits: { isFuture: true, future: { minutes: 30 } }
|
|
103
|
+
* });
|
|
104
|
+
* const safe = limiter.clamp(new Date()); // at least 30 minutes from now
|
|
105
|
+
* ```
|
|
70
106
|
*/
|
|
71
107
|
clamp(date: Date): Date;
|
|
108
|
+
/**
|
|
109
|
+
* Clamps an entire date range to fit within the allowed limits, constraining both
|
|
110
|
+
* start and end dates.
|
|
111
|
+
*
|
|
112
|
+
* @param dateRange - The date range to constrain.
|
|
113
|
+
*
|
|
114
|
+
* @example
|
|
115
|
+
* ```ts
|
|
116
|
+
* const limiter = new LimitDateTimeInstance({ limits: { isFuture: true } });
|
|
117
|
+
* const clamped = limiter.clampDateRange({ start: pastDate, end: futureDate });
|
|
118
|
+
* // clamped.start will be at least now
|
|
119
|
+
* ```
|
|
120
|
+
*/
|
|
72
121
|
clampDateRange(dateRange: DateRange): DateRange;
|
|
73
122
|
}
|
|
123
|
+
/**
|
|
124
|
+
* Factory function that creates a {@link LimitDateTimeInstance} from the given configuration.
|
|
125
|
+
*
|
|
126
|
+
* @param config - The limit configuration specifying bounds, future requirements, and rounding.
|
|
127
|
+
*
|
|
128
|
+
* @example
|
|
129
|
+
* ```ts
|
|
130
|
+
* const limiter = limitDateTimeInstance({
|
|
131
|
+
* limits: { min: new Date('2024-01-01'), isFuture: true },
|
|
132
|
+
* roundDownToMinute: true
|
|
133
|
+
* });
|
|
134
|
+
* const clamped = limiter.clamp(someDate);
|
|
135
|
+
* ```
|
|
136
|
+
*/
|
|
74
137
|
export declare function limitDateTimeInstance(config: LimitDateTimeConfig): LimitDateTimeInstance;
|