@trackunit/date-and-time-utils 0.0.31 → 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 +146 -45
  2. package/index.esm.js +146 -45
  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.
@@ -782,32 +822,45 @@ const isEqualUtil = (from, to) => {
782
822
  * Output: "1 year ago"
783
823
  */
784
824
  const timeSinceAuto = (from, to, timeZone, locale) => {
785
- const duration = polyfill.Temporal.Duration.from(toZonedDateTimeUtil(from, timeZone).since(toZonedDateTimeUtil(to, timeZone), {
786
- largestUnit: "years",
787
- smallestUnit: "seconds",
788
- }));
825
+ let since;
826
+ try {
827
+ since = toZonedDateTimeUtil(from, timeZone)
828
+ .toPlainDateTime()
829
+ .since(toZonedDateTimeUtil(to, timeZone).toPlainDateTime(), {
830
+ largestUnit: "years",
831
+ smallestUnit: "seconds",
832
+ });
833
+ }
834
+ catch (error) {
835
+ // eslint-disable-next-line no-console
836
+ console.warn(new Error("Not a valid duration", { cause: error }));
837
+ return "";
838
+ }
839
+ const duration = polyfill.Temporal.Duration.from(since);
789
840
  if (duration.blank) {
790
841
  return getRelativeTimeFormat(locale).format(-1, "seconds");
791
842
  }
792
843
  if (duration.years !== 0) {
793
- return timeSinceInYears(from, to, timeZone, locale);
844
+ return getRelativeTimeFormat(locale).format(duration.years, "years");
794
845
  }
795
846
  if (duration.months !== 0) {
796
- return timeSinceInMonths(from, to, timeZone, locale);
847
+ return getRelativeTimeFormat(locale).format(duration.months, "months");
797
848
  }
798
849
  if (duration.days !== 0) {
799
- return timeSinceInDays(from, to, timeZone, locale);
850
+ return getRelativeTimeFormat(locale).format(duration.days, "days");
800
851
  }
801
852
  if (duration.hours !== 0) {
802
- return timeSinceInHours(from, to, timeZone, locale);
853
+ return getRelativeTimeFormat(locale).format(duration.hours, "hours");
803
854
  }
804
855
  if (duration.minutes !== 0) {
805
- return timeSinceInMinutes(from, to, timeZone, locale);
856
+ return getRelativeTimeFormat(locale).format(duration.minutes, "minutes");
806
857
  }
807
858
  if (duration.seconds !== 0) {
808
- return timeSinceInSeconds(from, to, timeZone, locale);
859
+ return getRelativeTimeFormat(locale).format(duration.seconds, "seconds");
809
860
  }
810
- throw new Error("Not a valid duration", { cause: duration });
861
+ // eslint-disable-next-line no-console
862
+ console.warn(new Error("Not a valid duration", { cause: duration }));
863
+ return "";
811
864
  };
812
865
  /**
813
866
  * Calculates the time duration between two TemporalDate objects and returns the duration in seconds as a formatted string.
@@ -825,9 +878,17 @@ const timeSinceAuto = (from, to, timeZone, locale) => {
825
878
  * Output: "480 seconds ago"
826
879
  */
827
880
  const timeSinceInSeconds = (from, to, timeZone, locale) => {
828
- const duration = polyfill.Temporal.Duration.from(toZonedDateTimeUtil(from, timeZone).since(toZonedDateTimeUtil(to, timeZone), {
829
- largestUnit: "seconds",
830
- }));
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);
831
892
  return getRelativeTimeFormat(locale).format(duration.seconds, "seconds");
832
893
  };
833
894
  /**
@@ -846,9 +907,17 @@ const timeSinceInSeconds = (from, to, timeZone, locale) => {
846
907
  * Output: "8 minutes ago"
847
908
  */
848
909
  const timeSinceInMinutes = (from, to, timeZone, locale) => {
849
- const duration = polyfill.Temporal.Duration.from(toZonedDateTimeUtil(from, timeZone).since(toZonedDateTimeUtil(to, timeZone), {
850
- largestUnit: "minutes",
851
- }));
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);
852
921
  return getRelativeTimeFormat(locale).format(duration.minutes, "minutes");
853
922
  };
854
923
  /**
@@ -867,9 +936,17 @@ const timeSinceInMinutes = (from, to, timeZone, locale) => {
867
936
  * Output: "3 hours ago"
868
937
  */
869
938
  const timeSinceInHours = (from, to, timeZone, locale) => {
870
- const duration = polyfill.Temporal.Duration.from(toZonedDateTimeUtil(from, timeZone).since(toZonedDateTimeUtil(to, timeZone), {
871
- largestUnit: "hours",
872
- }));
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);
873
950
  return getRelativeTimeFormat(locale).format(duration.hours, "hours");
874
951
  };
875
952
  /**
@@ -894,9 +971,17 @@ const timeSinceInHours = (from, to, timeZone, locale) => {
894
971
  * Output: "6 days ago"
895
972
  */
896
973
  const timeSinceInDays = (from, to, timeZone, locale) => {
897
- const duration = polyfill.Temporal.Duration.from(toZonedDateTimeUtil(from, timeZone).since(toZonedDateTimeUtil(to, timeZone), {
898
- largestUnit: "days",
899
- }));
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);
900
985
  return getRelativeTimeFormat(locale).format(duration.days, "days");
901
986
  };
902
987
  /**
@@ -919,9 +1004,17 @@ const timeSinceInMonths = (from, to, timeZone, locale) => {
919
1004
  if (isLeapYear) {
920
1005
  return timeSinceInDays(from, to, timeZone, locale);
921
1006
  }
922
- const duration = polyfill.Temporal.Duration.from(toZonedDateTimeUtil(from, timeZone).since(toZonedDateTimeUtil(to, timeZone), {
923
- largestUnit: "months",
924
- }));
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);
925
1018
  return getRelativeTimeFormat(locale).format(duration.months, "months");
926
1019
  };
927
1020
  /**
@@ -944,9 +1037,17 @@ const timeSinceInYears = (from, to, timeZone, locale) => {
944
1037
  if (isLeapYear) {
945
1038
  return timeSinceInDays(from, to, timeZone, locale);
946
1039
  }
947
- const duration = polyfill.Temporal.Duration.from(toZonedDateTimeUtil(from, timeZone).since(toZonedDateTimeUtil(to, timeZone), {
948
- largestUnit: "years",
949
- }));
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);
950
1051
  return getRelativeTimeFormat(locale).format(duration.years, "years");
951
1052
  };
952
1053
  const getRelativeTimeFormat = (locale) => new Intl.RelativeTimeFormat(locale, { style: "long", numeric: "always" });
@@ -1168,7 +1269,7 @@ const toZonedDateTimeUtil = (date, timeZoneId, calendar) => {
1168
1269
  }
1169
1270
  return date.toZonedDateTime({
1170
1271
  calendar: polyfill.Temporal.Calendar.from(calendar !== null && calendar !== void 0 ? calendar : "iso8601"),
1171
- 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()),
1172
1273
  });
1173
1274
  };
1174
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.
@@ -778,32 +818,45 @@ const isEqualUtil = (from, to) => {
778
818
  * Output: "1 year ago"
779
819
  */
780
820
  const timeSinceAuto = (from, to, timeZone, locale) => {
781
- const duration = Temporal.Duration.from(toZonedDateTimeUtil(from, timeZone).since(toZonedDateTimeUtil(to, timeZone), {
782
- largestUnit: "years",
783
- smallestUnit: "seconds",
784
- }));
821
+ let since;
822
+ try {
823
+ since = toZonedDateTimeUtil(from, timeZone)
824
+ .toPlainDateTime()
825
+ .since(toZonedDateTimeUtil(to, timeZone).toPlainDateTime(), {
826
+ largestUnit: "years",
827
+ smallestUnit: "seconds",
828
+ });
829
+ }
830
+ catch (error) {
831
+ // eslint-disable-next-line no-console
832
+ console.warn(new Error("Not a valid duration", { cause: error }));
833
+ return "";
834
+ }
835
+ const duration = Temporal.Duration.from(since);
785
836
  if (duration.blank) {
786
837
  return getRelativeTimeFormat(locale).format(-1, "seconds");
787
838
  }
788
839
  if (duration.years !== 0) {
789
- return timeSinceInYears(from, to, timeZone, locale);
840
+ return getRelativeTimeFormat(locale).format(duration.years, "years");
790
841
  }
791
842
  if (duration.months !== 0) {
792
- return timeSinceInMonths(from, to, timeZone, locale);
843
+ return getRelativeTimeFormat(locale).format(duration.months, "months");
793
844
  }
794
845
  if (duration.days !== 0) {
795
- return timeSinceInDays(from, to, timeZone, locale);
846
+ return getRelativeTimeFormat(locale).format(duration.days, "days");
796
847
  }
797
848
  if (duration.hours !== 0) {
798
- return timeSinceInHours(from, to, timeZone, locale);
849
+ return getRelativeTimeFormat(locale).format(duration.hours, "hours");
799
850
  }
800
851
  if (duration.minutes !== 0) {
801
- return timeSinceInMinutes(from, to, timeZone, locale);
852
+ return getRelativeTimeFormat(locale).format(duration.minutes, "minutes");
802
853
  }
803
854
  if (duration.seconds !== 0) {
804
- return timeSinceInSeconds(from, to, timeZone, locale);
855
+ return getRelativeTimeFormat(locale).format(duration.seconds, "seconds");
805
856
  }
806
- throw new Error("Not a valid duration", { cause: duration });
857
+ // eslint-disable-next-line no-console
858
+ console.warn(new Error("Not a valid duration", { cause: duration }));
859
+ return "";
807
860
  };
808
861
  /**
809
862
  * Calculates the time duration between two TemporalDate objects and returns the duration in seconds as a formatted string.
@@ -821,9 +874,17 @@ const timeSinceAuto = (from, to, timeZone, locale) => {
821
874
  * Output: "480 seconds ago"
822
875
  */
823
876
  const timeSinceInSeconds = (from, to, timeZone, locale) => {
824
- const duration = Temporal.Duration.from(toZonedDateTimeUtil(from, timeZone).since(toZonedDateTimeUtil(to, timeZone), {
825
- largestUnit: "seconds",
826
- }));
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);
827
888
  return getRelativeTimeFormat(locale).format(duration.seconds, "seconds");
828
889
  };
829
890
  /**
@@ -842,9 +903,17 @@ const timeSinceInSeconds = (from, to, timeZone, locale) => {
842
903
  * Output: "8 minutes ago"
843
904
  */
844
905
  const timeSinceInMinutes = (from, to, timeZone, locale) => {
845
- const duration = Temporal.Duration.from(toZonedDateTimeUtil(from, timeZone).since(toZonedDateTimeUtil(to, timeZone), {
846
- largestUnit: "minutes",
847
- }));
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);
848
917
  return getRelativeTimeFormat(locale).format(duration.minutes, "minutes");
849
918
  };
850
919
  /**
@@ -863,9 +932,17 @@ const timeSinceInMinutes = (from, to, timeZone, locale) => {
863
932
  * Output: "3 hours ago"
864
933
  */
865
934
  const timeSinceInHours = (from, to, timeZone, locale) => {
866
- const duration = Temporal.Duration.from(toZonedDateTimeUtil(from, timeZone).since(toZonedDateTimeUtil(to, timeZone), {
867
- largestUnit: "hours",
868
- }));
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);
869
946
  return getRelativeTimeFormat(locale).format(duration.hours, "hours");
870
947
  };
871
948
  /**
@@ -890,9 +967,17 @@ const timeSinceInHours = (from, to, timeZone, locale) => {
890
967
  * Output: "6 days ago"
891
968
  */
892
969
  const timeSinceInDays = (from, to, timeZone, locale) => {
893
- const duration = Temporal.Duration.from(toZonedDateTimeUtil(from, timeZone).since(toZonedDateTimeUtil(to, timeZone), {
894
- largestUnit: "days",
895
- }));
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);
896
981
  return getRelativeTimeFormat(locale).format(duration.days, "days");
897
982
  };
898
983
  /**
@@ -915,9 +1000,17 @@ const timeSinceInMonths = (from, to, timeZone, locale) => {
915
1000
  if (isLeapYear) {
916
1001
  return timeSinceInDays(from, to, timeZone, locale);
917
1002
  }
918
- const duration = Temporal.Duration.from(toZonedDateTimeUtil(from, timeZone).since(toZonedDateTimeUtil(to, timeZone), {
919
- largestUnit: "months",
920
- }));
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);
921
1014
  return getRelativeTimeFormat(locale).format(duration.months, "months");
922
1015
  };
923
1016
  /**
@@ -940,9 +1033,17 @@ const timeSinceInYears = (from, to, timeZone, locale) => {
940
1033
  if (isLeapYear) {
941
1034
  return timeSinceInDays(from, to, timeZone, locale);
942
1035
  }
943
- const duration = Temporal.Duration.from(toZonedDateTimeUtil(from, timeZone).since(toZonedDateTimeUtil(to, timeZone), {
944
- largestUnit: "years",
945
- }));
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);
946
1047
  return getRelativeTimeFormat(locale).format(duration.years, "years");
947
1048
  };
948
1049
  const getRelativeTimeFormat = (locale) => new Intl.RelativeTimeFormat(locale, { style: "long", numeric: "always" });
@@ -1164,7 +1265,7 @@ const toZonedDateTimeUtil = (date, timeZoneId, calendar) => {
1164
1265
  }
1165
1266
  return date.toZonedDateTime({
1166
1267
  calendar: Temporal.Calendar.from(calendar !== null && calendar !== void 0 ? calendar : "iso8601"),
1167
- timeZone: Temporal.TimeZone.from(timeZoneId !== null && timeZoneId !== void 0 ? timeZoneId : Temporal.Now.timeZoneId()),
1268
+ timeZone: Temporal.TimeZone.from(timeZoneId || date.toString() || Temporal.Now.timeZoneId()),
1168
1269
  });
1169
1270
  };
1170
1271
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trackunit/date-and-time-utils",
3
- "version": "0.0.31",
3
+ "version": "0.0.34",
4
4
  "repository": "https://github.com/Trackunit/manager",
5
5
  "license": "SEE LICENSE IN LICENSE.txt",
6
6
  "engines": {