(start: TemporalValue, end: TemporalValue)
| 1635 | * Compute the duration between two temporal values. |
| 1636 | */ |
| 1637 | export function durationBetween(start: TemporalValue, end: TemporalValue): DurationValue | null { |
| 1638 | // Handle dates |
| 1639 | if (start instanceof DateValue && end instanceof DateValue) { |
| 1640 | const _startDate = new Date(Date.UTC(start.year, start.month - 1, start.day)); |
| 1641 | const endDate = new Date(Date.UTC(end.year, end.month - 1, end.day)); |
| 1642 | |
| 1643 | // Calculate difference in months |
| 1644 | let months = (end.year - start.year) * 12 + (end.month - start.month); |
| 1645 | |
| 1646 | // Adjust for day difference |
| 1647 | if (end.day < start.day) { |
| 1648 | months--; |
| 1649 | } |
| 1650 | |
| 1651 | // Calculate remaining days |
| 1652 | const adjustedStart = new Date(Date.UTC(start.year, start.month - 1 + months, start.day)); |
| 1653 | const days = Math.round((endDate.getTime() - adjustedStart.getTime()) / (24 * 60 * 60 * 1000)); |
| 1654 | |
| 1655 | return new DurationValue(months, days, 0, 0); |
| 1656 | } |
| 1657 | |
| 1658 | // Handle local times |
| 1659 | if (start instanceof LocalTimeValue && end instanceof LocalTimeValue) { |
| 1660 | const startNanos = |
| 1661 | start.hour * 3_600_000_000_000 + |
| 1662 | start.minute * 60_000_000_000 + |
| 1663 | start.second * 1_000_000_000 + |
| 1664 | start.nanosecond; |
| 1665 | const endNanos = |
| 1666 | end.hour * 3_600_000_000_000 + |
| 1667 | end.minute * 60_000_000_000 + |
| 1668 | end.second * 1_000_000_000 + |
| 1669 | end.nanosecond; |
| 1670 | |
| 1671 | const diffNanos = endNanos - startNanos; |
| 1672 | const seconds = Math.trunc(diffNanos / 1_000_000_000); |
| 1673 | const nanoseconds = diffNanos % 1_000_000_000; |
| 1674 | |
| 1675 | return new DurationValue(0, 0, seconds, nanoseconds); |
| 1676 | } |
| 1677 | |
| 1678 | // Handle times with offset (convert to common reference) |
| 1679 | if (start instanceof TimeValue && end instanceof TimeValue) { |
| 1680 | const startNanos = |
| 1681 | (start.hour * 3600 + start.minute * 60 + start.second - start.offset) * 1_000_000_000 + |
| 1682 | start.nanosecond; |
| 1683 | const endNanos = |
| 1684 | (end.hour * 3600 + end.minute * 60 + end.second - end.offset) * 1_000_000_000 + |
| 1685 | end.nanosecond; |
| 1686 | |
| 1687 | const diffNanos = endNanos - startNanos; |
| 1688 | const seconds = Math.trunc(diffNanos / 1_000_000_000); |
| 1689 | const nanoseconds = diffNanos % 1_000_000_000; |
| 1690 | |
| 1691 | return new DurationValue(0, 0, seconds, nanoseconds); |
| 1692 | } |
| 1693 | |
| 1694 | // Handle local datetimes |
no outgoing calls
no test coverage detected