Skip to content

Conversation

@khiga8
Copy link
Contributor

@khiga8 khiga8 commented Oct 21, 2025

Relates to: https://github.com/github/fixing-oss-pain-points/issues/148

Warning

This is an experimental feature. It may be modified or removed at any time!

This PR adds support for rendering absolute time format if the consumer has a data-prefers-absolute-time attribute on the body or html tag, set to true.

return `${this.prefix} ${formatter.format(date)}`.trim()
}

#getUserPreferredAbsoluteTimeFormat(date: Date): string {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For now, matching the title format.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For other potential reviewers, here's an example of what this looks like: Oct 23, 2025 6:59 AM EDT


#shouldDisplayUserPreferredAbsoluteTime(format: ResolvedFormat): boolean {
// Never override duration format with absolute format.
if (format === 'duration') return false
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think... this makes sense? @zaataylor check me on this :D

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes sense! 🫡 This will also work for elapsed format since they're aliases

// elapsed is an alias for 'duration'
if (format === 'elapsed') return 'duration'

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh and for micro, too:

// 'micro' is an alias for 'duration'
if (format === 'micro') return 'duration'

Comment on lines +248 to +249
this.ownerDocument.documentElement.getAttribute('data-prefers-absolute-time') === 'true' ||
this.ownerDocument.body?.getAttribute('data-prefers-absolute-time') === 'true'
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

adding support for body and html, based on @dgreif comment

staff only: slack

@khiga8 khiga8 requested review from dgreif and zaataylor October 21, 2025 17:55
@khiga8 khiga8 marked this pull request as ready for review October 21, 2025 18:39
@khiga8 khiga8 requested a review from a team as a code owner October 21, 2025 18:39
Copilot AI review requested due to automatic review settings October 21, 2025 18:39
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR introduces an experimental feature to support absolute time formatting based on user preference. When the data-prefers-absolute-time attribute is set to "true" on either the html or body element, relative time elements will display in absolute format instead of relative format.

Key Changes:

  • Added logic to detect and respect the data-prefers-absolute-time preference from document or body elements
  • Implemented absolute time formatting with locale-aware date/time display
  • Added comprehensive test coverage for the new feature including edge cases

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
src/relative-time-element.ts Implements the core absolute time formatting logic and preference detection
test/relative-time.js Adds test suite covering various scenarios for the absolute time feature
package.json Reorders lint commands to run TypeScript checks before ESLint

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Copy link
Contributor

@zaataylor zaataylor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall, these changes look good to me!

return `${this.prefix} ${formatter.format(date)}`.trim()
}

#getUserPreferredAbsoluteTimeFormat(date: Date): string {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For other potential reviewers, here's an example of what this looks like: Oct 23, 2025 6:59 AM EDT


#shouldDisplayUserPreferredAbsoluteTime(format: ResolvedFormat): boolean {
// Never override duration format with absolute format.
if (format === 'duration') return false
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes sense! 🫡 This will also work for elapsed format since they're aliases

// elapsed is an alias for 'duration'
if (format === 'elapsed') return 'duration'


#shouldDisplayUserPreferredAbsoluteTime(format: ResolvedFormat): boolean {
// Never override duration format with absolute format.
if (format === 'duration') return false
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh and for micro, too:

// 'micro' is an alias for 'duration'
if (format === 'micro') return 'duration'

@zaataylor zaataylor merged commit 00f47f8 into main Oct 22, 2025
4 checks passed
@khiga8 khiga8 deleted the tiny-wins-absolute-time-setting branch October 22, 2025 17:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants