diff --git a/src/utils.ts b/src/utils.ts index e6299dbb..2f55a6ee 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -182,22 +182,6 @@ export function encodeDirListing(data: Record): Uint8Array { export type Callback = (e?: ErrnoError, ...args: OptionalTuple) => unknown; -/** - * converts Date or number to a integer UNIX timestamp - * Grabbed from NodeJS sources (lib/fs.js) - * - * @internal - */ -export function _toUnixTimestamp(time: Date | number): number { - if (typeof time === 'number') { - return Math.floor(time); - } - if (time instanceof Date) { - return Math.floor(time.getTime() / 1000); - } - throw new Error('Cannot parse time'); -} - /** * Normalizes a mode * @internal @@ -230,15 +214,11 @@ export function normalizeTime(time: string | number | Date): Date { return time; } - if (typeof time == 'number') { - return new Date(time * 1000); - } - - if (typeof time == 'string') { + try { return new Date(time); + } catch { + throw new ErrnoError(Errno.EINVAL, 'Invalid time.'); } - - throw new ErrnoError(Errno.EINVAL, 'Invalid time.'); } /** diff --git a/tests/fs/times.test.ts b/tests/fs/times.test.ts index e65b992b..e3a6010c 100644 --- a/tests/fs/times.test.ts +++ b/tests/fs/times.test.ts @@ -2,22 +2,33 @@ import assert from 'node:assert'; import { suite, test } from 'node:test'; import { wait } from 'utilium'; import { ErrnoError } from '../../dist/error.js'; -import { _toUnixTimestamp } from '../../dist/utils.js'; +import type { StatsLike } from '../../dist/stats.js'; import { fs } from '../common.js'; const path = 'x.txt'; -suite('times', () => { - function expect_assert(resource: string | number, atime: Date | number, mtime: Date | number) { - const stats = typeof resource == 'string' ? fs.statSync(resource) : fs.fstatSync(resource); - // check up to single-second precision since sub-second precision is OS and fs dependent - assert.equal(_toUnixTimestamp(atime), _toUnixTimestamp(stats.atime)); - assert.equal(_toUnixTimestamp(mtime), _toUnixTimestamp(stats.mtime)); - } +/** + * Gets unix timestamps from stats + * + * @internal + */ +export function unixTimestamps(stats: StatsLike): Record<'atime' | 'mtime', number> { + return { + atime: Math.floor(stats.atimeMs), + mtime: Math.floor(stats.mtimeMs), + }; +} +suite('times', () => { async function runTest(atime: Date | number, mtime: Date | number): Promise { + const times = { + atime: typeof atime == 'number' ? Math.floor(atime) : atime.getTime(), + mtime: typeof mtime == 'number' ? Math.floor(mtime) : mtime.getTime(), + }; + await fs.promises.utimes(path, atime, mtime); - expect_assert(path, atime, mtime); + + assert.deepStrictEqual(unixTimestamps(await fs.promises.stat(path)), times); await fs.promises.utimes('foobarbaz', atime, mtime).catch((error: ErrnoError) => { assert(error instanceof ErrnoError); @@ -27,10 +38,10 @@ suite('times', () => { await using handle = await fs.promises.open(path, 'r'); await handle.utimes(atime, mtime); - expect_assert(handle.fd, atime, mtime); + assert.deepStrictEqual(unixTimestamps(await handle.stat()), times); fs.utimesSync(path, atime, mtime); - expect_assert(path, atime, mtime); + assert.deepStrictEqual(unixTimestamps(fs.statSync(path)), times); try { fs.utimesSync('foobarbaz', atime, mtime);