( temporal: TemporalValue, duration: DurationValue, )
| 1455 | * Add a duration to a temporal value. |
| 1456 | */ |
| 1457 | export function addDuration( |
| 1458 | temporal: TemporalValue, |
| 1459 | duration: DurationValue, |
| 1460 | ): TemporalValue | null { |
| 1461 | if (temporal instanceof DateValue) { |
| 1462 | // Add months with proper month-end handling |
| 1463 | const afterMonths = addMonthsToDate( |
| 1464 | temporal.year, |
| 1465 | temporal.month, |
| 1466 | temporal.day, |
| 1467 | duration.months, |
| 1468 | ); |
| 1469 | |
| 1470 | // Now add days |
| 1471 | const date = new Date( |
| 1472 | Date.UTC(afterMonths.year, afterMonths.month - 1, afterMonths.day + duration.days), |
| 1473 | ); |
| 1474 | |
| 1475 | // Add time components (convert to days) |
| 1476 | const totalSeconds = duration.seconds + duration.nanoseconds / 1_000_000_000; |
| 1477 | const additionalDays = Math.floor(totalSeconds / 86400); |
| 1478 | date.setUTCDate(date.getUTCDate() + additionalDays); |
| 1479 | |
| 1480 | return new DateValue(date.getUTCFullYear(), date.getUTCMonth() + 1, date.getUTCDate()); |
| 1481 | } |
| 1482 | |
| 1483 | if (temporal instanceof LocalTimeValue) { |
| 1484 | // Add duration to local time (only time components matter, wraps at 24h) |
| 1485 | let totalNanos = |
| 1486 | temporal.hour * 3_600_000_000_000 + |
| 1487 | temporal.minute * 60_000_000_000 + |
| 1488 | temporal.second * 1_000_000_000 + |
| 1489 | temporal.nanosecond + |
| 1490 | duration.seconds * 1_000_000_000 + |
| 1491 | duration.nanoseconds; |
| 1492 | |
| 1493 | // Handle days overflow (wrap around 24 hours) |
| 1494 | const nanosPerDay = 86_400_000_000_000; |
| 1495 | totalNanos = ((totalNanos % nanosPerDay) + nanosPerDay) % nanosPerDay; |
| 1496 | |
| 1497 | const hour = Math.floor(totalNanos / 3_600_000_000_000); |
| 1498 | const remainingAfterHours = totalNanos % 3_600_000_000_000; |
| 1499 | const minute = Math.floor(remainingAfterHours / 60_000_000_000); |
| 1500 | const remainingAfterMinutes = remainingAfterHours % 60_000_000_000; |
| 1501 | const second = Math.floor(remainingAfterMinutes / 1_000_000_000); |
| 1502 | const nanosecond = Math.round(remainingAfterMinutes % 1_000_000_000); |
| 1503 | |
| 1504 | return new LocalTimeValue(hour, minute, second, nanosecond); |
| 1505 | } |
| 1506 | |
| 1507 | if (temporal instanceof TimeValue) { |
| 1508 | // Add duration to time with offset |
| 1509 | let totalNanos = |
| 1510 | temporal.hour * 3_600_000_000_000 + |
| 1511 | temporal.minute * 60_000_000_000 + |
| 1512 | temporal.second * 1_000_000_000 + |
| 1513 | temporal.nanosecond + |
| 1514 | duration.seconds * 1_000_000_000 + |
no test coverage detected