(start: TemporalValue, end: TemporalValue)
| 1812 | * Compute the duration between two temporal values in seconds only. |
| 1813 | */ |
| 1814 | export function durationInSeconds(start: TemporalValue, end: TemporalValue): DurationValue | null { |
| 1815 | // Handle time types |
| 1816 | if ( |
| 1817 | (start instanceof LocalTimeValue || start instanceof TimeValue) && |
| 1818 | (end instanceof LocalTimeValue || end instanceof TimeValue) |
| 1819 | ) { |
| 1820 | let startSeconds: number; |
| 1821 | let endSeconds: number; |
| 1822 | |
| 1823 | if (start instanceof TimeValue) { |
| 1824 | startSeconds = start.hour * 3600 + start.minute * 60 + start.second - start.offset; |
| 1825 | } else { |
| 1826 | startSeconds = start.hour * 3600 + start.minute * 60 + start.second; |
| 1827 | } |
| 1828 | |
| 1829 | if (end instanceof TimeValue) { |
| 1830 | endSeconds = end.hour * 3600 + end.minute * 60 + end.second - end.offset; |
| 1831 | } else { |
| 1832 | endSeconds = end.hour * 3600 + end.minute * 60 + end.second; |
| 1833 | } |
| 1834 | |
| 1835 | const seconds = endSeconds - startSeconds; |
| 1836 | const nanoseconds = |
| 1837 | (end instanceof LocalTimeValue ? end.nanosecond : end.nanosecond) - |
| 1838 | (start instanceof LocalTimeValue ? start.nanosecond : start.nanosecond); |
| 1839 | |
| 1840 | return new DurationValue(0, 0, seconds, nanoseconds); |
| 1841 | } |
| 1842 | |
| 1843 | // Handle datetime types |
| 1844 | if ( |
| 1845 | (start instanceof LocalDateTimeValue || start instanceof DateTimeValue) && |
| 1846 | (end instanceof LocalDateTimeValue || end instanceof DateTimeValue) |
| 1847 | ) { |
| 1848 | const startDate = new Date( |
| 1849 | Date.UTC(start.year, start.month - 1, start.day, start.hour, start.minute, start.second), |
| 1850 | ); |
| 1851 | if (start instanceof DateTimeValue) { |
| 1852 | startDate.setTime(startDate.getTime() - start.offset * 1000); |
| 1853 | } |
| 1854 | |
| 1855 | const endDate = new Date( |
| 1856 | Date.UTC(end.year, end.month - 1, end.day, end.hour, end.minute, end.second), |
| 1857 | ); |
| 1858 | if (end instanceof DateTimeValue) { |
| 1859 | endDate.setTime(endDate.getTime() - end.offset * 1000); |
| 1860 | } |
| 1861 | |
| 1862 | const diffMs = endDate.getTime() - startDate.getTime(); |
| 1863 | const seconds = Math.floor(diffMs / 1000); |
| 1864 | const nanoseconds = (diffMs % 1000) * 1_000_000 + (end.nanosecond - start.nanosecond); |
| 1865 | |
| 1866 | return new DurationValue(0, 0, seconds, nanoseconds); |
| 1867 | } |
| 1868 | |
| 1869 | return null; |
| 1870 | } |
| 1871 |
no outgoing calls
no test coverage detected