@dereekb/date 10.1.30 → 10.2.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.
@@ -1,4 +1,4 @@
1
- import { type DayOfWeekNameFunction, type DateOrDateString, type ISO8601DateString, type Maybe, type Minutes, type Seconds, type TimezoneString, type ArrayOrValue, type MapFunction, type ISO8601DateStringUTCFull, type UTCDateString, type DayOfWeek, type UnixDateTimeNumber } from '@dereekb/util';
1
+ import { type DayOfWeekNameFunction, type DateOrDateString, type ISO8601DateString, type Maybe, type Minutes, type Seconds, type TimezoneString, type ArrayOrValue, type MapFunction, type ISO8601DateStringUTCFull, type UTCDateString, type DayOfWeek, type UnixDateTimeNumber, type DateOrUnixDateTimeNumber, type DateHourMinuteOrSecond, type FloorOrCeilRounding } from '@dereekb/util';
2
2
  export declare const MAX_FUTURE_DATE: Date;
3
3
  /**
4
4
  * Reads a date from the input value
@@ -108,18 +108,26 @@ export declare function utcDayForDate(date: Date): Date;
108
108
  * For example, if it is currently 9PM:
109
109
  * - if 10PM on any day is passed then 9PM the next day will be returned.
110
110
  * - if 11PM on any day is passed, 11PM today will be returned.
111
+ *
112
+ * @deprecated Fails in the rare case where it is the first two hours of a day in a daylight savings zone when daylight savings changes.
111
113
  */
112
114
  export declare function takeNextUpcomingTime(date: Date, roundDownToMinute?: boolean): Date;
113
115
  /**
114
116
  * Creates a new date and copies the hours/minutes from the previous date and applies them to a date for today.
117
+ *
118
+ * @deprecated Fails in the rare case where it is the first two hours of a day in a daylight savings zone when daylight savings changes.
115
119
  */
116
120
  export declare function copyHoursAndMinutesFromDateToToday(fromDate: Date, roundDownToMinute?: boolean): Date;
117
121
  /**
118
122
  * Copies the hours/minutes from now to the target date.
123
+ *
124
+ * @deprecated Fails in the rare case where it is the first two hours of a day in a daylight savings zone when daylight savings changes.
119
125
  */
120
126
  export declare function copyHoursAndMinutesFromNow(target: Date, roundDownToMinute?: boolean): Date;
121
127
  /**
122
128
  * Creates a new date and copies the hours/minutes from the input date to the target date.
129
+ *
130
+ * @deprecated Fails in the rare case where it is the first two hours of a day in a daylight savings zone when daylight savings changes.
123
131
  */
124
132
  export declare function copyHoursAndMinutesFromDate(target: Date, fromDate: Date, roundDownToMinute?: boolean): Date;
125
133
  /**
@@ -143,11 +151,22 @@ export declare const copyHoursAndMinutesToToday: typeof copyHoursAndMinutesToDat
143
151
  */
144
152
  export declare function roundDownToMinute(date?: Date): Date;
145
153
  /**
146
- * Removes all minutes,
154
+ * Removes the minutes, seconds and milliseconds from the input date, or returns the current date with no mkinutes, seconds or milliseconds.
155
+ */
156
+ export declare function roundDownToHour(date?: Date): Date;
157
+ export declare function roundDateDownTo(date: Date, roundToUnit: DateHourMinuteOrSecond): Date;
158
+ export declare function roundDateTo(date: Date, roundToUnit: DateHourMinuteOrSecond, roundType?: FloorOrCeilRounding): Date;
159
+ export declare function roundDateTo(unixDateTimeNumber: UnixDateTimeNumber, roundToUnit: DateHourMinuteOrSecond, roundType?: FloorOrCeilRounding): UnixDateTimeNumber;
160
+ export declare function roundDateToDate(date: DateOrUnixDateTimeNumber, roundToUnit: DateHourMinuteOrSecond, roundType?: FloorOrCeilRounding): Date;
161
+ /**
162
+ * Rounds the input Date value down to the nearest hour, minute, or second.
163
+ *
147
164
  * @param date
165
+ * @param roundToUnit
166
+ * @param roundType
148
167
  * @returns
149
168
  */
150
- export declare function roundDownToHour(date: Date): Date;
169
+ export declare function roundDateToUnixDateTimeNumber(date: DateOrUnixDateTimeNumber, roundToUnit: DateHourMinuteOrSecond, roundType?: FloorOrCeilRounding): UnixDateTimeNumber;
151
170
  export type ReduceDatesFunction = (inputDates: ArrayOrValue<Maybe<Date>>) => Maybe<Date>;
152
171
  export declare function reduceDatesFunction(reduceDates: (dates: Date[]) => Maybe<Date>): ReduceDatesFunction;
153
172
  /**
@@ -1,4 +1,4 @@
1
- import { type MapFunction, type Maybe, type Milliseconds, type TimezoneString, type ISO8601DayString, type YearNumber, type MapSameFunction, type Hours } from '@dereekb/util';
1
+ import { type MapFunction, type Maybe, type Milliseconds, type TimezoneString, type ISO8601DayString, type YearNumber, type MapSameFunction, type Hours, type Minutes, type LogicalDate } from '@dereekb/util';
2
2
  import { type DateRange, type TransformDateRangeDatesFunction } from './date.range';
3
3
  /**
4
4
  * Inherited from the RRule library where RRule only deals with UTC date/times, dates going into it must always be in UTC.
@@ -72,31 +72,36 @@ export declare function isSameDateTimezoneConversionConfig(a: DateTimezoneConver
72
72
  * @param date Date is required to get the correct offset for the given date.
73
73
  * @returns
74
74
  */
75
- export declare function getCurrentSystemOffsetInMs(date: Date): number;
75
+ export declare function getCurrentSystemOffsetInMs(date: Date): Milliseconds;
76
76
  /**
77
77
  * Returns the current system time offset in hours.
78
78
  *
79
79
  * @param date
80
80
  * @returns
81
81
  */
82
- export declare function getCurrentSystemOffsetInHours(date: Date): number;
82
+ export declare function getCurrentSystemOffsetInHours(date: Date): Hours;
83
83
  /**
84
- * Equivalent to -date.getTimezoneOffset().
84
+ * Returns the system offset for the input date, in minutes.
85
+ *
86
+ * The offset corresponds positively with the UTC offset, so UTC-6 is negative 6 hours, in milliseconds.
85
87
  *
86
88
  * @param date
87
89
  * @returns
88
90
  */
89
- export declare function getCurrentSystemOffsetInMinutes(date: Date): number;
91
+ export declare function getCurrentSystemOffsetInMinutes(date: Date): Minutes;
90
92
  /**
91
93
  * Returns the timezone offset in milliseconds.
92
94
  *
95
+ * This is preferential to Date.getTimezoneOffset() or date-fns's getTimezoneOffset() as those are currently wrong for the first
96
+ * two hours when daylight savings changes.
97
+ *
93
98
  * I.E. GMT-5 = -5 hours (in milliseconds)
94
99
  *
95
100
  * @param timezone
96
101
  * @param date
97
102
  * @returns
98
103
  */
99
- export declare function calculateTimezoneOffset(timezone: TimezoneString, date: Date): number;
104
+ export declare function calculateTimezoneOffset(timezone: TimezoneString, date: Date): Milliseconds;
100
105
  export type DateTimezoneConversionTarget = 'target' | 'base' | 'system';
101
106
  export type DateTimezoneOffsetFunction = (date: Date, from: DateTimezoneConversionTarget, to: DateTimezoneConversionTarget) => Milliseconds;
102
107
  export interface DateTimezoneBaseDateConverter {
@@ -155,8 +160,26 @@ export declare class DateTimezoneUtcNormalInstance implements DateTimezoneBaseDa
155
160
  get usesSystemTimezone(): boolean;
156
161
  get configuredTimezoneString(): Maybe<TimezoneString>;
157
162
  private readonly _getOffset;
163
+ private readonly _setOnDate;
158
164
  constructor(config: DateTimezoneUtcNormalInstanceInput);
159
- private _computeOffsetDate;
165
+ convertDate(date: Date, from: DateTimezoneConversionTarget, to: DateTimezoneConversionTarget): Date;
166
+ /**
167
+ * A "safer" conversion that will return a "mirrored" offset. Only functional with a "to" UTC value.
168
+ *
169
+ * This is required in cases where "reverse" offset will be used and must be consistent so they reverse in both directions the same amount compared to the base.
170
+ *
171
+ * For example, when daylight savings changed on November 3, 2024 the offset returned was 5 but to get back to the original an offset of 6 was required.
172
+ * This is where some contextual data was not being used. This function uses that contextual data to make sure the reverse will be consistent.
173
+ *
174
+ * @param baseDate The base date. Should have been derived from the originalContextDate using the convertDate() function
175
+ * @param originalContextDate Original date used to derive the baseDate.
176
+ * @param fromOrTo the "type" of date the originalContextDate is
177
+ */
178
+ safeMirroredConvertDate(baseDate: BaseDateAsUTC, originalContextDate: Date, contextType: DateTimezoneConversionTarget, safeConvert?: boolean): {
179
+ date: Date;
180
+ daylightSavingsOffset: number;
181
+ };
182
+ get setOnDate(): SetOnDateWithTimezoneNormalFunction;
160
183
  getCurrentOffset(date: Date, from: DateTimezoneConversionTarget, to: DateTimezoneConversionTarget): number;
161
184
  transform(date: Date, transform: DateTimezoneUtcNormalInstanceTransformType): Date;
162
185
  transformFunction(transform: DateTimezoneUtcNormalInstanceTransformType): MapFunction<Date, Date>;
@@ -176,6 +199,7 @@ export declare class DateTimezoneUtcNormalInstance implements DateTimezoneBaseDa
176
199
  systemDateToBaseDateOffset(date: Date): Milliseconds;
177
200
  targetDateToSystemDateOffset(date: Date): Milliseconds;
178
201
  systemDateToTargetDateOffset(date: Date): Milliseconds;
202
+ conversionOffset(date: Date, from: DateTimezoneConversionTarget, to: DateTimezoneConversionTarget): number;
179
203
  calculateAllOffsets<T = number>(date: Date, map?: DateTimezoneConversionFunction<T>): DateTimezoneConversionMap<T>;
180
204
  /**
181
205
  * Returns true if the input is midnight in the target timezone.
@@ -292,6 +316,68 @@ export declare function startOfDayInTimezoneDayStringFactory(timezone?: DateTime
292
316
  * @returns
293
317
  */
294
318
  export declare function startOfDayInTimezoneFromISO8601DayString(day: ISO8601DayString, timezone?: DateTimezoneUtcNormalFunctionInput): Date;
319
+ export interface SetOnDateWithTimezoneNormalFunctionInput {
320
+ /**
321
+ * Date to update
322
+ *
323
+ * If not defined, will use "now".
324
+ */
325
+ readonly date?: Maybe<Date>;
326
+ /**
327
+ * The input date target type of the date and copyFrom values.
328
+ *
329
+ * Defaults to "target"
330
+ */
331
+ readonly inputType?: DateTimezoneConversionTarget;
332
+ /**
333
+ * The return date target type.
334
+ *
335
+ * Defaults to to the inputType, or to "target" if neither are defined.
336
+ */
337
+ readonly outputType?: DateTimezoneConversionTarget;
338
+ /**
339
+ * Hours to set
340
+ */
341
+ readonly hours?: Maybe<number>;
342
+ /**
343
+ * Minutes to set
344
+ */
345
+ readonly minutes?: Maybe<number>;
346
+ /**
347
+ * (Optional) date value to copy from.
348
+ *
349
+ * If hours or minutes are set, those values take priority over the value read from this.
350
+ */
351
+ readonly copyFrom?: Maybe<LogicalDate>;
352
+ /**
353
+ * If true, will copy the hours from the input date.
354
+ *
355
+ * Defaults to true.
356
+ */
357
+ readonly copyHours?: Maybe<boolean>;
358
+ /**
359
+ * If true, will copy the minutes from the input date.
360
+ *
361
+ * Defaults to true.
362
+ */
363
+ readonly copyMinutes?: Maybe<boolean>;
364
+ /**
365
+ * Whether or not to round down to the nearest minute.
366
+ *
367
+ * Defaults to false.
368
+ */
369
+ readonly roundDownToMinute?: Maybe<boolean>;
370
+ }
371
+ /**
372
+ * Sets the input values on the input date.
373
+ */
374
+ export type SetOnDateWithTimezoneNormalFunction = ((input: SetOnDateWithTimezoneNormalFunctionInput) => Date) & {
375
+ readonly _timezoneInstance: DateTimezoneUtcNormalInstance;
376
+ };
377
+ /**
378
+ * Creates a new SetONDateFunction using the input
379
+ */
380
+ export declare function setOnDateWithTimezoneNormalFunction(timezone: DateTimezoneUtcNormalFunctionInput): SetOnDateWithTimezoneNormalFunction;
295
381
  /**
296
382
  * Convenience function for calling copyHoursAndMinutesFromDatesWithTimezoneNormal() with now.
297
383
  *
@@ -301,7 +387,7 @@ export declare function startOfDayInTimezoneFromISO8601DayString(day: ISO8601Day
301
387
  */
302
388
  export declare function copyHoursAndMinutesFromNowWithTimezoneNormal(input: Date, timezone: DateTimezoneUtcNormalFunctionInput): Date;
303
389
  /**
304
- * Converts the two input dates, which are dates in the same timezone/normal but than the current system, using the input DateTimezoneUtcNormalFunctionInput.
390
+ * Converts the two input dates, which are dates in the same timezone/normal instead of the current system, using the input DateTimezoneUtcNormalFunctionInput.
305
391
  *
306
392
  * This converts the dates to the system timezone normal, copies the values, then back to the original timezone normal.
307
393
  *
@@ -309,4 +395,4 @@ export declare function copyHoursAndMinutesFromNowWithTimezoneNormal(input: Date
309
395
  * @param copyFrom
310
396
  * @param timezone
311
397
  */
312
- export declare function copyHoursAndMinutesFromDateWithTimezoneNormal(input: Date, copyFrom: Date, timezone: DateTimezoneUtcNormalFunctionInput): Date;
398
+ export declare function copyHoursAndMinutesFromDateWithTimezoneNormal(input: Date, copyFrom: LogicalDate, timezone: DateTimezoneUtcNormalFunctionInput): Date;