MCPcopy Create free account
hub / github.com/codemix/graph / toString

Method toString

packages/graph/src/TemporalTypes.ts:1290–1342  ·  view source on GitHub ↗

* Convert to ISO 8601 duration string.

()

Source from the content-addressed store, hash-verified

1288 * Convert to ISO 8601 duration string.
1289 */
1290 public toString(): string {
1291 const parts: string[] = [];
1292
1293 // Determine if overall negative
1294 const isNegative = this.#months < 0 || this.#days < 0 || this.#seconds < 0;
1295
1296 // Use absolute values for formatting
1297 const absMonths = Math.abs(this.#months);
1298 const absDays = Math.abs(this.#days);
1299 const absSeconds = Math.abs(this.#seconds);
1300 const absNanos = Math.abs(this.#nanoseconds);
1301
1302 // Date part
1303 const years = Math.trunc(absMonths / 12);
1304 const monthsOfYear = absMonths % 12;
1305 if (years > 0) parts.push(`${years}Y`);
1306 if (monthsOfYear > 0) parts.push(`${monthsOfYear}M`);
1307 if (absDays > 0) parts.push(`${absDays}D`);
1308
1309 // Time part
1310 const timeParts: string[] = [];
1311 const hours = Math.trunc(absSeconds / 3600);
1312 const minutes = Math.trunc((absSeconds % 3600) / 60);
1313 const secs = absSeconds % 60;
1314
1315 if (hours > 0) timeParts.push(`${hours}H`);
1316 if (minutes > 0) timeParts.push(`${minutes}M`);
1317 if (secs > 0 || absNanos > 0) {
1318 if (absNanos > 0) {
1319 // Format with fractional seconds
1320 let nanosStr = String(absNanos).padStart(9, "0");
1321 nanosStr = nanosStr.replace(/0+$/, ""); // Remove trailing zeros
1322 timeParts.push(`${secs}.${nanosStr}S`);
1323 } else {
1324 timeParts.push(`${secs}S`);
1325 }
1326 }
1327
1328 let result = "P";
1329 if (parts.length > 0) {
1330 result += parts.join("");
1331 }
1332 if (timeParts.length > 0) {
1333 result += "T" + timeParts.join("");
1334 }
1335
1336 // Handle empty duration
1337 if (result === "P") {
1338 result = "PT0S";
1339 }
1340
1341 return isNegative ? "-" + result : result;
1342 }
1343
1344 /**
1345 * Add this duration to another duration.

Callers

nothing calls this directly

Calls 1

pushMethod · 0.45

Tested by

no test coverage detected