MCPcopy Create free account
hub / github.com/Effect-TS/effect / unsafeFormatIso

Function unsafeFormatIso

packages/effect/src/Duration.ts:887–945  ·  view source on GitHub ↗
(self: DurationInput)

Source from the content-addressed store, hash-verified

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

Callers 1

formatIsoFunction · 0.85

Calls 5

isFiniteFunction · 0.85
partsFunction · 0.85
NumberInterface · 0.85
decodeFunction · 0.70
joinMethod · 0.65

Tested by

no test coverage detected