Skip to content
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

Cannot get format=relative to display seconds #269

Open
bear24rw opened this issue Nov 14, 2023 · 2 comments
Open

Cannot get format=relative to display seconds #269

bear24rw opened this issue Nov 14, 2023 · 2 comments

Comments

@bear24rw
Copy link

I cannot get relative format to display second level precision. The docs suggest that the default precision is seconds and this table suggests that with format relative i should see seconds. But I only see now until 1 minute has passed. format=elapsed works as i expect.

image
<html>
  <body>
    <p>Loaded this page <relative-time datetime="" format="relative" id="dynamic1"></relative-time></p>
    <p>You've been on this page for <relative-time datetime="" format="duration" id="dynamic2"></relative-time></p>
    <script type="module" src="https://unpkg.com/@github/relative-time-element@latest/dist/bundle.js"></script>
    <script>
    document.getElementById('dynamic1').date = new Date()
    document.getElementById('dynamic2').date = new Date()
    </script>
  </body>
</html>
@keithamus
Copy link
Member

Sounds like this is a bug. Following the code I can't see where there's a problem. If you would kindly submit a PR with some failing tests we can investigate further.

@rhcarvalho
Copy link

Of the non-deprecated formats, only format="duration" is showing and updating with seconds precision.
The default (no format attribute) and format="relative" doesn't update until after 1 min has passed.

duration is not a replacement for relative because it doesn't have the prefix/suffix: "X seconds" vs "X seconds ago".

Tested with v4.3.1.


I think I found the issue debugging with devtools:

} else if (format === 'relative') {
newText = this.#getRelativeFormat(duration)

#getRelativeFormat(duration: Duration): string {

if (unit === 'second' && int < 10) {
return relativeFormat.format(0, 'second')
}

Times in the past (like -X seconds) would always enter the if branch and format the value 0 ("now") instead of the number of seconds.

Maybe what was meant is:

    if (unit === 'second' && Math.abs(int) < 10) {
        return relativeFormat.format(0, 'second');
    }

Such that we don't show "now", 1, 2, 3..., 9, but instead go "now", 10, 11, ...

I'm not sure though, I think that's unspecified behavior.

I traced it back to 6118a16.

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

No branches or pull requests

3 participants