0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-03-03 09:31:22 -05:00

fix(#4546) Added Math.trunc to toSecondsFromEpoch to conform the result to u64 (#4575)

This commit is contained in:
Parker Gabel 2020-04-03 12:20:40 -07:00 committed by GitHub
parent 2426174485
commit 0db04d6a42
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 52 additions and 1 deletions

View file

@ -2,7 +2,7 @@
import { sendSync, sendAsync } from "../dispatch_json.ts";
function toSecondsFromEpoch(v: number | Date): number {
return v instanceof Date ? v.valueOf() / 1000 : v;
return v instanceof Date ? Math.trunc(v.valueOf() / 1000) : v;
}
export function utimeSync(

View file

@ -58,6 +58,31 @@ unitTest(
}
);
unitTest(
{ perms: { read: true, write: true } },
function utimeSyncFileDateSuccess() {
const testDir = Deno.makeTempDirSync();
const filename = testDir + "/file.txt";
Deno.writeFileSync(filename, new TextEncoder().encode("hello"), {
mode: 0o666,
});
const atime = new Date();
const mtime = new Date();
Deno.utimeSync(filename, atime, mtime);
const fileInfo = Deno.statSync(filename);
// atime and mtime must be scaled by a factor of 1000 to be recorded in seconds
assertFuzzyTimestampEquals(
fileInfo.accessed,
Math.trunc(atime.valueOf() / 1000)
);
assertFuzzyTimestampEquals(
fileInfo.modified,
Math.trunc(mtime.valueOf() / 1000)
);
}
);
unitTest(
{ perms: { read: true, write: true } },
function utimeSyncLargeNumberSuccess(): void {
@ -158,6 +183,32 @@ unitTest(
}
);
unitTest(
{ perms: { read: true, write: true } },
async function utimeFileDateSuccess(): Promise<void> {
const testDir = Deno.makeTempDirSync();
const filename = testDir + "/file.txt";
Deno.writeFileSync(filename, new TextEncoder().encode("hello"), {
mode: 0o666,
});
const atime = new Date();
const mtime = new Date();
await Deno.utime(filename, atime, mtime);
const fileInfo = Deno.statSync(filename);
// The dates must be scaled by a factored of 1000 to make them seconds
assertFuzzyTimestampEquals(
fileInfo.accessed,
Math.trunc(atime.valueOf() / 1000)
);
assertFuzzyTimestampEquals(
fileInfo.modified,
Math.trunc(mtime.valueOf() / 1000)
);
}
);
unitTest(
{ perms: { read: true, write: true } },
async function utimeNotFound(): Promise<void> {