chronos-ts 1.0.3 → 2.0.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.
@@ -0,0 +1,428 @@
1
+ /**
2
+ * Core type definitions for Chronos
3
+ * @module types
4
+ */
5
+ /**
6
+ * All supported time units in the library
7
+ */
8
+ export type TimeUnit = 'millisecond' | 'second' | 'minute' | 'hour' | 'day' | 'week' | 'month' | 'quarter' | 'year' | 'decade' | 'century' | 'millennium';
9
+ /**
10
+ * Plural forms of time units
11
+ */
12
+ export type TimeUnitPlural = 'milliseconds' | 'seconds' | 'minutes' | 'hours' | 'days' | 'weeks' | 'months' | 'quarters' | 'years' | 'decades' | 'centuries' | 'millennia';
13
+ /**
14
+ * Short forms of time units
15
+ */
16
+ export type TimeUnitShort = 'ms' | 's' | 'm' | 'h' | 'd' | 'w' | 'M' | 'Q' | 'y';
17
+ /**
18
+ * Combined time unit type accepting any form
19
+ */
20
+ export type AnyTimeUnit = TimeUnit | TimeUnitPlural | TimeUnitShort;
21
+ /**
22
+ * Days of the week (0 = Sunday, 6 = Saturday)
23
+ */
24
+ export declare enum DayOfWeek {
25
+ Sunday = 0,
26
+ Monday = 1,
27
+ Tuesday = 2,
28
+ Wednesday = 3,
29
+ Thursday = 4,
30
+ Friday = 5,
31
+ Saturday = 6
32
+ }
33
+ /**
34
+ * Months of the year (1 = January, 12 = December)
35
+ */
36
+ export declare enum Month {
37
+ January = 1,
38
+ February = 2,
39
+ March = 3,
40
+ April = 4,
41
+ May = 5,
42
+ June = 6,
43
+ July = 7,
44
+ August = 8,
45
+ September = 9,
46
+ October = 10,
47
+ November = 11,
48
+ December = 12
49
+ }
50
+ /**
51
+ * Valid input types that can be parsed into a Chronos instance
52
+ */
53
+ export type DateInput = string | number | Date | ChronosLike | null | undefined;
54
+ /**
55
+ * Interface for Chronos-like objects
56
+ */
57
+ export interface ChronosLike {
58
+ toDate(): Date;
59
+ valueOf(): number;
60
+ }
61
+ /**
62
+ * Object representing individual date/time components
63
+ */
64
+ export interface DateTimeComponents {
65
+ year?: number;
66
+ month?: number;
67
+ day?: number;
68
+ hour?: number;
69
+ minute?: number;
70
+ second?: number;
71
+ millisecond?: number;
72
+ }
73
+ /**
74
+ * Object for setting date/time parts
75
+ */
76
+ export interface DateTimeSetter {
77
+ year?: number;
78
+ month?: number;
79
+ date?: number;
80
+ day?: number;
81
+ hour?: number;
82
+ minute?: number;
83
+ second?: number;
84
+ millisecond?: number;
85
+ }
86
+ /**
87
+ * Duration object for intervals
88
+ */
89
+ export interface Duration {
90
+ years?: number;
91
+ months?: number;
92
+ weeks?: number;
93
+ days?: number;
94
+ hours?: number;
95
+ minutes?: number;
96
+ seconds?: number;
97
+ milliseconds?: number;
98
+ }
99
+ /**
100
+ * ISO 8601 duration string pattern
101
+ */
102
+ export type ISODuration = string;
103
+ /**
104
+ * Valid timezone input types
105
+ */
106
+ export type TimezoneInput = string | number | TimezoneInfo;
107
+ /**
108
+ * Timezone offset information
109
+ */
110
+ export interface TimezoneOffset {
111
+ /**
112
+ * Offset in minutes from UTC
113
+ */
114
+ minutes: number;
115
+ /**
116
+ * Offset in hours from UTC
117
+ */
118
+ hours: number;
119
+ /**
120
+ * Offset as string (e.g., "+05:30", "-08:00")
121
+ */
122
+ string: string;
123
+ }
124
+ /**
125
+ * DST transition information
126
+ */
127
+ export interface DSTransition {
128
+ /**
129
+ * Date of the DST transition
130
+ */
131
+ date: Date;
132
+ /**
133
+ * Offset before transition (in minutes)
134
+ */
135
+ fromOffset: number;
136
+ /**
137
+ * Offset after transition (in minutes)
138
+ */
139
+ toOffset: number;
140
+ /**
141
+ * Whether this is the start of DST (spring forward)
142
+ */
143
+ isDSTStart: boolean;
144
+ }
145
+ /**
146
+ * Timezone information object
147
+ */
148
+ export interface TimezoneInfo {
149
+ /**
150
+ * IANA timezone identifier
151
+ */
152
+ identifier: string;
153
+ /**
154
+ * Display name of the timezone
155
+ */
156
+ name: string;
157
+ /**
158
+ * Timezone abbreviation (e.g., EST, PST)
159
+ */
160
+ abbreviation: string;
161
+ /**
162
+ * Current UTC offset
163
+ */
164
+ offset: TimezoneOffset;
165
+ /**
166
+ * Whether DST is currently in effect
167
+ */
168
+ isDST: boolean;
169
+ /**
170
+ * Whether this timezone observes DST
171
+ */
172
+ observesDST: boolean;
173
+ }
174
+ /**
175
+ * Format token types for custom formatting
176
+ */
177
+ export interface FormatTokens {
178
+ [key: string]: ((date: Date) => string) | string;
179
+ }
180
+ /**
181
+ * Predefined format presets
182
+ */
183
+ export type FormatPreset = 'date' | 'time' | 'datetime' | 'iso' | 'rfc2822' | 'rfc3339' | 'atom' | 'cookie' | 'rss' | 'w3c';
184
+ /**
185
+ * Comparison operators
186
+ */
187
+ export type CompareOperator = '<' | '<=' | '=' | '>=' | '>' | '!=';
188
+ /**
189
+ * Comparison granularity options
190
+ */
191
+ export type CompareGranularity = TimeUnit | 'default';
192
+ /**
193
+ * Period options for iteration
194
+ */
195
+ export interface PeriodOptions {
196
+ /**
197
+ * Exclude the start date from iteration
198
+ */
199
+ excludeStart?: boolean;
200
+ /**
201
+ * Exclude the end date from iteration
202
+ */
203
+ excludeEnd?: boolean;
204
+ /**
205
+ * Maximum number of iterations
206
+ */
207
+ recurrences?: number;
208
+ /**
209
+ * Whether period modifications return new instances
210
+ */
211
+ immutable?: boolean;
212
+ }
213
+ /**
214
+ * Period boundary options
215
+ */
216
+ export declare enum PeriodBoundary {
217
+ Open = "open",
218
+ Closed = "closed",
219
+ OpenStart = "openStart",
220
+ OpenEnd = "openEnd"
221
+ }
222
+ /**
223
+ * Locale configuration
224
+ */
225
+ export interface LocaleConfig {
226
+ code: string;
227
+ months: string[];
228
+ monthsShort: string[];
229
+ weekdays: string[];
230
+ weekdaysShort: string[];
231
+ weekdaysMin: string[];
232
+ ordinal: (n: number) => string;
233
+ formats: {
234
+ LT: string;
235
+ LTS: string;
236
+ L: string;
237
+ LL: string;
238
+ LLL: string;
239
+ LLLL: string;
240
+ l?: string;
241
+ ll?: string;
242
+ lll?: string;
243
+ llll?: string;
244
+ };
245
+ relativeTime: {
246
+ future: string;
247
+ past: string;
248
+ s: string;
249
+ ss: string;
250
+ m: string;
251
+ mm: string;
252
+ h: string;
253
+ hh: string;
254
+ d: string;
255
+ dd: string;
256
+ w?: string;
257
+ ww?: string;
258
+ M: string;
259
+ MM: string;
260
+ y: string;
261
+ yy: string;
262
+ };
263
+ meridiem?: (hour: number, minute: number, isLowercase: boolean) => string;
264
+ week?: {
265
+ dow: number;
266
+ doy: number;
267
+ };
268
+ }
269
+ /**
270
+ * Global configuration options
271
+ */
272
+ export interface ChronosConfig {
273
+ /**
274
+ * Default timezone
275
+ */
276
+ timezone?: string;
277
+ /**
278
+ * Default locale
279
+ */
280
+ locale?: string;
281
+ /**
282
+ * First day of week (0 = Sunday, 1 = Monday, etc.)
283
+ */
284
+ weekStartsOn?: DayOfWeek;
285
+ /**
286
+ * First week of year contains this date
287
+ */
288
+ firstWeekContainsDate?: 1 | 4;
289
+ /**
290
+ * Strict parsing mode
291
+ */
292
+ strict?: boolean;
293
+ }
294
+ /**
295
+ * Makes all properties optional recursively
296
+ */
297
+ export type DeepPartial<T> = {
298
+ [P in keyof T]?: T[P] extends object ? DeepPartial<T[P]> : T[P];
299
+ };
300
+ /**
301
+ * Extract numeric keys from an object type
302
+ */
303
+ export type NumericKeys<T> = {
304
+ [K in keyof T]: T[K] extends number ? K : never;
305
+ }[keyof T];
306
+ /**
307
+ * Type guard result
308
+ */
309
+ export type TypeGuard<T> = (value: unknown) => value is T;
310
+ /**
311
+ * Chainable method return type
312
+ */
313
+ export type Chainable<T> = T & {
314
+ clone(): T;
315
+ };
316
+ /**
317
+ * Options for human-readable difference output
318
+ */
319
+ export interface HumanDiffOptions {
320
+ /**
321
+ * Use short form (1d vs 1 day)
322
+ */
323
+ short?: boolean;
324
+ /**
325
+ * Number of units to include
326
+ */
327
+ parts?: number;
328
+ /**
329
+ * Use absolute value
330
+ */
331
+ absolute?: boolean;
332
+ /**
333
+ * Separator between parts
334
+ */
335
+ separator?: string;
336
+ /**
337
+ * Conjunction for last part
338
+ */
339
+ conjunction?: string;
340
+ /**
341
+ * Include numeric suffix (st, nd, rd, th)
342
+ */
343
+ ordinal?: boolean;
344
+ }
345
+ /**
346
+ * Diff result with all units
347
+ */
348
+ export interface DiffResult {
349
+ years: number;
350
+ months: number;
351
+ weeks: number;
352
+ days: number;
353
+ hours: number;
354
+ minutes: number;
355
+ seconds: number;
356
+ milliseconds: number;
357
+ totalMilliseconds: number;
358
+ }
359
+ /**
360
+ * Serializable representation of a Chronos instance
361
+ */
362
+ export interface ChronosSerializable {
363
+ timestamp: number;
364
+ timezone: string;
365
+ offset: number;
366
+ }
367
+ /**
368
+ * JSON representation
369
+ */
370
+ export interface ChronosJSON {
371
+ iso: string;
372
+ timestamp: number;
373
+ timezone: string;
374
+ }
375
+ /**
376
+ * Represents a time range/span
377
+ */
378
+ export interface TimeRange {
379
+ start: Date;
380
+ end: Date;
381
+ }
382
+ /**
383
+ * Business hours configuration
384
+ */
385
+ export interface BusinessHours {
386
+ open: {
387
+ hour: number;
388
+ minute: number;
389
+ };
390
+ close: {
391
+ hour: number;
392
+ minute: number;
393
+ };
394
+ days: DayOfWeek[];
395
+ holidays?: Date[];
396
+ }
397
+ /**
398
+ * Calendar week representation
399
+ */
400
+ export interface CalendarWeek {
401
+ weekNumber: number;
402
+ year: number;
403
+ days: Date[];
404
+ }
405
+ /**
406
+ * Calendar month representation
407
+ */
408
+ export interface CalendarMonth {
409
+ year: number;
410
+ month: number;
411
+ weeks: CalendarWeek[];
412
+ }
413
+ export declare const MILLISECONDS_PER_SECOND = 1000;
414
+ export declare const SECONDS_PER_MINUTE = 60;
415
+ export declare const MINUTES_PER_HOUR = 60;
416
+ export declare const HOURS_PER_DAY = 24;
417
+ export declare const DAYS_PER_WEEK = 7;
418
+ export declare const MONTHS_PER_YEAR = 12;
419
+ export declare const DAYS_PER_YEAR = 365;
420
+ export declare const DAYS_PER_LEAP_YEAR = 366;
421
+ export declare const MILLISECONDS_PER_MINUTE: number;
422
+ export declare const MILLISECONDS_PER_HOUR: number;
423
+ export declare const MILLISECONDS_PER_DAY: number;
424
+ export declare const MILLISECONDS_PER_WEEK: number;
425
+ export declare const AVERAGE_DAYS_PER_MONTH = 30.436875;
426
+ export declare const AVERAGE_DAYS_PER_YEAR = 365.2425;
427
+ export declare const MILLISECONDS_PER_MONTH: number;
428
+ export declare const MILLISECONDS_PER_YEAR: number;
@@ -0,0 +1,71 @@
1
+ "use strict";
2
+ /**
3
+ * Core type definitions for Chronos
4
+ * @module types
5
+ */
6
+ Object.defineProperty(exports, "__esModule", { value: true });
7
+ exports.MILLISECONDS_PER_YEAR = exports.MILLISECONDS_PER_MONTH = exports.AVERAGE_DAYS_PER_YEAR = exports.AVERAGE_DAYS_PER_MONTH = exports.MILLISECONDS_PER_WEEK = exports.MILLISECONDS_PER_DAY = exports.MILLISECONDS_PER_HOUR = exports.MILLISECONDS_PER_MINUTE = exports.DAYS_PER_LEAP_YEAR = exports.DAYS_PER_YEAR = exports.MONTHS_PER_YEAR = exports.DAYS_PER_WEEK = exports.HOURS_PER_DAY = exports.MINUTES_PER_HOUR = exports.SECONDS_PER_MINUTE = exports.MILLISECONDS_PER_SECOND = exports.PeriodBoundary = exports.Month = exports.DayOfWeek = void 0;
8
+ // ============================================================================
9
+ // Day of Week Types
10
+ // ============================================================================
11
+ /**
12
+ * Days of the week (0 = Sunday, 6 = Saturday)
13
+ */
14
+ var DayOfWeek;
15
+ (function (DayOfWeek) {
16
+ DayOfWeek[DayOfWeek["Sunday"] = 0] = "Sunday";
17
+ DayOfWeek[DayOfWeek["Monday"] = 1] = "Monday";
18
+ DayOfWeek[DayOfWeek["Tuesday"] = 2] = "Tuesday";
19
+ DayOfWeek[DayOfWeek["Wednesday"] = 3] = "Wednesday";
20
+ DayOfWeek[DayOfWeek["Thursday"] = 4] = "Thursday";
21
+ DayOfWeek[DayOfWeek["Friday"] = 5] = "Friday";
22
+ DayOfWeek[DayOfWeek["Saturday"] = 6] = "Saturday";
23
+ })(DayOfWeek || (exports.DayOfWeek = DayOfWeek = {}));
24
+ /**
25
+ * Months of the year (1 = January, 12 = December)
26
+ */
27
+ var Month;
28
+ (function (Month) {
29
+ Month[Month["January"] = 1] = "January";
30
+ Month[Month["February"] = 2] = "February";
31
+ Month[Month["March"] = 3] = "March";
32
+ Month[Month["April"] = 4] = "April";
33
+ Month[Month["May"] = 5] = "May";
34
+ Month[Month["June"] = 6] = "June";
35
+ Month[Month["July"] = 7] = "July";
36
+ Month[Month["August"] = 8] = "August";
37
+ Month[Month["September"] = 9] = "September";
38
+ Month[Month["October"] = 10] = "October";
39
+ Month[Month["November"] = 11] = "November";
40
+ Month[Month["December"] = 12] = "December";
41
+ })(Month || (exports.Month = Month = {}));
42
+ /**
43
+ * Period boundary options
44
+ */
45
+ var PeriodBoundary;
46
+ (function (PeriodBoundary) {
47
+ PeriodBoundary["Open"] = "open";
48
+ PeriodBoundary["Closed"] = "closed";
49
+ PeriodBoundary["OpenStart"] = "openStart";
50
+ PeriodBoundary["OpenEnd"] = "openEnd";
51
+ })(PeriodBoundary || (exports.PeriodBoundary = PeriodBoundary = {}));
52
+ // ============================================================================
53
+ // Constants
54
+ // ============================================================================
55
+ exports.MILLISECONDS_PER_SECOND = 1000;
56
+ exports.SECONDS_PER_MINUTE = 60;
57
+ exports.MINUTES_PER_HOUR = 60;
58
+ exports.HOURS_PER_DAY = 24;
59
+ exports.DAYS_PER_WEEK = 7;
60
+ exports.MONTHS_PER_YEAR = 12;
61
+ exports.DAYS_PER_YEAR = 365;
62
+ exports.DAYS_PER_LEAP_YEAR = 366;
63
+ exports.MILLISECONDS_PER_MINUTE = exports.MILLISECONDS_PER_SECOND * exports.SECONDS_PER_MINUTE;
64
+ exports.MILLISECONDS_PER_HOUR = exports.MILLISECONDS_PER_MINUTE * exports.MINUTES_PER_HOUR;
65
+ exports.MILLISECONDS_PER_DAY = exports.MILLISECONDS_PER_HOUR * exports.HOURS_PER_DAY;
66
+ exports.MILLISECONDS_PER_WEEK = exports.MILLISECONDS_PER_DAY * exports.DAYS_PER_WEEK;
67
+ // Average values for variable-length units
68
+ exports.AVERAGE_DAYS_PER_MONTH = 30.436875;
69
+ exports.AVERAGE_DAYS_PER_YEAR = 365.2425;
70
+ exports.MILLISECONDS_PER_MONTH = exports.MILLISECONDS_PER_DAY * exports.AVERAGE_DAYS_PER_MONTH;
71
+ exports.MILLISECONDS_PER_YEAR = exports.MILLISECONDS_PER_DAY * exports.AVERAGE_DAYS_PER_YEAR;
@@ -0,0 +1,127 @@
1
+ /**
2
+ * Utility functions for Chronos
3
+ * @module utils
4
+ */
5
+ import { AnyTimeUnit, TimeUnit, Duration, DateInput, ChronosLike, MILLISECONDS_PER_SECOND, MILLISECONDS_PER_MINUTE, MILLISECONDS_PER_HOUR, MILLISECONDS_PER_DAY, MILLISECONDS_PER_WEEK, MILLISECONDS_PER_MONTH, MILLISECONDS_PER_YEAR } from '../types';
6
+ export { MILLISECONDS_PER_SECOND, MILLISECONDS_PER_MINUTE, MILLISECONDS_PER_HOUR, MILLISECONDS_PER_DAY, MILLISECONDS_PER_WEEK, MILLISECONDS_PER_MONTH, MILLISECONDS_PER_YEAR, };
7
+ /**
8
+ * Check if value is a Date object
9
+ */
10
+ export declare function isDate(value: unknown): value is Date;
11
+ /**
12
+ * Check if value is a valid date input
13
+ */
14
+ export declare function isValidDateInput(value: unknown): value is DateInput;
15
+ /**
16
+ * Check if value implements ChronosLike interface
17
+ */
18
+ export declare function isChronosLike(value: unknown): value is ChronosLike;
19
+ /**
20
+ * Check if value is a Duration object
21
+ */
22
+ export declare function isDuration(value: unknown): value is Duration;
23
+ /**
24
+ * Check if value is a valid ISO 8601 duration string
25
+ */
26
+ export declare function isISODuration(value: unknown): value is string;
27
+ /**
28
+ * Check if a year is a leap year
29
+ */
30
+ export declare function isLeapYear(year: number): boolean;
31
+ /**
32
+ * Normalize a time unit to its canonical form
33
+ */
34
+ export declare function normalizeUnit(unit: AnyTimeUnit | string): TimeUnit;
35
+ /**
36
+ * Get the plural form of a time unit
37
+ */
38
+ export declare function pluralizeUnit(unit: TimeUnit, count: number): string;
39
+ /**
40
+ * Get milliseconds for a given time unit
41
+ */
42
+ export declare function getMillisecondsPerUnit(unit: TimeUnit): number;
43
+ /**
44
+ * Get the number of days in a specific month
45
+ */
46
+ export declare function getDaysInMonth(year: number, month: number): number;
47
+ /**
48
+ * Get the number of days in a year
49
+ */
50
+ export declare function getDaysInYear(year: number): number;
51
+ /**
52
+ * Get the day of year (1-366)
53
+ */
54
+ export declare function getDayOfYear(date: Date): number;
55
+ /**
56
+ * Get the ISO week number (1-53)
57
+ */
58
+ export declare function getISOWeek(date: Date): number;
59
+ /**
60
+ * Get the ISO week year
61
+ */
62
+ export declare function getISOWeekYear(date: Date): number;
63
+ /**
64
+ * Get the quarter (1-4)
65
+ */
66
+ export declare function getQuarter(date: Date): number;
67
+ /**
68
+ * Get start of a time unit
69
+ */
70
+ export declare function startOf(date: Date, unit: TimeUnit): Date;
71
+ /**
72
+ * Get end of a time unit
73
+ */
74
+ export declare function endOf(date: Date, unit: TimeUnit): Date;
75
+ /**
76
+ * Add a duration to a date
77
+ * Handles month overflow by clamping to the last day of the month
78
+ */
79
+ export declare function addDuration(date: Date, duration: Duration): Date;
80
+ /**
81
+ * Subtract a duration from a date
82
+ */
83
+ export declare function subtractDuration(date: Date, duration: Duration): Date;
84
+ /**
85
+ * Add a specific number of units to a date
86
+ */
87
+ export declare function addUnits(date: Date, amount: number, unit: TimeUnit): Date;
88
+ /**
89
+ * Calculate the difference between two dates in a specific unit
90
+ */
91
+ export declare function diffInUnits(date1: Date, date2: Date, unit: TimeUnit): number;
92
+ /**
93
+ * Parse an ISO 8601 duration string
94
+ */
95
+ export declare function parseISODuration(duration: string): Duration;
96
+ /**
97
+ * Convert a duration to ISO 8601 format
98
+ */
99
+ export declare function durationToISO(duration: Duration): string;
100
+ /**
101
+ * Compare two dates at a specific granularity
102
+ */
103
+ export declare function compareAtGranularity(date1: Date, date2: Date, unit: TimeUnit): number;
104
+ /**
105
+ * Check if two dates are the same at a specific granularity
106
+ */
107
+ export declare function isSameAt(date1: Date, date2: Date, unit: TimeUnit): boolean;
108
+ /**
109
+ * Create a deep clone of a date
110
+ */
111
+ export declare function cloneDate(date: Date): Date;
112
+ /**
113
+ * Check if a date is valid
114
+ */
115
+ export declare function isValidDate(date: Date): boolean;
116
+ /**
117
+ * Ensure a value is within bounds
118
+ */
119
+ export declare function clamp(value: number, min: number, max: number): number;
120
+ /**
121
+ * Get ordinal suffix for a number (1st, 2nd, 3rd, etc.)
122
+ */
123
+ export declare function ordinalSuffix(n: number): string;
124
+ /**
125
+ * Pad a number with leading zeros
126
+ */
127
+ export declare function padStart(value: number | string, length: number, char?: string): string;