catamphetamine / javascript-time-ago

International highly customizable relative date/time formatting

Home Page:https://catamphetamine.gitlab.io/react-time-ago/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Test `twitter.test.js` not passing

GauBen opened this issue · comments

Hello, I noticed that a test is not passing:

formatDatePastBy(-1 * month * 8).should.equal('Dec 10')

  1) style/twitter
       should format Twitter style relative time (English) (absolute dates):

      AssertionError: expected 'Dec 9' to equal 'Dec 10'
      + expected - actual

      -Dec 9
      +Dec 10
      
      at Context.equal (source/style/twitter.test.js:72:43)
      at processImmediate (node:internal/timers:464:21)

Hmmm.
On my machine, all tests seem to pass:

image

image

That's weird, and I don't know how to help but:

  • Current date on my machine Sun Aug 8 10:28:47 CEST 2021
  • Current timezone CEST +0200
  • Daylight saving time (DST) yes

The issue could be somehow related to time zones.

The test creates a date:

new Date(2016, 3, 10, 12, 0)

The date is gonna be at 12:00 in the current user's time zone.
When I'm running it, it's a date corresponding to 12:00 in Moscow.
When you're running it, it's a date corresponding to 12:00 in France.

But, at the same time, the date formatter used in twitter style format should also format the date in the same time zone, so it shouldn't be an issue:

var d = new Date(2016, 3, 10, 12, 0)
console.log(d)
// Outputs: Sun Apr 10 2016 12:00:00 GMT+0300 (Moscow Standard Time)

var f = new Intl.DateTimeFormat('en', {
  month: 'short',
  day: 'numeric',
  hour: 'numeric',
  minute: 'numeric'
})
console.log(f.format(a))
// Outputs: "Apr 10, 12:00 PM"

So, regardless from the user's time zone, it should output the same results.
But in your case it outputs Dec 9 instead of Dec 10.
That's weird.
That shouldn't be possible.

Another thing is that month * 8 is 243.52 and there are 244 days between Apr 10th and Dec 10th

formatDatePastBy(-243.55 * 24 * 60 * 60).should.equal('Dec 10') // Passes
formatDatePastBy(-243.54 * 24 * 60 * 60).should.equal('Dec 10') // Fails