Skip to content

useTemporalCalculus for Time

A minimal time library with fundamental operations that compose into powerful temporal manipulations

useTemporal

Quick Start

bash
npm install usetemporal
bash
pnpm add usetemporal
bash
yarn add usetemporal
typescript
import { createTemporal, usePeriod, divide } from 'usetemporal'

const temporal = createTemporal({ date: new Date() })
const month = usePeriod(temporal, 'month')
const days = divide(temporal, month.value, 'day')

Philosophy: Calculus for Time

Like calculus provides fundamental operations (dx, ∫) that compose into powerful tools, useTemporal provides fundamental time operations that compose into complex temporal manipulations.

typescript
// Fundamental operations - like dx and ∫
period(temporal, date, unit)        // Create any period
divide(temporal, period, unit)      // Hierarchical subdivision
merge(temporal, periods)            // Combine periods
next/previous/go                    // Relative navigation

// Everything else is composition
const daysInMonth = (month) => divide(temporal, month, 'day')
const businessDays = (period) => divide(temporal, period, 'day')
  .filter(day => ![0, 6].includes(day.date.getDay()))

Why useTemporal?

Traditional Approach - Manual and error-prone:

javascript
const startOfMonth = new Date(date.getFullYear(), date.getMonth(), 1)
const endOfMonth = new Date(date.getFullYear(), date.getMonth() + 1, 0)
const days = []
for (let d = new Date(startOfMonth); d <= endOfMonth; d.setDate(d.getDate() + 1)) {
  days.push(new Date(d))
}

useTemporal - Elegant and reactive:

typescript
const month = usePeriod(temporal, 'month')
const days = divide(temporal, month.value, 'day')
// Automatically updates when month changes

Key Features

Hierarchical Time

typescript
const year = usePeriod(temporal, 'year')
const months = divide(temporal, year.value, 'month')
const days = divide(temporal, months[0], 'day')
const hours = divide(temporal, days[0], 'hour')

Built-in Reactivity

typescript
import { watch } from '@vue/reactivity'

watch(month, (newMonth) => {
  console.log('Month changed:', newMonth.date)
})

Custom Units

typescript
import { defineUnit } from 'usetemporal'

defineUnit('fortnight', {
  duration: { weeks: 2 },
  validate: (adapter, date) => true
})

const fortnight = usePeriod(temporal, 'fortnight')

Ready to Start?

Released under the MIT License.