javascript-time-ago icon indicating copy to clipboard operation
javascript-time-ago copied to clipboard

Test `twitter.test.js` not passing

Open GauBen opened this issue 4 years ago • 4 comments

Hello, I noticed that a test is not passing:

https://github.com/catamphetamine/javascript-time-ago/blob/ae379f7a0ed4947d0c564804799024fc181ecf0e/source/style/twitter.test.js#L72

  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)

GauBen avatar Aug 08 '21 08:08 GauBen

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

image

catamphetamine avatar Aug 08 '21 08:08 catamphetamine

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

GauBen avatar Aug 08 '21 08:08 GauBen

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.

catamphetamine avatar Aug 08 '21 08:08 catamphetamine

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

GauBen avatar Aug 08 '21 08:08 GauBen