-
Notifications
You must be signed in to change notification settings - Fork 5.6k
fix: handle numeric timestamps and prevent RangeError in calculateTimeAgo #9688
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
UGilfoyle
wants to merge
2
commits into
makeplane:preview
Choose a base branch
from
UGilfoyle:fix/datetime-safe-calculate-time-ago
base: preview
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| /** | ||
| * Copyright (c) 2023-present Plane Software, Inc. and contributors | ||
| * SPDX-License-Identifier: AGPL-3.0-only | ||
| * See the LICENSE file for details. | ||
| */ | ||
|
|
||
| import { describe, expect, it } from "vitest"; | ||
| import { calculateTimeAgo, calculateTimeAgoShort } from "../src/datetime"; | ||
|
|
||
| describe("calculateTimeAgo and calculateTimeAgoShort", () => { | ||
| describe("calculateTimeAgo", () => { | ||
| it("should format Date instances correctly", () => { | ||
| const oneHourAgo = new Date(Date.now() - 3600 * 1000); | ||
| expect(calculateTimeAgo(oneHourAgo)).toMatch(/about 1 hour ago|1 hour ago/); | ||
| }); | ||
|
|
||
| it("should format numeric millisecond timestamps without throwing RangeError", () => { | ||
| const oneMinuteAgo = Date.now() - 60 * 1000; | ||
| expect(calculateTimeAgo(oneMinuteAgo)).toMatch(/1 minute ago|minute ago/); | ||
| }); | ||
|
|
||
| it("should format ISO strings without throwing", () => { | ||
| const oneDayAgo = new Date(Date.now() - 24 * 3600 * 1000).toISOString(); | ||
| expect(calculateTimeAgo(oneDayAgo)).toMatch(/1 day ago|yesterday/); | ||
| }); | ||
|
|
||
| it("should safely return empty string for null, undefined, or invalid inputs", () => { | ||
| expect(calculateTimeAgo(null)).toBe(""); | ||
| expect(calculateTimeAgo(undefined)).toBe(""); | ||
| expect(calculateTimeAgo("")).toBe(""); | ||
| expect(calculateTimeAgo("invalid-date-string")).toBe(""); | ||
| expect(calculateTimeAgo(NaN)).toBe(""); | ||
| }); | ||
| }); | ||
|
|
||
| describe("calculateTimeAgoShort", () => { | ||
| it("should format elapsed time into compact units", () => { | ||
| const now = Date.now(); | ||
| expect(calculateTimeAgoShort(now - 30 * 1000)).toMatch(/^(29|30|31)s$/); | ||
| expect(calculateTimeAgoShort(now - 5 * 60 * 1000)).toBe("5m"); | ||
| expect(calculateTimeAgoShort(now - 3 * 3600 * 1000)).toBe("3h"); | ||
| expect(calculateTimeAgoShort(now - 4 * 24 * 3600 * 1000)).toBe("4d"); | ||
| expect(calculateTimeAgoShort(now - 60 * 24 * 3600 * 1000)).toBe("2mo"); | ||
| expect(calculateTimeAgoShort(now - 750 * 24 * 3600 * 1000)).toBe("2y"); | ||
| }); | ||
|
|
||
| it("should return 0s for future dates", () => { | ||
| expect(calculateTimeAgoShort(Date.now() + 60 * 1000)).toBe("0s"); | ||
| }); | ||
|
|
||
| it("should safely return empty string for invalid inputs", () => { | ||
| expect(calculateTimeAgoShort(null)).toBe(""); | ||
| expect(calculateTimeAgoShort(undefined)).toBe(""); | ||
| expect(calculateTimeAgoShort("")).toBe(""); | ||
| expect(calculateTimeAgoShort("invalid-date")).toBe(""); | ||
| expect(calculateTimeAgoShort(NaN)).toBe(""); | ||
| }); | ||
| }); | ||
| }); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
Handle all valid numeric timestamp inputs.
The current guards return
""for0, even though Unix epoch0is valid. Numeric Unix-second values are also interpreted as milliseconds, and numeric timestamp strings are not normalized consistently. This can produce incorrect or missing time-ago output.Update both helpers to distinguish only
null,undefined, and empty strings before parsing, normalize millisecond/second numbers and numeric strings consistently, and add regression tests for epoch0, Unix seconds, and numeric timestamp strings.Also applies to: 188-190.
📍 Affects 1 file
packages/utils/src/datetime.ts#L172-L174(this comment)packages/utils/src/datetime.ts#L174-L174🤖 Prompt for AI Agents
Source: Coding guidelines