@jsb188/app 1.1.4 → 1.1.6

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/utils/datetime.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import { DateTime } from 'luxon';
2
2
  import i18n from '../i18n';
3
+ import { getObject } from './object';
3
4
  import { isValidTimeZone, parseDateInTimezone } from './timeZone';
4
5
 
5
6
  /**
@@ -32,17 +33,6 @@ interface TimeAgoParams {
32
33
  skipTodayCheck: boolean;
33
34
  }
34
35
 
35
- interface FullTimeParams {
36
- timeZone: string | null;
37
- locales: string;
38
- hideYear: boolean;
39
- hideDate: boolean;
40
- hideTime: boolean;
41
- textDateStyle: 'long' | 'short' | null;
42
- alwaysShowYear: boolean;
43
- includeWeekday: boolean;
44
- }
45
-
46
36
  /**
47
37
  * Get start of day
48
38
  */
@@ -59,10 +49,44 @@ function getDaysDiff(date1: Date, date2: Date) {
59
49
  return Math.floor((getStartOfDay(date1).getTime() - getStartOfDay(date2).getTime()) / (1000 * 60 * 60 * 24));
60
50
  }
61
51
 
52
+ /**
53
+ * Get difference in weeks
54
+ */
55
+
56
+ function getWeeksDiff(date1: Date, date2: Date) {
57
+ return Math.floor(getDaysDiff(date1, date2) / 7);
58
+ }
59
+
60
+ /**
61
+ * Get difference in months
62
+ */
63
+
64
+ function getMonthsDiff(date1: Date, date2: Date) {
65
+ return (date1.getFullYear() - date2.getFullYear()) * 12 + (date1.getMonth() - date2.getMonth());
66
+ }
67
+
62
68
  /**
63
69
  * Get date period from date
64
70
  */
65
71
 
72
+ export function getWeeksMonthAgo(d1: Date, d2: Date, weeksThresh = 12): string {
73
+ const diffWeeks = getWeeksDiff(d2, d1);
74
+ if (diffWeeks === 0) {
75
+ return i18n.t('datetime.period_THIS_WEEK');
76
+ } else if (diffWeeks <= weeksThresh) {
77
+ return i18n.t('datetime.weeks_ago_ct', { smart_count: diffWeeks });
78
+ }
79
+
80
+ const diffMonths = getMonthsDiff(d2, d1);
81
+ return i18n.t('datetime.months_ago_ct', { smart_count: diffMonths });
82
+ }
83
+
84
+ /**
85
+ * Get date period from date
86
+ * @param d1 - Date to check
87
+ * @param d2 - Reference date (usually current date)
88
+ */
89
+
66
90
  export function getDatePeriod(d1: Date, d2: Date): DatePeriodEnum {
67
91
  const diffDays = getDaysDiff(d1, d2);
68
92
 
@@ -105,8 +129,18 @@ export function getDatePeriod(d1: Date, d2: Date): DatePeriodEnum {
105
129
  */
106
130
 
107
131
  export function getFullDateTime(
108
- _d: Date | string | number | null,
109
- params?: Partial<FullTimeParams>,
132
+ d_: Date | string | number | null,
133
+ params?: Partial<{
134
+ timeZone: string | null;
135
+ locales: string;
136
+ hideYear: boolean;
137
+ hideDate: boolean;
138
+ hideTime: boolean;
139
+ textDateStyle: 'long' | 'short' | 'numeric' | null;
140
+ militaryTime: boolean;
141
+ alwaysShowYear: boolean;
142
+ includeWeekday: boolean | 'long' | 'short' | 'narrow';
143
+ }>,
110
144
  ) {
111
145
  const {
112
146
  locales = 'en-US',
@@ -115,16 +149,17 @@ export function getFullDateTime(
115
149
  hideDate,
116
150
  hideTime,
117
151
  textDateStyle,
152
+ militaryTime,
118
153
  alwaysShowYear,
119
154
  includeWeekday,
120
155
  } = params || {};
121
156
 
122
157
  let d;
123
- if (_d instanceof Date) {
124
- d = _d;
158
+ if (d_ instanceof Date) {
159
+ d = d_;
125
160
  } else {
126
- // Make sure _d is not null, before using this function
127
- d = _d ? new Date(_d) : new Date(-1);
161
+ // Make sure d_ is not null, before using this function
162
+ d = d_ ? new Date(d_) : new Date(-1);
128
163
  }
129
164
 
130
165
  let timeZone = timeZone_ || undefined; // cannot be null, else it will throw error
@@ -135,8 +170,9 @@ export function getFullDateTime(
135
170
  if (hideDate) {
136
171
  return d.toLocaleTimeString(locales, {
137
172
  timeZone,
138
- hour: 'numeric',
139
- minute: '2-digit',
173
+ hour: hideTime ? undefined : militaryTime ? '2-digit' : 'numeric',
174
+ minute: hideTime ? undefined : '2-digit',
175
+ hour12: !militaryTime,
140
176
  });
141
177
  }
142
178
 
@@ -150,14 +186,22 @@ export function getFullDateTime(
150
186
  hideYear_ = true;
151
187
  }
152
188
 
189
+ let weekday;
190
+ if (['long', 'short', 'narrow'].includes(includeWeekday as string)) {
191
+ weekday = includeWeekday;
192
+ } else if (includeWeekday) {
193
+ weekday = textDateStyle === 'long' ? 'long' : 'short';
194
+ }
195
+
153
196
  return d.toLocaleDateString(locales, {
154
197
  timeZone,
155
198
  day: 'numeric',
156
- weekday: includeWeekday && textDateStyle === 'long' ? 'long' : includeWeekday ? 'short' : undefined,
199
+ weekday: weekday as 'long' | 'short' | 'narrow',
157
200
  month: textDateStyle || 'numeric',
158
201
  year: hideYear_ ? undefined : 'numeric',
159
- hour: hideTime ? undefined : 'numeric',
202
+ hour: hideTime ? undefined : militaryTime ? '2-digit' : 'numeric',
160
203
  minute: hideTime ? undefined : '2-digit',
204
+ hour12: !militaryTime,
161
205
  });
162
206
  }
163
207
 
@@ -194,7 +238,10 @@ export function getTodayStartOfDay(timeZone?: string | null) {
194
238
  }
195
239
 
196
240
  /**
197
- * Get timezone enforced date
241
+ * Get timezone enforced calendar date
242
+ * @param d - Date to convert
243
+ * @param timeZone - Timezone to enforce
244
+ * @returns Date - Enforced calendar date in the specified timezone
198
245
  */
199
246
 
200
247
  export function getCalDate(d: Date, timeZone_?: string | null) {
@@ -218,17 +265,123 @@ export function getCalDate(d: Date, timeZone_?: string | null) {
218
265
  };
219
266
  }
220
267
 
268
+ /**
269
+ * Get timezone enforced calendar date; but month-day first
270
+ * @param d - Date to convert
271
+ * @param timeZone - Timezone to enforce
272
+ * @returns Date - Enforced calendar date in the specified timezone
273
+ */
274
+
275
+ export function getReadableCalDate(d_: string | Date, timeZone?: string | null) {
276
+ let d;
277
+ if (typeof d_ === 'string' && /^\d{4}-\d{2}-\d{2}$/.test(d_)) {
278
+ // Assume YYYY-MM-DD format
279
+ const [year, month, day] = d_.split('-').map(Number);
280
+ d = new Date(year, month - 1, day);
281
+ } else if (d_ instanceof Date) {
282
+ d = d_;
283
+ } else {
284
+ d = d_ && new Date(d_);
285
+ }
286
+
287
+ if (!d || isNaN(d.getTime())) {
288
+ return null;
289
+ }
290
+
291
+ let dt = DateTime.fromJSDate(d, { zone: timeZone || undefined });
292
+ // If timezone is invalid, fallback to system/local
293
+ if (!dt.isValid) {
294
+ dt = DateTime.fromJSDate(d); // system default zone
295
+ }
296
+
297
+ if (!dt.isValid) {
298
+ return null;
299
+ }
300
+
301
+ return dt.toFormat('MM/dd/yyyy');
302
+ }
303
+
304
+ /**
305
+ * Update JS date with year/month/day and hour/minutes
306
+ */
307
+
308
+ export function updateDate(
309
+ d_: Date | string,
310
+ update: Partial<{
311
+ year: number;
312
+ month: number;
313
+ day: number;
314
+ hour: number;
315
+ minute: number;
316
+ }>,
317
+ timeZone?: string | null,
318
+ ): Date | null {
319
+ if (timeZone) {
320
+ // Use luxon to handle timezone-aware date manipulation
321
+ let dt = DateTime.fromJSDate(d_ instanceof Date ? d_ : new Date(d_), { zone: timeZone });
322
+
323
+ if (update.year !== undefined) {
324
+ dt = dt.set({ year: update.year });
325
+ }
326
+ if (update.month !== undefined) {
327
+ dt = dt.set({ month: update.month });
328
+ }
329
+ if (update.day !== undefined) {
330
+ dt = dt.set({ day: update.day });
331
+ }
332
+ if (update.hour !== undefined) {
333
+ dt = dt.set({ hour: update.hour });
334
+ }
335
+ if (update.minute !== undefined) {
336
+ dt = dt.set({ minute: update.minute });
337
+ }
338
+
339
+ return dt.isValid ? dt.toJSDate() : null;
340
+ }
341
+
342
+ const d = d_ instanceof Date ? d_ : new Date(d_);
343
+
344
+ if (update.year) {
345
+ d.setFullYear(update.year);
346
+ }
347
+
348
+ if (update.month !== undefined) {
349
+ d.setMonth(update.month - 1); // JS months are 0-indexed
350
+ }
351
+
352
+ if (update.day) {
353
+ d.setDate(update.day);
354
+ }
355
+
356
+ if (update.hour !== undefined) {
357
+ d.setHours(update.hour);
358
+ }
359
+
360
+ if (update.minute !== undefined) {
361
+ d.setMinutes(update.minute);
362
+ }
363
+
364
+ return d;
365
+ }
366
+
221
367
  /**
222
368
  * Convert Int cal date to String YYYY-MM-DD format
223
369
  */
224
370
 
225
- export function convertIntToCalDate(calDateInt: number | string): string {
226
- const value = String(calDateInt);
227
- if (value.length === 8) {
228
- return `${value.slice(0, 4)}-${value.slice(4, 6)}-${value.slice(6, 8)}`;
229
- }
230
- // If not 8 digits, return as is
231
- return value;
371
+ export function convertIntToCalDate(
372
+ calDateInt: number | string,
373
+ delimiter: string = '-',
374
+ yearsFirst: boolean = true,
375
+ ): string {
376
+ const value = String(calDateInt);
377
+ if (value.length === 8) {
378
+ if (yearsFirst) {
379
+ return `${value.slice(0, 4)}${delimiter}${value.slice(4, 6)}${delimiter}${value.slice(6, 8)}`;
380
+ }
381
+ return `${value.slice(4, 6)}${delimiter}${value.slice(6, 8)}${delimiter}${value.slice(0, 4)}`;
382
+ }
383
+ // If not 8 digits, return as is
384
+ return value;
232
385
  }
233
386
 
234
387
  /**
@@ -239,9 +392,21 @@ export function convertIntToCalDate(calDateInt: number | string): string {
239
392
  */
240
393
 
241
394
  export function isValidCalDate(
242
- calDate: string | number,
395
+ calDate_: string | number,
396
+ allowDashes: boolean = false,
243
397
  minCalDate: number = 19500101,
244
398
  ): boolean {
399
+ let calDate;
400
+ if (
401
+ allowDashes &&
402
+ typeof calDate_ === 'string' &&
403
+ /^\d{4}-\d{2}-\d{2}$/.test(calDate_)
404
+ ) {
405
+ calDate = calDate_.replace(/-/g, '');
406
+ } else {
407
+ calDate = calDate_;
408
+ }
409
+
245
410
  const value = Number(calDate);
246
411
  if (!calDate || !Number.isInteger(value) || value < 10000000 || value > 90000000) {
247
412
  return false;
@@ -271,10 +436,12 @@ export function isFutureCalDate(
271
436
 
272
437
  /**
273
438
  * Convert date time "time" ago
439
+ * @param d_ - Date to convert
440
+ * @param params - Optional parameters for time ago conversion
274
441
  */
275
442
 
276
443
  export function getTimeAgo(
277
- _d: Date | string | number | null,
444
+ d_: Date | string | number | null,
278
445
  params?: Partial<TimeAgoParams>,
279
446
  ) {
280
447
  const {
@@ -287,13 +454,13 @@ export function getTimeAgo(
287
454
  } = params || {};
288
455
 
289
456
  let d;
290
- if (_d instanceof Date) {
291
- d = _d;
292
- } else if (!isNaN(Number(_d))) {
293
- d = new Date(Number(_d));
457
+ if (d_ instanceof Date) {
458
+ d = d_;
459
+ } else if (!isNaN(Number(d_))) {
460
+ d = new Date(Number(d_));
294
461
  } else {
295
462
  // Make sure date is not null, before using this function
296
- d = _d ? new Date(_d) : new Date(-1);
463
+ d = d_ ? new Date(d_) : new Date(-1);
297
464
  }
298
465
 
299
466
  const ts = d.getTime();
@@ -358,34 +525,112 @@ export function getTimeAgo(
358
525
  */
359
526
 
360
527
  export function getFullDate(
361
- _d: Date | string | number | null,
362
- outputStyle: 'DATE_ONLY' | 'DATE_TEXT' | 'MINIMAL' | 'DETAILED' = 'DATE_ONLY',
528
+ d_: Date | string | number | null,
529
+ outputStyle_:
530
+ | 'NUMERIC'
531
+ | 'NUMERIC_TIME'
532
+ | 'DATE_ONLY_SHORT'
533
+ | 'DAY_IF_WEEK'
534
+ | 'TOMORROW_OR_NUMERIC'
535
+ | 'DATE_TEXT'
536
+ | 'MINIMAL'
537
+ | 'DETAILED' = 'NUMERIC',
538
+ timeZone: string | null,
363
539
  locales: string = 'en-US',
364
540
  ) {
365
541
  let d;
366
- if (_d instanceof Date) {
367
- d = _d;
542
+ if (d_ instanceof Date) {
543
+ d = d_;
368
544
  } else {
369
- // Make sure _d is not null, before using this function
370
- d = _d ? new Date(_d) : new Date(-1);
545
+ // Make sure d_ is not null, before using this function
546
+ d = d_ ? new Date(d_) : new Date(-1);
547
+ }
548
+
549
+ let outputStyle;
550
+ if (['TOMORROW_OR_NUMERIC', 'DAY_IF_WEEK'].includes(outputStyle_)) {
551
+ const datePeriod = getDatePeriod(d, new Date());
552
+ if (['TODAY', 'TOMORROW'].includes(datePeriod)) {
553
+ return i18n.t(`datetime.period_${datePeriod}`);
554
+ }
555
+
556
+ const daysRemaining = getDaysDiff(d, new Date());
557
+ if (daysRemaining >= 0 && daysRemaining <= 6) {
558
+ // return "Monday", "Tuesday", etc.
559
+ const dayOfWeekText = d.toLocaleDateString(locales, {
560
+ timeZone: timeZone || undefined, // null is not allowed, it will throw error
561
+ ...(outputStyle_ === 'TOMORROW_OR_NUMERIC' ? {} : {
562
+ weekday: 'long',
563
+ }),
564
+ });
565
+ return dayOfWeekText;
566
+ }
567
+
568
+ outputStyle = 'NUMERIC';
569
+ } else {
570
+ outputStyle = outputStyle_;
371
571
  }
372
572
 
373
573
  switch (outputStyle) {
374
- case 'DATE_ONLY':
574
+ case 'NUMERIC':
375
575
  // Expected output: "9/1/2024"
376
- return new Intl.DateTimeFormat(locales).format(d);
576
+ return new Intl.DateTimeFormat(locales, {
577
+ timeZone: timeZone || undefined, // null is not allowed, it will throw error
578
+ }).format(d);
579
+ case 'NUMERIC_TIME':
580
+ // Expected output: "9/1/2024, 8:30 PM"
581
+ return new Intl.DateTimeFormat(locales, {
582
+ month: '2-digit',
583
+ day: '2-digit',
584
+ year: 'numeric',
585
+ hour: 'numeric',
586
+ minute: '2-digit',
587
+ hour12: true,
588
+ timeZone: timeZone || undefined, // null is not allowed, it will throw error
589
+ }).format(d);
377
590
  case 'MINIMAL':
378
591
  // Expected output: "Sep 1, 2024, 6:54 PM"
379
592
  return new Intl.DateTimeFormat(locales, {
380
593
  dateStyle: 'medium', // 'full', 'long', 'medium', 'short'
381
594
  timeStyle: 'short', // 'full', 'long', 'medium', 'short'
382
- // timeZone: 'Australia/Sydney',
595
+ timeZone: timeZone || undefined, // null is not allowed, it will throw error
383
596
  }).format(d);
597
+ // return DateTime.fromJSDate(d, timeZone ? { zone: timeZone } : undefined).toFormat('MMM d yyyy, h:mm a');
598
+ case 'DATE_ONLY_SHORT': {
599
+ // Expected output: "Sep 1" (or "Sep 1, 25" if not current year)
600
+ const dateYear = d.getFullYear();
601
+ const currentYear = new Date().getFullYear();
602
+
603
+ // This will crash because "medium" style with "numeric" year is not allowed
604
+ // return new Intl.DateTimeFormat(locales, {
605
+ // dateStyle: 'medium',
606
+ // year: dateYear === currentYear ? undefined : 'numeric',
607
+ // timeZone: timeZone || undefined, // null is not allowed, it will throw error
608
+ // }).format(d);
609
+
610
+ if (dateYear === currentYear) {
611
+ return new Intl.DateTimeFormat(locales, {
612
+ year: undefined,
613
+ month: 'short',
614
+ day: 'numeric',
615
+ timeZone: timeZone || undefined, // null is not allowed, it will throw error
616
+ }).format(d);
617
+ }
618
+
619
+ return new Intl.DateTimeFormat(locales, {
620
+ // Use this if you want 4 digit years
621
+ // dateStyle: 'medium',
622
+ // Use this if you want 2 digit years
623
+ year: '2-digit',
624
+ month: 'short',
625
+ day: 'numeric',
626
+ timeZone: timeZone || undefined, // null is not allowed, it will throw error
627
+ }).format(d).replace(', ', ", '");
628
+ }
384
629
  case 'DATE_TEXT':
385
630
  // Expected output: "September 1, 2024"
386
631
  return new Intl.DateTimeFormat(locales, {
387
632
  dateStyle: 'long',
388
- // timeZone: 'Australia/Sydney',
633
+ timeZone: timeZone || undefined, // null is not allowed, it will throw error
389
634
  }).format(d);
390
635
  case 'DETAILED':
391
636
  default:
@@ -395,7 +640,7 @@ export function getFullDate(
395
640
  const date = new Intl.DateTimeFormat(locales, {
396
641
  dateStyle: 'long', // 'full', 'long', 'medium', 'short'
397
642
  timeStyle: 'short', // 'full', 'long', 'medium', 'short'
398
- // timeZone: 'Australia/Sydney',
643
+ timeZone: timeZone || undefined, // null is not allowed, it will throw error
399
644
  }).format(d);
400
645
 
401
646
  const parts = new Intl.DateTimeFormat(locales, {
@@ -433,9 +678,13 @@ export function getDayPeriod(): 'MORNING' | 'AFTERNOON' | 'EVENING' | 'NIGHT' {
433
678
  * Convert YYYY-MM-DD to Date
434
679
  */
435
680
 
436
- export function getDateFromCalDate(calDate: string) {
437
- const [year, month, day] = calDate.split('-');
438
- return new Date(Number(year), Number(month) - 1, Number(day));
681
+ export function getDateFromCalDate(value: string | number, timeZone?: string | null): Date {
682
+ const calDate = typeof value === 'number' ? convertIntToCalDate(value) : value;
683
+ const dt = DateTime.fromISO(calDate, timeZone ? { zone: timeZone } : undefined);
684
+ const jsDate = dt.toJSDate();
685
+ // const [year, month, day] = calDate.split('-');
686
+ // console.log(new Date(Number(year), Number(month) - 1, Number(day)));
687
+ return jsDate;
439
688
  }
440
689
 
441
690
  /**
@@ -450,14 +699,14 @@ export function getDateFromCalDate(calDate: string) {
450
699
 
451
700
  export function groupByDatePeriod<T>(
452
701
  items: T[],
453
- dateKey: string,
702
+ dateKey: string, // "." delimited getter is allowed here
454
703
  groupByDate?: boolean,
455
704
  timeZone?: string | null,
456
705
  mapFn?: (item: any) => any,
457
706
  ): DatePeriodObj[][] {
458
707
  const ordered = items.sort((a: any, b: any) => {
459
- const valueA = a[dateKey];
460
- const valueB = b[dateKey];
708
+ const valueA = getObject(a, dateKey);
709
+ const valueB = getObject(b, dateKey);
461
710
 
462
711
  if (valueA < valueB) {
463
712
  return 1;
@@ -480,7 +729,7 @@ export function groupByDatePeriod<T>(
480
729
 
481
730
  const lastItem = group[group.length - 1];
482
731
  const lastDatePeriod: any = lastItem?.datePeriod;
483
- const date = new Date(item[dateKey]);
732
+ const date = new Date(getObject(item, dateKey));
484
733
  const datePeriod = groupByDate ? getCalDate(date, timeZone).calDate : getDatePeriod(date, d);
485
734
 
486
735
  const dateObj = {
@@ -521,7 +770,7 @@ export function labelSortByDatePeriod<T>(
521
770
  timeZone?: string | null,
522
771
  mapFn?: (item: any) => any,
523
772
  ): DatePeriodObj[] {
524
- const ordered = items.sort((a: any, b: any) => {
773
+ const ordered: any[] = items.sort((a: any, b: any) => {
525
774
  const valueA = a[dateKey];
526
775
  const valueB = b[dateKey];
527
776
 
@@ -563,7 +812,7 @@ export function labelSortByDatePeriod<T>(
563
812
  output.push(dateObj);
564
813
  }
565
814
 
566
- return output;
815
+ return output as DatePeriodObj[];
567
816
  }
568
817
 
569
818
  /**
@@ -580,3 +829,115 @@ export function isValidDate(d?: Date | string | number | null) {
580
829
  }
581
830
  return false;
582
831
  }
832
+
833
+ /**
834
+ * Get HHMM in increments
835
+ */
836
+
837
+ export function getHHMMIncrements(
838
+ d: Date,
839
+ minutesIncrement: number,
840
+ utc: boolean = false,
841
+ ) {
842
+ let hh, minutes;
843
+ if (utc) {
844
+ hh = d.getUTCHours().toString().padStart(2, '0');
845
+ minutes = d.getUTCMinutes();
846
+ } else {
847
+ hh = d.getHours().toString().padStart(2, '0');
848
+ minutes = d.getMinutes();
849
+ }
850
+
851
+ const interval = Math.floor(minutes / minutesIncrement) * minutesIncrement;
852
+ const hhmm = `${hh}${interval.toString().padStart(2, '0')}`;
853
+
854
+ return hhmm;
855
+ }
856
+
857
+ /**
858
+ * Get color indicator based on expiration date
859
+ * @param expirationDate - Expiration date in YYYY-MM-DD format or Date object
860
+ * @returns 'red' | 'yellow' | 'green' | null
861
+ */
862
+
863
+ export function getExpirationColor(
864
+ expirationDate: string | Date, // YYYY-MM-DD format or Date object
865
+ validColor: 'green' | null = null,
866
+ ): 'red' | 'yellow' | 'green' | null {
867
+ let expDate: Date;
868
+ if (expirationDate instanceof Date) {
869
+ expDate = expirationDate;
870
+ } else {
871
+ expDate = new Date(expirationDate);
872
+ }
873
+
874
+ if (!isValidDate(expDate)) {
875
+ return 'red'; // Invalid date
876
+ }
877
+
878
+ const now = new Date();
879
+ const diff = expDate.getTime() - now.getTime();
880
+ const diffDays = Math.ceil(diff / (1000 * 3600 * 24));
881
+
882
+ if (diffDays < 0) {
883
+ return 'red'; // Expired
884
+ } else if (diffDays <= 30) {
885
+ return 'yellow'; // Expiring soon
886
+ }
887
+
888
+ return validColor; // Valid
889
+ }
890
+
891
+ /**
892
+ * Get HHMM time from "hh:mm" string
893
+ */
894
+
895
+ export function timeToHHMM(timeStr: string): string {
896
+ const [hh, mm] = timeStr.split(':');
897
+ if (hh && mm) {
898
+ return `${hh.padStart(2, '0')}${mm.padStart(2, '0')}`;
899
+ }
900
+ return timeStr.padStart(4, '0');
901
+ }
902
+
903
+ /**
904
+ * Sort & group by date/month
905
+ */
906
+
907
+ export function groupByDateMonth<T extends { date: Date }>(
908
+ items: T[],
909
+ timeZone: string
910
+ ): {
911
+ byDate: Record<string, T[]>;
912
+ byMonth: Record<string, T[]>;
913
+ } {
914
+ // 1. Sort by absolute time
915
+ const sorted = [...items].sort(
916
+ (a, b) => a.date.getTime() - b.date.getTime()
917
+ );
918
+
919
+ const byDate: Record<string, T[]> = {};
920
+ const byMonth: Record<string, T[]> = {};
921
+
922
+ for (const item of sorted) {
923
+ const dt = DateTime
924
+ .fromJSDate(item.date, { zone: timeZone });
925
+
926
+ const dateKey = dt.toFormat('yyyy-MM-dd');
927
+ const monthKey = dt.toFormat('yyyy-MM');
928
+
929
+ // Group by day
930
+ if (!byDate[dateKey]) {
931
+ byDate[dateKey] = [];
932
+ }
933
+ byDate[dateKey].push(item);
934
+
935
+ // Group by month
936
+ if (!byMonth[monthKey]) {
937
+ byMonth[monthKey] = [];
938
+ }
939
+ byMonth[monthKey].push(item);
940
+ }
941
+
942
+ return { byDate, byMonth };
943
+ }