Skip to content

Commit

Permalink
fix: format mtime as timespec (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
achingbrain authored and hugomrdias committed Jan 8, 2020
1 parent 5d93881 commit a68f8b1
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
6 changes: 4 additions & 2 deletions src/files/format-mtime.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
'use strict'

function formatMtime (mtime) {
if (mtime === undefined) {
if (mtime == null) {
return '-'
}

return new Date(mtime * 1000).toLocaleDateString(Intl.DateTimeFormat().resolvedOptions().locale, {
const date = new Date((mtime.secs * 1000) + Math.round(mtime.nsecs / 1000))

return date.toLocaleDateString(Intl.DateTimeFormat().resolvedOptions().locale, {
year: 'numeric',
month: 'short',
day: 'numeric',
Expand Down
6 changes: 5 additions & 1 deletion test/files/format-mtime.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ const expect = chai.expect

describe('format-mtime', function () {
it('formats mtime', function () {
expect((new Date(formatMtime(0))).getTime()).to.equal(0)
expect(formatMtime({ secs: 100, nsecs: 0 })).to.include('Jan 1, 1970')
})

it('formats empty mtime', function () {
expect(formatMtime()).to.equal('-')
})
})

0 comments on commit a68f8b1

Please sign in to comment.