#4248·date-fns

fromUnixTime docs still say decimal values are discarded

Author: MicroMiloCreated Jul 16, 2026Updated Jul 16, 2026

Summary

fromUnixTime currently preserves fractional seconds, but its public documentation says that decimal values are discarded. This makes the documented contract disagree with the runtime behavior for inputs such as 0.001, -0.001, and 1640888727.872.

Code path

  • pkgs/core/src/fromUnixTime/index.ts:17 documents: Decimal values will be discarded.
  • pkgs/core/src/fromUnixTime/index.ts:35 currently returns toDate(unixTime * 1000, options?.in), which preserves fractional seconds.
  • pkgs/core/src/fromUnixTime/test.ts:7-10 covers integer Unix timestamps but does not cover fractional seconds.

Steps to reproduce

On the current main revision I checked (4098115cf705e3af7f663d8e5b0686e39a9f478a):

typescript
import { fromUnixTime } from "date-fns";

console.log(fromUnixTime(0.001).getTime());
// actual: 1
// documented "decimal values will be discarded" behavior would be: 0

console.log(fromUnixTime(-0.001).getTime());
// actual: -1
// documented "decimal values will be discarded" behavior would be: 0

console.log(fromUnixTime(1640888727.872).toISOString());
// actual: "2021-12-30T18:25:27.872Z"
// documented discard behavior would be: "2021-12-30T18:25:27.000Z"

Expected behavior

The documentation and runtime behavior should agree. Either:

  1. update the documentation to state that fractional seconds are preserved as milliseconds, or
  2. change the implementation to discard decimal seconds before multiplying by 1000.

Actual behavior

The implementation preserves fractional seconds while the documentation says decimal values are discarded.

Existing coverage

This is related to #1917, which discusses whether fromUnixTime should preserve fractional seconds/milliseconds. However, the current issue is specifically about the present documentation still stating that decimal values are discarded while the current implementation preserves them.

Suggested fix

If preserving fractional seconds is now the intended behavior, update the fromUnixTime description and add a regression test for fractional seconds, for example:

typescript
expect(fromUnixTime(0.001).getTime()).toBe(1);
expect(fromUnixTime(1640888727.872).getTime()).toBe(1640888727872);

If discarding decimals is still intended, change the implementation to truncate before multiplying:

typescript
return toDate(Math.trunc(unixTime) * 1000, options?.in);

Suggested tests

  • fractional positive seconds, e.g. 0.001
  • fractional negative seconds, e.g. -0.001
  • realistic millisecond-bearing Unix timestamp, e.g. 1640888727.872

Submitted with Codex.