| 876 | * @category conversions |
| 877 | */ |
| 878 | export const unsafeFormatIso = (self: DurationInput): string => { |
| 879 | const duration = decode(self) |
| 880 | if (!isFinite(duration)) { |
| 881 | throw new RangeError("Cannot format infinite duration") |
| 882 | } |
| 883 | |
| 884 | const fragments = [] |
| 885 | const { |
| 886 | days, |
| 887 | hours, |
| 888 | millis, |
| 889 | minutes, |
| 890 | nanos, |
| 891 | seconds |
| 892 | } = parts(duration) |
| 893 | |
| 894 | let rest = days |
| 895 | if (rest >= 365) { |
| 896 | const years = Math.floor(rest / 365) |
| 897 | rest %= 365 |
| 898 | fragments.push(`${years}Y`) |
| 899 | } |
| 900 | |
| 901 | if (rest >= 30) { |
| 902 | const months = Math.floor(rest / 30) |
| 903 | rest %= 30 |
| 904 | fragments.push(`${months}M`) |
| 905 | } |
| 906 | |
| 907 | if (rest >= 7) { |
| 908 | const weeks = Math.floor(rest / 7) |
| 909 | rest %= 7 |
| 910 | fragments.push(`${weeks}W`) |
| 911 | } |
| 912 | |
| 913 | if (rest > 0) { |
| 914 | fragments.push(`${rest}D`) |
| 915 | } |
| 916 | |
| 917 | if (hours !== 0 || minutes !== 0 || seconds !== 0 || millis !== 0 || nanos !== 0) { |
| 918 | fragments.push("T") |
| 919 | |
| 920 | if (hours !== 0) { |
| 921 | fragments.push(`${hours}H`) |
| 922 | } |
| 923 | |
| 924 | if (minutes !== 0) { |
| 925 | fragments.push(`${minutes}M`) |
| 926 | } |
| 927 | |
| 928 | if (seconds !== 0 || millis !== 0 || nanos !== 0) { |
| 929 | const total = BigInt(seconds) * bigint1e9 + BigInt(millis) * bigint1e6 + BigInt(nanos) |
| 930 | const str = (Number(total) / 1e9).toFixed(9).replace(/\.?0+$/, "") |
| 931 | fragments.push(`${str}S`) |
| 932 | } |
| 933 | } |
| 934 | |
| 935 | return `P${fragments.join("") || "T0S"}` |