Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/library/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@magmacomputing/library",
"version": "2.11.0",
"version": "2.12.0",
Comment thread
magmacomputing marked this conversation as resolved.
Outdated
"description": "Shared utility library for Tempo",
"author": "Magma Computing Solutions",
"license": "MIT",
Expand Down Expand Up @@ -99,4 +99,4 @@
"optionalDependencies": {
"@js-temporal/polyfill": "^0.5.1"
}
}
}
26 changes: 16 additions & 10 deletions packages/tempo/doc/tempo.duration.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const duration = now.until(xmas);
now.until('afternoon', 'minutes'); // → 84.45 (fractional: 'afternoon' has a fixed time)
now.until('xmas', 'days'); // → 219 (whole number — see note below)
now.until('xmas', 'weeks'); // → 31.28 (fractional — weeks don't divide evenly into days)
now.until(Tempo.Now(), 'hours'); // → 48 (targets can also be Temporal/Tempo instances)
now.until(Tempo.now(), 'hours'); // → 48 (targets can also be Temporal/Tempo instances)
Comment thread
magmacomputing marked this conversation as resolved.
Outdated
```

::: tip Date-only targets inherit the current time
Expand All @@ -41,23 +41,24 @@ When a target resolves to a **date without a time component** (e.g. `'xmas'`, `'
This matches natural-language intuition: *"How many days until Christmas?"* expects `219`, not `219.43`. Targets with an **explicit time** (e.g. `'afternoon'`, `'9am'`) always produce fractional values because the target time differs from the anchor's current time-of-day.
:::

::: warning Return Types
If you call `.until()` **without** a unit, it returns a `Tempo.Duration` object, onto which you can chain `.balance()` and `.format()` (see below).
If you provide a unit (like `'days'`), it returns a primitive JavaScript `Number`. Calling `.balance()` on a Number will throw an error.
:::

### `.since()`
Calculates the time elapsed *since* a past date. By default, it returns a human-readable localized string (powered by `Intl.RelativeTimeFormat`).

```javascript
const now = new Tempo({ locale: 'en-US' });
const birthday = new Tempo('1990-05-10');

// 1. Returns localized relative string based on the given unit
birthday.since('now', 'years'); // → "36 years ago" (depending on locale)
birthday.since('now', 'days'); // → "13,150 days ago"
now.since(birthday, 'years'); // → "36 years ago" (depending on locale)
now.since(birthday, 'days'); // → "13,150 days ago"

// 2. Pass a custom formatter for natural language output (e.g. "yesterday")
const yesterday = now.add({ days: -1 });
const autoFormat = new Intl.RelativeTimeFormat('en-US', { numeric: 'auto' });
now.since(yesterday, { unit: 'days', intl: { relativeTime: { format: autoFormat } } }); // → "yesterday"

// 2. Returns an ISO 8601 Duration String if no unit is provided
birthday.since('yesterday'); // → "-P35Y11M29DT9H32M19.402S"
// 3. Returns an ISO 8601 Duration String if no unit is provided
now.since(birthday); // → "-P36Y..."
```

::: info Return Type
Expand All @@ -77,6 +78,11 @@ console.log(dur.days); // 15

## Intelligent Balancing

::: warning Return Types
If you call `.until()` **without** a unit, it returns a `Tempo.Duration` object, onto which you can chain `.balance()` and `.format()` (see below).
If you provide a unit (like `'days'`), it returns a primitive JavaScript `Number`. Calling `.balance()` on a Number will throw an error.
:::

Sometimes you have a raw number of days (e.g. `365 days`) and you want to mathematically "balance" it into larger units (like `1 year`). Tempo provides the `.balance()` method directly on the Duration object.

### Strict Calendar Math
Expand Down
4 changes: 2 additions & 2 deletions packages/tempo/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@magmacomputing/tempo",
"version": "2.11.0",
"version": "2.12.0",
"description": "The Tempo core library",
"author": "Magma Computing Solutions",
"license": "MIT",
Expand Down Expand Up @@ -242,4 +242,4 @@
"doc": "doc",
"test": "test"
}
}
}
2 changes: 1 addition & 1 deletion packages/tempo/src/module/module.duration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ function duration(this: Tempo, type: 'until' | 'since', arg?: any, until?: any)
.map(nbr => nbr.toString().padStart(3, '0'))
.join('')
const rtConfig = (this as any).config.intl?.relativeTime;
const rtOptions = opts['relativeTime'];
const rtOptions = opts['intl']?.relativeTime || opts['relativeTime'];

const rtf = (isFunction(rtOptions) ? rtOptions : rtOptions?.format)
|| (isFunction(rtConfig) ? rtConfig : rtConfig?.format)
Expand Down
Loading