From a68f8b18cdd0de2c4c491bd39df6f821029ddded Mon Sep 17 00:00:00 2001 From: Alex Potsides Date: Wed, 8 Jan 2020 19:30:03 +0000 Subject: [PATCH] fix: format mtime as timespec (#20) --- src/files/format-mtime.js | 6 ++++-- test/files/format-mtime.spec.js | 6 +++++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/files/format-mtime.js b/src/files/format-mtime.js index 5ce121e..486cecb 100644 --- a/src/files/format-mtime.js +++ b/src/files/format-mtime.js @@ -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', diff --git a/test/files/format-mtime.spec.js b/test/files/format-mtime.spec.js index d7a6eae..80c44ad 100644 --- a/test/files/format-mtime.spec.js +++ b/test/files/format-mtime.spec.js @@ -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('-') }) })