1 | import { normalizeInterval } from "./_lib/normalizeInterval.js";
|
2 | import { add } from "./add.js";
|
3 | import { differenceInDays } from "./differenceInDays.js";
|
4 | import { differenceInHours } from "./differenceInHours.js";
|
5 | import { differenceInMinutes } from "./differenceInMinutes.js";
|
6 | import { differenceInMonths } from "./differenceInMonths.js";
|
7 | import { differenceInSeconds } from "./differenceInSeconds.js";
|
8 | import { differenceInYears } from "./differenceInYears.js";
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 | export function intervalToDuration(interval, options) {
|
36 | const { start, end } = normalizeInterval(options?.in, interval);
|
37 | const duration = {};
|
38 |
|
39 | const years = differenceInYears(end, start);
|
40 | if (years) duration.years = years;
|
41 |
|
42 | const remainingMonths = add(start, { years: duration.years });
|
43 | const months = differenceInMonths(end, remainingMonths);
|
44 | if (months) duration.months = months;
|
45 |
|
46 | const remainingDays = add(remainingMonths, { months: duration.months });
|
47 | const days = differenceInDays(end, remainingDays);
|
48 | if (days) duration.days = days;
|
49 |
|
50 | const remainingHours = add(remainingDays, { days: duration.days });
|
51 | const hours = differenceInHours(end, remainingHours);
|
52 | if (hours) duration.hours = hours;
|
53 |
|
54 | const remainingMinutes = add(remainingHours, { hours: duration.hours });
|
55 | const minutes = differenceInMinutes(end, remainingMinutes);
|
56 | if (minutes) duration.minutes = minutes;
|
57 |
|
58 | const remainingSeconds = add(remainingMinutes, { minutes: duration.minutes });
|
59 | const seconds = differenceInSeconds(end, remainingSeconds);
|
60 | if (seconds) duration.seconds = seconds;
|
61 |
|
62 | return duration;
|
63 | }
|
64 |
|
65 |
|
66 | export default intervalToDuration;
|