Skip to content

Commit

Permalink
Fixed normalizeTime
Browse files Browse the repository at this point in the history
Removed `_toUnixTimestamp`
Streamlined times tests
  • Loading branch information
james-pre committed Nov 11, 2024
1 parent 2270f28 commit a498e74
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 34 deletions.
26 changes: 3 additions & 23 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,22 +182,6 @@ export function encodeDirListing(data: Record<string, bigint>): Uint8Array {

export type Callback<Args extends unknown[] = []> = (e?: ErrnoError, ...args: OptionalTuple<Args>) => 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
Expand Down Expand Up @@ -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.');
}

/**
Expand Down
33 changes: 22 additions & 11 deletions tests/fs/times.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<number>): 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<void> {
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);
Expand All @@ -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);
Expand Down

0 comments on commit a498e74

Please sign in to comment.