| 1048 | * - P14D = 14 days (or P2W) |
| 1049 | */ |
| 1050 | export class DurationValue implements TemporalValue { |
| 1051 | public readonly temporalType = "duration"; |
| 1052 | |
| 1053 | #months: number; |
| 1054 | #days: number; |
| 1055 | #seconds: number; |
| 1056 | #nanoseconds: number; |
| 1057 | |
| 1058 | public constructor( |
| 1059 | months: number = 0, |
| 1060 | days: number = 0, |
| 1061 | seconds: number = 0, |
| 1062 | nanoseconds: number = 0, |
| 1063 | ) { |
| 1064 | this.#months = months; |
| 1065 | this.#days = days; |
| 1066 | this.#seconds = seconds; |
| 1067 | this.#nanoseconds = nanoseconds; |
| 1068 | } |
| 1069 | |
| 1070 | /** |
| 1071 | * Create a DurationValue from an ISO 8601 duration string. |
| 1072 | * Format: P[n]Y[n]M[n]W[n]DT[n]H[n]M[n]S |
| 1073 | */ |
| 1074 | public static fromString(iso: string): DurationValue | null { |
| 1075 | // Handle negative durations |
| 1076 | let isNegative = false; |
| 1077 | let str = iso; |
| 1078 | if (str.startsWith("-")) { |
| 1079 | isNegative = true; |
| 1080 | str = str.substring(1); |
| 1081 | } |
| 1082 | |
| 1083 | if (!str.startsWith("P")) return null; |
| 1084 | str = str.substring(1); |
| 1085 | |
| 1086 | let months = 0; |
| 1087 | let days = 0; |
| 1088 | let seconds = 0; |
| 1089 | let nanoseconds = 0; |
| 1090 | |
| 1091 | // Split by T to separate date and time parts |
| 1092 | const parts = str.split("T"); |
| 1093 | const datePart = parts[0] || ""; |
| 1094 | const timePart = parts[1] || ""; |
| 1095 | |
| 1096 | // Parse date part (years, months, weeks, days) |
| 1097 | // Match patterns like "1Y", "2M", "1W", "3D" |
| 1098 | const datePattern = /(-?\d+(?:\.\d+)?)(Y|M|W|D)/g; |
| 1099 | let match: RegExpExecArray | null; |
| 1100 | while ((match = datePattern.exec(datePart)) !== null) { |
| 1101 | const value = parseFloat(match[1] ?? "0"); |
| 1102 | const unit = match[2]; |
| 1103 | switch (unit) { |
| 1104 | case "Y": |
| 1105 | months += value * 12; |
| 1106 | break; |
| 1107 | case "M": |
nothing calls this directly
no outgoing calls
no test coverage detected