Skip to content

Commit a68f8b1

Browse files
achingbrainhugomrdias
authored andcommitted
fix: format mtime as timespec (#20)
1 parent 5d93881 commit a68f8b1

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

src/files/format-mtime.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
'use strict'
22

33
function formatMtime (mtime) {
4-
if (mtime === undefined) {
4+
if (mtime == null) {
55
return '-'
66
}
77

8-
return new Date(mtime * 1000).toLocaleDateString(Intl.DateTimeFormat().resolvedOptions().locale, {
8+
const date = new Date((mtime.secs * 1000) + Math.round(mtime.nsecs / 1000))
9+
10+
return date.toLocaleDateString(Intl.DateTimeFormat().resolvedOptions().locale, {
911
year: 'numeric',
1012
month: 'short',
1113
day: 'numeric',

test/files/format-mtime.spec.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ const expect = chai.expect
1010

1111
describe('format-mtime', function () {
1212
it('formats mtime', function () {
13-
expect((new Date(formatMtime(0))).getTime()).to.equal(0)
13+
expect(formatMtime({ secs: 100, nsecs: 0 })).to.include('Jan 1, 1970')
14+
})
15+
16+
it('formats empty mtime', function () {
17+
expect(formatMtime()).to.equal('-')
1418
})
1519
})

0 commit comments

Comments
 (0)