@catbee/utils 2.0.0 → 2.0.2

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/date/index.d.ts CHANGED
@@ -22,12 +22,31 @@
22
22
  * SOFTWARE.
23
23
  */
24
24
 
25
+ /**
26
+ * Named date format presets
27
+ *
28
+ * | Preset | Equivalent | Example (en-US) |
29
+ * |---------------|---------------------------------------|----------------------------------------------------|
30
+ * | 'short' | 'M/d/yy, h:mm a' | 6/15/15, 9:03 AM |
31
+ * | 'medium' | 'MMM d, y, h:mm:ss a' | Jun 15, 2015, 9:03:01 AM |
32
+ * | 'long' | 'MMMM d, y, h:mm:ss a z' | June 15, 2015 at 9:03:01 AM GMT+1 |
33
+ * | 'full' | 'EEEE, MMMM d, y, h:mm:ss a zzzz' | Monday, June 15, 2015 at 9:03:01 AM GMT+01:00 |
34
+ * | 'shortDate' | 'M/d/yy' | 6/15/15 |
35
+ * | 'mediumDate' | 'MMM d, y' | Jun 15, 2015 |
36
+ * | 'longDate' | 'MMMM d, y' | June 15, 2015 |
37
+ * | 'fullDate' | 'EEEE, MMMM d, y' | Monday, June 15, 2015 |
38
+ * | 'shortTime' | 'h:mm a' | 9:03 AM |
39
+ * | 'mediumTime' | 'h:mm:ss a' | 9:03:01 AM |
40
+ * | 'longTime' | 'h:mm:ss a z' | 9:03:01 AM GMT+1 |
41
+ * | 'fullTime' | 'h:mm:ss a zzzz' | 9:03:01 AM GMT+01:00 |
42
+ */
43
+ type DateFormatPreset = 'short' | 'medium' | 'long' | 'full' | 'shortDate' | 'mediumDate' | 'longDate' | 'fullDate' | 'shortTime' | 'mediumTime' | 'longTime' | 'fullTime';
25
44
  /**
26
45
  * Format options for the formatDate function
27
46
  */
28
47
  interface DateFormatOptions {
29
- /** Date format pattern (default: 'yyyy-MM-dd') */
30
- format?: string;
48
+ /** Date format pattern, named preset, or 'relative' (default: 'yyyy-MM-dd') */
49
+ format?: DateFormatPreset | string;
31
50
  /** Locale to use for formatting (default: system locale) */
32
51
  locale?: string | string[];
33
52
  /** Time zone to use (default: system time zone) */
@@ -65,13 +84,20 @@ declare function formatRelativeTime(date: Date | number, now?: Date | number, lo
65
84
  /**
66
85
  * Parse a date string or timestamp into a Date object.
67
86
  *
87
+ * **Timezone Behavior:**
88
+ * - Date-only strings (e.g., '2023-05-15') are parsed as **local midnight**,
89
+ * not UTC midnight (differs from `new Date()` native behavior)
90
+ * - ISO strings with timezone (e.g., '2023-05-15T10:30:00Z') use the specified timezone
91
+ * - This follows Luxon's "local time first" philosophy
92
+ *
68
93
  * @param input - Date string or timestamp to parse
69
94
  * @param fallback - Fallback date if parsing fails
70
95
  * @returns Parsed Date object or fallback
71
96
  *
72
97
  * @example
73
98
  * ```typescript
74
- * parseDate('2023-05-15'); // Date object for May 15, 2023
99
+ * parseDate('2023-05-15'); // Local midnight on May 15, 2023
100
+ * parseDate('2023-05-15T10:30:00Z'); // 10:30 AM UTC on May 15, 2023
75
101
  * parseDate('invalid', new Date()); // Returns current date as fallback
76
102
  * ```
77
103
  */
@@ -94,6 +120,26 @@ declare function parseDate(input: string | number, fallback?: Date): Date | null
94
120
  * ```
95
121
  */
96
122
  declare function dateDiff(date1: Date | number, date2?: Date | number, unit?: 'milliseconds' | 'seconds' | 'minutes' | 'hours' | 'days' | 'months' | 'years'): number;
123
+ /**
124
+ * Calculate the difference in calendar days between two dates as observed in a specific timezone.
125
+ * Both dates are converted to calendar year/month/day values in the supplied IANA timezone, and
126
+ * the difference between those calendar dates is returned.
127
+ *
128
+ * JavaScript `Date` instances do not retain timezone identity, so this function does not detect
129
+ * whether the input dates originated from different timezones.
130
+ *
131
+ * @param d1 - First date
132
+ * @param d2 - Second date
133
+ * @param tz - IANA timezone identifier used to interpret both dates (e.g., 'America/New_York')
134
+ * @return Number of calendar days difference between the two dates in the specified timezone
135
+ * @throws {RangeError} If `tz` is not a valid IANA timezone identifier, as thrown by the underlying `Intl` APIs
136
+ * @example
137
+ * ```typescript
138
+ * // Calculate days difference in New York timezone
139
+ * dateDiffDaysTZ(new Date('2023-05-15T00:00:00'), new Date('2023-05-14T23:00:00'), 'America/New_York'); // 1
140
+ * ```
141
+ */
142
+ declare function dateDiffDaysTZ(d1: Date, d2: Date, tz: string): number;
97
143
  /**
98
144
  * Add a specified amount of time to a date.
99
145
  *
@@ -111,7 +157,9 @@ declare function dateDiff(date1: Date | number, date2?: Date | number, unit?: 'm
111
157
  * addToDate(new Date('2023-05-15T10:00:00'), -2, 'hours'); // Date for May 15, 2023 08:00:00
112
158
  * ```
113
159
  */
114
- declare function addToDate(date: Date | number, amount: number, unit: 'milliseconds' | 'seconds' | 'minutes' | 'hours' | 'days' | 'months' | 'years'): Date;
160
+ declare function addToDate(date: Date | number, amount: number, unit: 'milliseconds' | 'seconds' | 'minutes' | 'hours' | 'days' | 'months' | 'years', options?: {
161
+ timeZone?: string;
162
+ }): Date;
115
163
  /**
116
164
  * Get the start of a time period containing the specified date.
117
165
  *
@@ -303,6 +351,96 @@ declare function quarterOf(date: Date | number): 1 | 2 | 3 | 4;
303
351
  * weekOfYear(new Date('2024-01-15')); // 3
304
352
  */
305
353
  declare function weekOfYear(date: Date | number): number;
354
+ /**
355
+ * Get the UTC offset in minutes for a specific IANA timezone at a given instant.
356
+ * Positive values mean ahead of UTC (e.g., +120 for UTC+2), negative behind.
357
+ *
358
+ * @param timeZone - IANA timezone identifier (e.g., 'America/New_York')
359
+ * @param date - Point in time to evaluate (default: now)
360
+ * @returns Offset in minutes from UTC
361
+ *
362
+ * @example
363
+ * ```typescript
364
+ * getTimezoneOffset('America/New_York'); // -300 (EST) or -240 (EDT)
365
+ * getTimezoneOffset('Europe/Berlin'); // +60 (CET) or +120 (CEST)
366
+ * ```
367
+ */
368
+ declare function getTimezoneOffset(timeZone: string, date?: Date | number): number;
369
+ /**
370
+ * Convert a Date to the wall-clock components in a specific timezone.
371
+ * Returns an object with year, month (1-12), day, hour, minute, second, millisecond.
372
+ *
373
+ * @param date - Date to convert
374
+ * @param timeZone - IANA timezone identifier
375
+ * @returns Date components in the target timezone
376
+ *
377
+ * @example
378
+ * ```typescript
379
+ * const utcNoon = new Date('2024-06-15T12:00:00Z');
380
+ * toTimeZone(utcNoon, 'America/New_York');
381
+ * // { year: 2024, month: 6, day: 15, hour: 8, minute: 0, second: 0, millisecond: 0 }
382
+ * ```
383
+ */
384
+ declare function toTimeZone(date: Date | number, timeZone: string): {
385
+ year: number;
386
+ month: number;
387
+ day: number;
388
+ hour: number;
389
+ minute: number;
390
+ second: number;
391
+ millisecond: number;
392
+ };
393
+ /**
394
+ * Format a Date in a specific timezone using common format patterns.
395
+ * Supports 'yyyy-MM-dd', 'yyyy-MM-dd HH:mm:ss', and Intl-based formatting.
396
+ *
397
+ * @param date - Date to format
398
+ * @param timeZone - IANA timezone identifier
399
+ * @param format - Format pattern (default: 'yyyy-MM-dd HH:mm:ss')
400
+ * @param locale - Locale for Intl formatting
401
+ * @returns Formatted date string in the target timezone
402
+ *
403
+ * @example
404
+ * ```typescript
405
+ * const utcDate = new Date('2024-06-15T18:30:00Z');
406
+ * formatDateInTimeZone(utcDate, 'America/New_York', 'yyyy-MM-dd HH:mm:ss');
407
+ * // '2024-06-15 14:30:00'
408
+ *
409
+ * formatDateInTimeZone(utcDate, 'Asia/Tokyo', 'yyyy-MM-dd');
410
+ * // '2024-06-16'
411
+ * ```
412
+ */
413
+ declare function formatDateInTimeZone(date: Date | number, timeZone: string, format?: string, locale?: string | string[]): string;
414
+ /**
415
+ * Check if a given IANA timezone is currently observing DST.
416
+ *
417
+ * @param timeZone - IANA timezone identifier
418
+ * @param date - Point in time to check (default: now)
419
+ * @returns True if the timezone is in DST at the given moment
420
+ *
421
+ * @example
422
+ * ```typescript
423
+ * isDST('America/New_York', new Date('2024-07-01')); // true (EDT)
424
+ * isDST('America/New_York', new Date('2024-01-01')); // false (EST)
425
+ * isDST('Asia/Tokyo'); // false (no DST)
426
+ * ```
427
+ */
428
+ declare function isDST(timeZone: string, date?: Date | number): boolean;
429
+ /**
430
+ * Get the IANA timezone abbreviation (e.g., "EST", "EDT", "CET") for a date.
431
+ *
432
+ * @param timeZone - IANA timezone identifier
433
+ * @param date - Point in time (default: now)
434
+ * @param locale - Locale for formatting (default: 'en-US')
435
+ * @returns Timezone abbreviation string
436
+ *
437
+ * @example
438
+ * ```typescript
439
+ * getTimezoneAbbreviation('America/New_York', new Date('2024-01-15')); // 'EST'
440
+ * getTimezoneAbbreviation('America/New_York', new Date('2024-07-15')); // 'EDT'
441
+ * ```
442
+ */
443
+ declare function getTimezoneAbbreviation(timeZone: string, date?: Date | number, locale?: string): string;
306
444
 
307
445
  /**
308
446
  * DateBuilder class for fluent date manipulation and building.
@@ -693,7 +831,41 @@ declare class DateBuilder {
693
831
  * Convert to string.
694
832
  */
695
833
  toString(): string;
834
+ /**
835
+ * Format the date in a specific timezone.
836
+ * @param timeZone - IANA timezone identifier (e.g., 'America/New_York')
837
+ * @param format - Format pattern (default: 'yyyy-MM-dd HH:mm:ss')
838
+ */
839
+ formatInTimeZone(timeZone: string, format?: string): string;
840
+ /**
841
+ * Get the wall-clock components in a specific timezone.
842
+ * @param timeZone - IANA timezone identifier
843
+ */
844
+ toTimeZone(timeZone: string): {
845
+ year: number;
846
+ month: number;
847
+ day: number;
848
+ hour: number;
849
+ minute: number;
850
+ second: number;
851
+ millisecond: number;
852
+ };
853
+ /**
854
+ * Get the UTC offset in minutes for a specific timezone at this date's instant.
855
+ * @param timeZone - IANA timezone identifier
856
+ */
857
+ getTimezoneOffset(timeZone: string): number;
858
+ /**
859
+ * Check if the specified timezone is observing DST at this date's instant.
860
+ * @param timeZone - IANA timezone identifier
861
+ */
862
+ isDST(timeZone: string): boolean;
863
+ /**
864
+ * Get the timezone abbreviation (e.g., 'EST', 'EDT') for a specific timezone.
865
+ * @param timeZone - IANA timezone identifier
866
+ */
867
+ getTimezoneAbbreviation(timeZone: string): string;
696
868
  }
697
869
 
698
- export { DateBuilder, addDays, addMonths, addToDate, addYears, dateDiff, daysInMonth, endOf, formatDate, formatDuration, formatRelativeTime, getDateFromDuration, isBetween, isFuture, isLeapYear, isPast, isToday, isWeekend, parseDate, parseDuration, quarterOf, startOf, weekOfYear };
699
- export type { DateFormatOptions };
870
+ export { DateBuilder, addDays, addMonths, addToDate, addYears, dateDiff, dateDiffDaysTZ, daysInMonth, endOf, formatDate, formatDateInTimeZone, formatDuration, formatRelativeTime, getDateFromDuration, getTimezoneAbbreviation, getTimezoneOffset, isBetween, isDST, isFuture, isLeapYear, isPast, isToday, isWeekend, parseDate, parseDuration, quarterOf, startOf, toTimeZone, weekOfYear };
871
+ export type { DateFormatOptions, DateFormatPreset };