Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

fix(core): render current time as "just now" in Timestamp (#3159)

Cindy Zhang8d0ef8574ba2
diff --git a/.changeset/timestamp-current-time-future-phrase.md b/.changeset/timestamp-current-time-future-phrase.md
new file mode 100644
index 0000000000..ed95ba5137
--- /dev/null
+++ b/.changeset/timestamp-current-time-future-phrase.md
@@ -0,0 +1,7 @@
+---
+'@astryxdesign/core': patch
+---
+
+[fix] `Timestamp` now renders the current time (and small clock skew up to ~30s in the future) as "now" instead of "in a few seconds" (#3099).
+
+@cixzhang @durvesh1992
diff --git a/packages/core/src/Timestamp/Timestamp.test.tsx b/packages/core/src/Timestamp/Timestamp.test.tsx
index 82c6dc4495..f41225e02d 100644
--- a/packages/core/src/Timestamp/Timestamp.test.tsx
+++ b/packages/core/src/Timestamp/Timestamp.test.tsx
@@ -33,10 +33,30 @@ describe('Timestamp', () => {
expect(screen.getByText('2 hours ago')).toBeInTheDocument();
});
- it('renders "just now" for very recent times', () => {
+ it('renders "now" for very recent times', () => {
const fiveSecondsAgo = Date.now() / 1000 - 5;
render(<Timestamp value={fiveSecondsAgo} format="relative" />);
- expect(screen.getByText('just now')).toBeInTheDocument();
+ expect(screen.getByText('now')).toBeInTheDocument();
+ });
+
+ it('renders "now" for the current instant (not a future phrase)', () => {
+ // A value equal to "right now". Because the internal `now` baseline is
+ // captured at render time, it can lag the value by a fraction of a second,
+ // producing a tiny negative delta that must not be treated as the future.
+ render(<Timestamp value={Date.now() / 1000} format="relative" />);
+ expect(screen.queryByText(/^in /)).not.toBeInTheDocument();
+ expect(screen.getByText('now')).toBeInTheDocument();
+ });
+
+ it('renders "now" for a value a hair in the future (clock skew)', () => {
+ // Real-world clock / captured-now skew can make a current-ish value land a
+ // fraction of a second in the future relative to the component's internal
+ // `now`. This must read as the present ("now"), never "in a few
+ // seconds". Regression test for the right-now -> future-phrase bug.
+ const aHairInTheFuture = Date.now() / 1000 + 0.6;
+ render(<Timestamp value={aHairInTheFuture} format="relative" />);
+ expect(screen.queryByText(/^in /)).not.toBeInTheDocument();
+ expect(screen.getByText('now')).toBeInTheDocument();
});
it('renders "yesterday" for times ~1 day ago', () => {
@@ -49,11 +69,7 @@ describe('Timestamp', () => {
it('renders date format', () => {
render(
- <Timestamp
- value="2026-02-19T17:00:00Z"
- format="date"
- data-testid="ts"
- />,
+ <Timestamp value="2026-02-19T17:00:00Z" format="date" data-testid="ts" />,
);
const el = screen.getByTestId('ts');
expect(el.textContent).toContain('2026');
@@ -77,11 +93,7 @@ describe('Timestamp', () => {
it('renders time format', () => {
render(
- <Timestamp
- value="2026-02-19T17:00:00Z"
- format="time"
- data-testid="ts"
- />,
+ <Timestamp value="2026-02-19T17:00:00Z" format="time" data-testid="ts" />,
);
const el = screen.getByTestId('ts');
// Should contain time but not year
@@ -197,7 +209,7 @@ describe('Timestamp', () => {
it('live updates relative time', () => {
const now = Date.now() / 1000;
render(<Timestamp value={now - 5} format="relative" isLive />);
- expect(screen.getByText('just now')).toBeInTheDocument();
+ expect(screen.getByText('now')).toBeInTheDocument();
act(() => {
vi.advanceTimersByTime(30_000);
@@ -210,11 +222,7 @@ describe('Timestamp', () => {
it('forwards ref', () => {
const ref = {current: null as HTMLTimeElement | null};
render(
- <Timestamp
- ref={ref}
- value="2026-03-25T10:00:00Z"
- format="date_time"
- />,
+ <Timestamp ref={ref} value="2026-03-25T10:00:00Z" format="date_time" />,
);
expect(ref.current).toBeInstanceOf(HTMLTimeElement);
});
@@ -240,6 +248,24 @@ describe('Timestamp', () => {
expect(screen.getByText('in 1 hour')).toBeInTheDocument();
});
+ it('renders "now" for a value a few seconds in the future (clock skew)', () => {
+ // Beyond the sub-second render lag but still within the skew tolerance: a
+ // value ~20s ahead of our clock is almost always skew (the value's clock
+ // running fast), not a genuine future event, so it should read as the
+ // present rather than "in a few seconds".
+ const twentySecondsFromNow = Date.now() / 1000 + 20;
+ render(<Timestamp value={twentySecondsFromNow} format="relative" />);
+ expect(screen.queryByText(/^in /)).not.toBeInTheDocument();
+ expect(screen.getByText('now')).toBeInTheDocument();
+ });
+
+ it('renders a genuine near-future time beyond the skew tolerance', () => {
+ // Past the skew window — this is a real upcoming time, not clock drift.
+ const fortyFiveSecondsFromNow = Date.now() / 1000 + 45;
+ render(<Timestamp value={fortyFiveSecondsFromNow} format="relative" />);
+ expect(screen.getByText('in a few seconds')).toBeInTheDocument();
+ });
+
// --- Long-ago relative ---
it('renders months ago for dates older than 30 days', () => {
diff --git a/packages/core/src/Timestamp/Timestamp.tsx b/packages/core/src/Timestamp/Timestamp.tsx
index cc8f7be650..59db3183f3 100644
--- a/packages/core/src/Timestamp/Timestamp.tsx
+++ b/packages/core/src/Timestamp/Timestamp.tsx
@@ -19,12 +19,7 @@
import {useEffect, useRef, useState, lazy, Suspense} from 'react';
import * as stylex from '@stylexjs/stylex';
import {Text} from '../Text';
-import type {
- TextType,
- TextSize,
- TextColor,
- TextWeight,
-} from '../theme/types';
+import type {TextType, TextSize, TextColor, TextWeight} from '../theme/types';
import {mergeProps, mergeRefs} from '../utils';
import type {BaseProps} from '../BaseProps';
import {themeProps} from '../utils/themeProps';
@@ -54,7 +49,7 @@ export interface TimestampProps extends BaseProps<HTMLTimeElement> {
value: string | number;
/**
* Display format.
- * - `'relative'`: "2 hours ago", "yesterday", "just now"
+ * - `'relative'`: "2 hours ago", "yesterday", "now"
* - `'auto'`: Relative for recent times, `date_time` for older
* - `'date'`: "Mar 21, 2025"
* - `'date_time'`: "Mar 21, 2025, 2:51 PM"
@@ -138,6 +133,18 @@ const YEAR = 365 * DAY;
/** Default auto threshold: 7 days in seconds */
const DEFAULT_AUTO_THRESHOLD = 7 * DAY;
+/**
+ * Tolerance (in seconds) for treating a *future* timestamp as the present.
+ * A value only a handful of seconds ahead of our reference clock is almost
+ * always clock skew — the displayed `now` lagging the real clock, or the value
+ * being produced on a slightly faster clock — not a genuine future event, so
+ * it reads as "now" rather than a confusing "in a few seconds". The future
+ * side gets a wider window than the past (which only needs to absorb the
+ * sub-second render-time lag) because future drift is far more likely to be
+ * skew than real.
+ */
+const FUTURE_SKEW_TOLERANCE = 30;
+
function parseValue(value: string | number): Date {
if (typeof value === 'number') {
// Heuristic: if the number is less than 1e12, treat as seconds; otherwise ms.
@@ -150,9 +157,25 @@ function parseValue(value: string | number): Date {
function getRelativeTimeString(date: Date, now: Date): string {
const diffSeconds = Math.round((now.getTime() - date.getTime()) / 1000);
+ // Treat values at (or a hair before/after) the present as "now". The
+ // internal `now` reference is captured at render time, so it can lag the
+ // real clock; a value equal to "right now" can land a fraction of a second
+ // in the future and round to a small negative delta. Without this clamp,
+ // such values fall into the future branch and render "in a few seconds".
+ if (Math.abs(diffSeconds) < 10) {
+ return 'now';
+ }
+
if (diffSeconds < 0) {
// Future dates
const absDiff = Math.abs(diffSeconds);
+ // A value only a few seconds ahead of our clock is almost always skew, not
+ // a genuine future event — render it as the present rather than a
+ // confusing "in a few seconds". Wider than the past window above on
+ // purpose (see FUTURE_SKEW_TOLERANCE).
+ if (absDiff <= FUTURE_SKEW_TOLERANCE) {
+ return 'now';
+ }
if (absDiff < MINUTE) {
return 'in a few seconds';
}
@@ -176,9 +199,6 @@ function getRelativeTimeString(date: Date, now: Date): string {
return `in ${years} ${years === 1 ? 'year' : 'years'}`;
}
- if (diffSeconds < 10) {
- return 'just now';
- }
if (diffSeconds < MINUTE) {
return `${diffSeconds} seconds ago`;
}