@trackunit/date-and-time-utils 0.0.33 → 0.0.34

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.
Files changed (3) hide show
  1. package/index.cjs.js +137 -43
  2. package/index.esm.js +137 -43
  3. package/package.json +1 -1
package/index.cjs.js CHANGED
@@ -508,9 +508,17 @@ const endOfMonthUtil = (date, timeZone) => {
508
508
  * Output: 5
509
509
  */
510
510
  const differenceInMinutesUtil = (from, to) => {
511
- return toZonedDateTimeUtil(from).until(toZonedDateTimeUtil(to), {
512
- largestUnit: "minute",
513
- }).minutes;
511
+ let until = new polyfill.Temporal.Duration();
512
+ try {
513
+ until = toZonedDateTimeUtil(from).toPlainTime().until(toZonedDateTimeUtil(to).toPlainTime(), {
514
+ largestUnit: "minutes",
515
+ });
516
+ }
517
+ catch (error) {
518
+ // eslint-disable-next-line no-console
519
+ console.warn("Error: ", error);
520
+ }
521
+ return until.minutes;
514
522
  };
515
523
  /**
516
524
  * Calculates the difference in hours between two dates.
@@ -534,9 +542,17 @@ const differenceInMinutesUtil = (from, to) => {
534
542
  * Output: 2
535
543
  */
536
544
  const differenceInHoursUtil = (from, to) => {
537
- return toZonedDateTimeUtil(from).until(toZonedDateTimeUtil(to), {
538
- largestUnit: "hour",
539
- }).hours;
545
+ let until = new polyfill.Temporal.Duration();
546
+ try {
547
+ until = toZonedDateTimeUtil(from).toPlainTime().until(toZonedDateTimeUtil(to).toPlainTime(), {
548
+ largestUnit: "hours",
549
+ });
550
+ }
551
+ catch (error) {
552
+ // eslint-disable-next-line no-console
553
+ console.warn("Error: ", error);
554
+ }
555
+ return until.hours;
540
556
  };
541
557
  /**
542
558
  * Calculates the difference in days between two dates.
@@ -560,9 +576,17 @@ const differenceInHoursUtil = (from, to) => {
560
576
  * Output: 3
561
577
  */
562
578
  const differenceInDaysUtil = (from, to) => {
563
- return toZonedDateTimeUtil(from).until(toZonedDateTimeUtil(to), {
564
- largestUnit: "day",
565
- }).days;
579
+ let until = new polyfill.Temporal.Duration();
580
+ try {
581
+ until = toZonedDateTimeUtil(from).toPlainDate().until(toZonedDateTimeUtil(to).toPlainDate(), {
582
+ largestUnit: "day",
583
+ });
584
+ }
585
+ catch (error) {
586
+ // eslint-disable-next-line no-console
587
+ console.warn("Error: ", error, until.days);
588
+ }
589
+ return until.days;
566
590
  };
567
591
  /**
568
592
  * Calculates the difference in weeks between two dates.
@@ -586,9 +610,17 @@ const differenceInDaysUtil = (from, to) => {
586
610
  * Output: 1
587
611
  */
588
612
  const differenceInWeeksUtil = (from, to) => {
589
- return toZonedDateTimeUtil(from).until(toZonedDateTimeUtil(to), {
590
- largestUnit: "week",
591
- }).weeks;
613
+ let until = new polyfill.Temporal.Duration();
614
+ try {
615
+ until = toZonedDateTimeUtil(from).toPlainDate().until(toZonedDateTimeUtil(to).toPlainDate(), {
616
+ largestUnit: "week",
617
+ });
618
+ }
619
+ catch (error) {
620
+ // eslint-disable-next-line no-console
621
+ console.warn("Error: ", error);
622
+ }
623
+ return until.weeks;
592
624
  };
593
625
  /**
594
626
  * Calculates the difference in months between two dates.
@@ -612,9 +644,17 @@ const differenceInWeeksUtil = (from, to) => {
612
644
  * Output: 1
613
645
  */
614
646
  const differenceInMonthsUtil = (from, to) => {
615
- return toZonedDateTimeUtil(from).until(toZonedDateTimeUtil(to), {
616
- largestUnit: "month",
617
- }).months;
647
+ let until = new polyfill.Temporal.Duration();
648
+ try {
649
+ until = toZonedDateTimeUtil(from).toPlainDate().until(toZonedDateTimeUtil(to).toPlainDate(), {
650
+ largestUnit: "month",
651
+ });
652
+ }
653
+ catch (error) {
654
+ // eslint-disable-next-line no-console
655
+ console.warn("Error: ", error);
656
+ }
657
+ return until.months;
618
658
  };
619
659
  /**
620
660
  * Checks if a date is in the future.
@@ -784,7 +824,9 @@ const isEqualUtil = (from, to) => {
784
824
  const timeSinceAuto = (from, to, timeZone, locale) => {
785
825
  let since;
786
826
  try {
787
- since = toZonedDateTimeUtil(from, timeZone).since(toZonedDateTimeUtil(to, timeZone), {
827
+ since = toZonedDateTimeUtil(from, timeZone)
828
+ .toPlainDateTime()
829
+ .since(toZonedDateTimeUtil(to, timeZone).toPlainDateTime(), {
788
830
  largestUnit: "years",
789
831
  smallestUnit: "seconds",
790
832
  });
@@ -836,9 +878,17 @@ const timeSinceAuto = (from, to, timeZone, locale) => {
836
878
  * Output: "480 seconds ago"
837
879
  */
838
880
  const timeSinceInSeconds = (from, to, timeZone, locale) => {
839
- const duration = polyfill.Temporal.Duration.from(toZonedDateTimeUtil(from, timeZone).since(toZonedDateTimeUtil(to, timeZone), {
840
- largestUnit: "seconds",
841
- }));
881
+ let since = new polyfill.Temporal.Duration();
882
+ try {
883
+ since = toZonedDateTimeUtil(from, timeZone).since(toZonedDateTimeUtil(to, timeZone), {
884
+ largestUnit: "seconds",
885
+ });
886
+ }
887
+ catch (error) {
888
+ // eslint-disable-next-line no-console
889
+ console.warn("Error: ", error);
890
+ }
891
+ const duration = polyfill.Temporal.Duration.from(since);
842
892
  return getRelativeTimeFormat(locale).format(duration.seconds, "seconds");
843
893
  };
844
894
  /**
@@ -857,9 +907,17 @@ const timeSinceInSeconds = (from, to, timeZone, locale) => {
857
907
  * Output: "8 minutes ago"
858
908
  */
859
909
  const timeSinceInMinutes = (from, to, timeZone, locale) => {
860
- const duration = polyfill.Temporal.Duration.from(toZonedDateTimeUtil(from, timeZone).since(toZonedDateTimeUtil(to, timeZone), {
861
- largestUnit: "minutes",
862
- }));
910
+ let since = new polyfill.Temporal.Duration();
911
+ try {
912
+ since = toZonedDateTimeUtil(from, timeZone).since(toZonedDateTimeUtil(to, timeZone), {
913
+ largestUnit: "minutes",
914
+ });
915
+ }
916
+ catch (error) {
917
+ // eslint-disable-next-line no-console
918
+ console.warn("Error: ", error);
919
+ }
920
+ const duration = polyfill.Temporal.Duration.from(since);
863
921
  return getRelativeTimeFormat(locale).format(duration.minutes, "minutes");
864
922
  };
865
923
  /**
@@ -878,9 +936,17 @@ const timeSinceInMinutes = (from, to, timeZone, locale) => {
878
936
  * Output: "3 hours ago"
879
937
  */
880
938
  const timeSinceInHours = (from, to, timeZone, locale) => {
881
- const duration = polyfill.Temporal.Duration.from(toZonedDateTimeUtil(from, timeZone).since(toZonedDateTimeUtil(to, timeZone), {
882
- largestUnit: "hours",
883
- }));
939
+ let since = new polyfill.Temporal.Duration();
940
+ try {
941
+ since = toZonedDateTimeUtil(from, timeZone).since(toZonedDateTimeUtil(to, timeZone), {
942
+ largestUnit: "hours",
943
+ });
944
+ }
945
+ catch (error) {
946
+ // eslint-disable-next-line no-console
947
+ console.warn("Error: ", error);
948
+ }
949
+ const duration = polyfill.Temporal.Duration.from(since);
884
950
  return getRelativeTimeFormat(locale).format(duration.hours, "hours");
885
951
  };
886
952
  /**
@@ -905,9 +971,17 @@ const timeSinceInHours = (from, to, timeZone, locale) => {
905
971
  * Output: "6 days ago"
906
972
  */
907
973
  const timeSinceInDays = (from, to, timeZone, locale) => {
908
- const duration = polyfill.Temporal.Duration.from(toZonedDateTimeUtil(from, timeZone).since(toZonedDateTimeUtil(to, timeZone), {
909
- largestUnit: "days",
910
- }));
974
+ let since = new polyfill.Temporal.Duration();
975
+ try {
976
+ since = toZonedDateTimeUtil(from, timeZone).since(toZonedDateTimeUtil(to, timeZone), {
977
+ largestUnit: "days",
978
+ });
979
+ }
980
+ catch (error) {
981
+ // eslint-disable-next-line no-console
982
+ console.warn("Error: ", error);
983
+ }
984
+ const duration = polyfill.Temporal.Duration.from(since);
911
985
  return getRelativeTimeFormat(locale).format(duration.days, "days");
912
986
  };
913
987
  /**
@@ -930,9 +1004,17 @@ const timeSinceInMonths = (from, to, timeZone, locale) => {
930
1004
  if (isLeapYear) {
931
1005
  return timeSinceInDays(from, to, timeZone, locale);
932
1006
  }
933
- const duration = polyfill.Temporal.Duration.from(toZonedDateTimeUtil(from, timeZone).since(toZonedDateTimeUtil(to, timeZone), {
934
- largestUnit: "months",
935
- }));
1007
+ let since = new polyfill.Temporal.Duration();
1008
+ try {
1009
+ since = toZonedDateTimeUtil(from, timeZone).since(toZonedDateTimeUtil(to, timeZone), {
1010
+ largestUnit: "months",
1011
+ });
1012
+ }
1013
+ catch (error) {
1014
+ // eslint-disable-next-line no-console
1015
+ console.warn("Error: ", error);
1016
+ }
1017
+ const duration = polyfill.Temporal.Duration.from(since);
936
1018
  return getRelativeTimeFormat(locale).format(duration.months, "months");
937
1019
  };
938
1020
  /**
@@ -955,9 +1037,17 @@ const timeSinceInYears = (from, to, timeZone, locale) => {
955
1037
  if (isLeapYear) {
956
1038
  return timeSinceInDays(from, to, timeZone, locale);
957
1039
  }
958
- const duration = polyfill.Temporal.Duration.from(toZonedDateTimeUtil(from, timeZone).since(toZonedDateTimeUtil(to, timeZone), {
959
- largestUnit: "years",
960
- }));
1040
+ let since = new polyfill.Temporal.Duration();
1041
+ try {
1042
+ since = toZonedDateTimeUtil(from, timeZone).since(toZonedDateTimeUtil(to, timeZone), {
1043
+ largestUnit: "years",
1044
+ });
1045
+ }
1046
+ catch (error) {
1047
+ // eslint-disable-next-line no-console
1048
+ console.warn("Error: ", error);
1049
+ }
1050
+ const duration = polyfill.Temporal.Duration.from(since);
961
1051
  return getRelativeTimeFormat(locale).format(duration.years, "years");
962
1052
  };
963
1053
  const getRelativeTimeFormat = (locale) => new Intl.RelativeTimeFormat(locale, { style: "long", numeric: "always" });
@@ -995,6 +1085,7 @@ const toDuration = (number, unit, roundAt) => {
995
1085
  throw new Error("Not a valid unit", { cause: unit });
996
1086
  }
997
1087
  };
1088
+ const defaultDuration = { days: 0, hours: 0, minutes: 0, seconds: 0 };
998
1089
  /**
999
1090
  * Build a duration string
1000
1091
  *
@@ -1002,24 +1093,27 @@ const toDuration = (number, unit, roundAt) => {
1002
1093
  * @param unit - "days | "hours" | minutes
1003
1094
  */
1004
1095
  const durationBuilder = (number, unit) => {
1005
- const isInt = Number.isSafeInteger(number);
1006
- const { days, hours, minutes, seconds } = calculateTime(number, unit);
1096
+ const isSigned = Math.sign(number) === -1;
1097
+ const sign = isSigned ? "-" : "";
1098
+ const unsignedNumber = Math.abs(number);
1099
+ const isInt = Number.isSafeInteger(unsignedNumber);
1100
+ const { days, hours, minutes, seconds } = !isInt ? calculateTime(unsignedNumber, unit) : defaultDuration;
1007
1101
  switch (unit) {
1008
1102
  case "days":
1009
1103
  if (isInt) {
1010
- return `P${number}D`;
1104
+ return `${sign}P${unsignedNumber}D`;
1011
1105
  }
1012
- return `P${days}DT${hours}H${minutes}M${seconds}S`;
1106
+ return `${sign}P${days}DT${hours}H${minutes}M${seconds}S`;
1013
1107
  case "hours":
1014
1108
  if (isInt) {
1015
- return `PT${number}H`;
1109
+ return `${sign}PT${unsignedNumber}H`;
1016
1110
  }
1017
- return `PT${hours}H${minutes}M${seconds}S`;
1111
+ return `${sign}PT${hours}H${minutes}M${seconds}S`;
1018
1112
  case "minutes":
1019
1113
  if (isInt) {
1020
- return `PT${number}M`;
1114
+ return `${sign}PT${unsignedNumber}M`;
1021
1115
  }
1022
- return `PT${minutes}M${seconds}S`;
1116
+ return `${sign}PT${minutes}M${seconds}S`;
1023
1117
  default:
1024
1118
  return `PT0S`;
1025
1119
  }
@@ -1175,7 +1269,7 @@ const toZonedDateTimeUtil = (date, timeZoneId, calendar) => {
1175
1269
  }
1176
1270
  return date.toZonedDateTime({
1177
1271
  calendar: polyfill.Temporal.Calendar.from(calendar !== null && calendar !== void 0 ? calendar : "iso8601"),
1178
- timeZone: polyfill.Temporal.TimeZone.from(timeZoneId !== null && timeZoneId !== void 0 ? timeZoneId : polyfill.Temporal.Now.timeZoneId()),
1272
+ timeZone: polyfill.Temporal.TimeZone.from(timeZoneId || date.toString() || polyfill.Temporal.Now.timeZoneId()),
1179
1273
  });
1180
1274
  };
1181
1275
  /**
package/index.esm.js CHANGED
@@ -504,9 +504,17 @@ const endOfMonthUtil = (date, timeZone) => {
504
504
  * Output: 5
505
505
  */
506
506
  const differenceInMinutesUtil = (from, to) => {
507
- return toZonedDateTimeUtil(from).until(toZonedDateTimeUtil(to), {
508
- largestUnit: "minute",
509
- }).minutes;
507
+ let until = new Temporal.Duration();
508
+ try {
509
+ until = toZonedDateTimeUtil(from).toPlainTime().until(toZonedDateTimeUtil(to).toPlainTime(), {
510
+ largestUnit: "minutes",
511
+ });
512
+ }
513
+ catch (error) {
514
+ // eslint-disable-next-line no-console
515
+ console.warn("Error: ", error);
516
+ }
517
+ return until.minutes;
510
518
  };
511
519
  /**
512
520
  * Calculates the difference in hours between two dates.
@@ -530,9 +538,17 @@ const differenceInMinutesUtil = (from, to) => {
530
538
  * Output: 2
531
539
  */
532
540
  const differenceInHoursUtil = (from, to) => {
533
- return toZonedDateTimeUtil(from).until(toZonedDateTimeUtil(to), {
534
- largestUnit: "hour",
535
- }).hours;
541
+ let until = new Temporal.Duration();
542
+ try {
543
+ until = toZonedDateTimeUtil(from).toPlainTime().until(toZonedDateTimeUtil(to).toPlainTime(), {
544
+ largestUnit: "hours",
545
+ });
546
+ }
547
+ catch (error) {
548
+ // eslint-disable-next-line no-console
549
+ console.warn("Error: ", error);
550
+ }
551
+ return until.hours;
536
552
  };
537
553
  /**
538
554
  * Calculates the difference in days between two dates.
@@ -556,9 +572,17 @@ const differenceInHoursUtil = (from, to) => {
556
572
  * Output: 3
557
573
  */
558
574
  const differenceInDaysUtil = (from, to) => {
559
- return toZonedDateTimeUtil(from).until(toZonedDateTimeUtil(to), {
560
- largestUnit: "day",
561
- }).days;
575
+ let until = new Temporal.Duration();
576
+ try {
577
+ until = toZonedDateTimeUtil(from).toPlainDate().until(toZonedDateTimeUtil(to).toPlainDate(), {
578
+ largestUnit: "day",
579
+ });
580
+ }
581
+ catch (error) {
582
+ // eslint-disable-next-line no-console
583
+ console.warn("Error: ", error, until.days);
584
+ }
585
+ return until.days;
562
586
  };
563
587
  /**
564
588
  * Calculates the difference in weeks between two dates.
@@ -582,9 +606,17 @@ const differenceInDaysUtil = (from, to) => {
582
606
  * Output: 1
583
607
  */
584
608
  const differenceInWeeksUtil = (from, to) => {
585
- return toZonedDateTimeUtil(from).until(toZonedDateTimeUtil(to), {
586
- largestUnit: "week",
587
- }).weeks;
609
+ let until = new Temporal.Duration();
610
+ try {
611
+ until = toZonedDateTimeUtil(from).toPlainDate().until(toZonedDateTimeUtil(to).toPlainDate(), {
612
+ largestUnit: "week",
613
+ });
614
+ }
615
+ catch (error) {
616
+ // eslint-disable-next-line no-console
617
+ console.warn("Error: ", error);
618
+ }
619
+ return until.weeks;
588
620
  };
589
621
  /**
590
622
  * Calculates the difference in months between two dates.
@@ -608,9 +640,17 @@ const differenceInWeeksUtil = (from, to) => {
608
640
  * Output: 1
609
641
  */
610
642
  const differenceInMonthsUtil = (from, to) => {
611
- return toZonedDateTimeUtil(from).until(toZonedDateTimeUtil(to), {
612
- largestUnit: "month",
613
- }).months;
643
+ let until = new Temporal.Duration();
644
+ try {
645
+ until = toZonedDateTimeUtil(from).toPlainDate().until(toZonedDateTimeUtil(to).toPlainDate(), {
646
+ largestUnit: "month",
647
+ });
648
+ }
649
+ catch (error) {
650
+ // eslint-disable-next-line no-console
651
+ console.warn("Error: ", error);
652
+ }
653
+ return until.months;
614
654
  };
615
655
  /**
616
656
  * Checks if a date is in the future.
@@ -780,7 +820,9 @@ const isEqualUtil = (from, to) => {
780
820
  const timeSinceAuto = (from, to, timeZone, locale) => {
781
821
  let since;
782
822
  try {
783
- since = toZonedDateTimeUtil(from, timeZone).since(toZonedDateTimeUtil(to, timeZone), {
823
+ since = toZonedDateTimeUtil(from, timeZone)
824
+ .toPlainDateTime()
825
+ .since(toZonedDateTimeUtil(to, timeZone).toPlainDateTime(), {
784
826
  largestUnit: "years",
785
827
  smallestUnit: "seconds",
786
828
  });
@@ -832,9 +874,17 @@ const timeSinceAuto = (from, to, timeZone, locale) => {
832
874
  * Output: "480 seconds ago"
833
875
  */
834
876
  const timeSinceInSeconds = (from, to, timeZone, locale) => {
835
- const duration = Temporal.Duration.from(toZonedDateTimeUtil(from, timeZone).since(toZonedDateTimeUtil(to, timeZone), {
836
- largestUnit: "seconds",
837
- }));
877
+ let since = new Temporal.Duration();
878
+ try {
879
+ since = toZonedDateTimeUtil(from, timeZone).since(toZonedDateTimeUtil(to, timeZone), {
880
+ largestUnit: "seconds",
881
+ });
882
+ }
883
+ catch (error) {
884
+ // eslint-disable-next-line no-console
885
+ console.warn("Error: ", error);
886
+ }
887
+ const duration = Temporal.Duration.from(since);
838
888
  return getRelativeTimeFormat(locale).format(duration.seconds, "seconds");
839
889
  };
840
890
  /**
@@ -853,9 +903,17 @@ const timeSinceInSeconds = (from, to, timeZone, locale) => {
853
903
  * Output: "8 minutes ago"
854
904
  */
855
905
  const timeSinceInMinutes = (from, to, timeZone, locale) => {
856
- const duration = Temporal.Duration.from(toZonedDateTimeUtil(from, timeZone).since(toZonedDateTimeUtil(to, timeZone), {
857
- largestUnit: "minutes",
858
- }));
906
+ let since = new Temporal.Duration();
907
+ try {
908
+ since = toZonedDateTimeUtil(from, timeZone).since(toZonedDateTimeUtil(to, timeZone), {
909
+ largestUnit: "minutes",
910
+ });
911
+ }
912
+ catch (error) {
913
+ // eslint-disable-next-line no-console
914
+ console.warn("Error: ", error);
915
+ }
916
+ const duration = Temporal.Duration.from(since);
859
917
  return getRelativeTimeFormat(locale).format(duration.minutes, "minutes");
860
918
  };
861
919
  /**
@@ -874,9 +932,17 @@ const timeSinceInMinutes = (from, to, timeZone, locale) => {
874
932
  * Output: "3 hours ago"
875
933
  */
876
934
  const timeSinceInHours = (from, to, timeZone, locale) => {
877
- const duration = Temporal.Duration.from(toZonedDateTimeUtil(from, timeZone).since(toZonedDateTimeUtil(to, timeZone), {
878
- largestUnit: "hours",
879
- }));
935
+ let since = new Temporal.Duration();
936
+ try {
937
+ since = toZonedDateTimeUtil(from, timeZone).since(toZonedDateTimeUtil(to, timeZone), {
938
+ largestUnit: "hours",
939
+ });
940
+ }
941
+ catch (error) {
942
+ // eslint-disable-next-line no-console
943
+ console.warn("Error: ", error);
944
+ }
945
+ const duration = Temporal.Duration.from(since);
880
946
  return getRelativeTimeFormat(locale).format(duration.hours, "hours");
881
947
  };
882
948
  /**
@@ -901,9 +967,17 @@ const timeSinceInHours = (from, to, timeZone, locale) => {
901
967
  * Output: "6 days ago"
902
968
  */
903
969
  const timeSinceInDays = (from, to, timeZone, locale) => {
904
- const duration = Temporal.Duration.from(toZonedDateTimeUtil(from, timeZone).since(toZonedDateTimeUtil(to, timeZone), {
905
- largestUnit: "days",
906
- }));
970
+ let since = new Temporal.Duration();
971
+ try {
972
+ since = toZonedDateTimeUtil(from, timeZone).since(toZonedDateTimeUtil(to, timeZone), {
973
+ largestUnit: "days",
974
+ });
975
+ }
976
+ catch (error) {
977
+ // eslint-disable-next-line no-console
978
+ console.warn("Error: ", error);
979
+ }
980
+ const duration = Temporal.Duration.from(since);
907
981
  return getRelativeTimeFormat(locale).format(duration.days, "days");
908
982
  };
909
983
  /**
@@ -926,9 +1000,17 @@ const timeSinceInMonths = (from, to, timeZone, locale) => {
926
1000
  if (isLeapYear) {
927
1001
  return timeSinceInDays(from, to, timeZone, locale);
928
1002
  }
929
- const duration = Temporal.Duration.from(toZonedDateTimeUtil(from, timeZone).since(toZonedDateTimeUtil(to, timeZone), {
930
- largestUnit: "months",
931
- }));
1003
+ let since = new Temporal.Duration();
1004
+ try {
1005
+ since = toZonedDateTimeUtil(from, timeZone).since(toZonedDateTimeUtil(to, timeZone), {
1006
+ largestUnit: "months",
1007
+ });
1008
+ }
1009
+ catch (error) {
1010
+ // eslint-disable-next-line no-console
1011
+ console.warn("Error: ", error);
1012
+ }
1013
+ const duration = Temporal.Duration.from(since);
932
1014
  return getRelativeTimeFormat(locale).format(duration.months, "months");
933
1015
  };
934
1016
  /**
@@ -951,9 +1033,17 @@ const timeSinceInYears = (from, to, timeZone, locale) => {
951
1033
  if (isLeapYear) {
952
1034
  return timeSinceInDays(from, to, timeZone, locale);
953
1035
  }
954
- const duration = Temporal.Duration.from(toZonedDateTimeUtil(from, timeZone).since(toZonedDateTimeUtil(to, timeZone), {
955
- largestUnit: "years",
956
- }));
1036
+ let since = new Temporal.Duration();
1037
+ try {
1038
+ since = toZonedDateTimeUtil(from, timeZone).since(toZonedDateTimeUtil(to, timeZone), {
1039
+ largestUnit: "years",
1040
+ });
1041
+ }
1042
+ catch (error) {
1043
+ // eslint-disable-next-line no-console
1044
+ console.warn("Error: ", error);
1045
+ }
1046
+ const duration = Temporal.Duration.from(since);
957
1047
  return getRelativeTimeFormat(locale).format(duration.years, "years");
958
1048
  };
959
1049
  const getRelativeTimeFormat = (locale) => new Intl.RelativeTimeFormat(locale, { style: "long", numeric: "always" });
@@ -991,6 +1081,7 @@ const toDuration = (number, unit, roundAt) => {
991
1081
  throw new Error("Not a valid unit", { cause: unit });
992
1082
  }
993
1083
  };
1084
+ const defaultDuration = { days: 0, hours: 0, minutes: 0, seconds: 0 };
994
1085
  /**
995
1086
  * Build a duration string
996
1087
  *
@@ -998,24 +1089,27 @@ const toDuration = (number, unit, roundAt) => {
998
1089
  * @param unit - "days | "hours" | minutes
999
1090
  */
1000
1091
  const durationBuilder = (number, unit) => {
1001
- const isInt = Number.isSafeInteger(number);
1002
- const { days, hours, minutes, seconds } = calculateTime(number, unit);
1092
+ const isSigned = Math.sign(number) === -1;
1093
+ const sign = isSigned ? "-" : "";
1094
+ const unsignedNumber = Math.abs(number);
1095
+ const isInt = Number.isSafeInteger(unsignedNumber);
1096
+ const { days, hours, minutes, seconds } = !isInt ? calculateTime(unsignedNumber, unit) : defaultDuration;
1003
1097
  switch (unit) {
1004
1098
  case "days":
1005
1099
  if (isInt) {
1006
- return `P${number}D`;
1100
+ return `${sign}P${unsignedNumber}D`;
1007
1101
  }
1008
- return `P${days}DT${hours}H${minutes}M${seconds}S`;
1102
+ return `${sign}P${days}DT${hours}H${minutes}M${seconds}S`;
1009
1103
  case "hours":
1010
1104
  if (isInt) {
1011
- return `PT${number}H`;
1105
+ return `${sign}PT${unsignedNumber}H`;
1012
1106
  }
1013
- return `PT${hours}H${minutes}M${seconds}S`;
1107
+ return `${sign}PT${hours}H${minutes}M${seconds}S`;
1014
1108
  case "minutes":
1015
1109
  if (isInt) {
1016
- return `PT${number}M`;
1110
+ return `${sign}PT${unsignedNumber}M`;
1017
1111
  }
1018
- return `PT${minutes}M${seconds}S`;
1112
+ return `${sign}PT${minutes}M${seconds}S`;
1019
1113
  default:
1020
1114
  return `PT0S`;
1021
1115
  }
@@ -1171,7 +1265,7 @@ const toZonedDateTimeUtil = (date, timeZoneId, calendar) => {
1171
1265
  }
1172
1266
  return date.toZonedDateTime({
1173
1267
  calendar: Temporal.Calendar.from(calendar !== null && calendar !== void 0 ? calendar : "iso8601"),
1174
- timeZone: Temporal.TimeZone.from(timeZoneId !== null && timeZoneId !== void 0 ? timeZoneId : Temporal.Now.timeZoneId()),
1268
+ timeZone: Temporal.TimeZone.from(timeZoneId || date.toString() || Temporal.Now.timeZoneId()),
1175
1269
  });
1176
1270
  };
1177
1271
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trackunit/date-and-time-utils",
3
- "version": "0.0.33",
3
+ "version": "0.0.34",
4
4
  "repository": "https://github.com/Trackunit/manager",
5
5
  "license": "SEE LICENSE IN LICENSE.txt",
6
6
  "engines": {