2020-01-02 15:13:47 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2019-09-02 17:07:11 -04:00
|
|
|
import { sendSync, sendAsync } from "./dispatch_json.ts";
|
2019-05-01 05:08:12 -04:00
|
|
|
|
2019-08-23 01:30:14 -04:00
|
|
|
function toSecondsFromEpoch(v: number | Date): number {
|
|
|
|
return v instanceof Date ? v.valueOf() / 1000 : v;
|
2019-05-01 05:08:12 -04:00
|
|
|
}
|
|
|
|
|
2020-03-02 10:19:42 -05:00
|
|
|
/** **UNSTABLE**: needs investigation into high precision time.
|
|
|
|
*
|
|
|
|
* Synchronously changes the access and modification times of a file system
|
2019-05-01 05:08:12 -04:00
|
|
|
* object referenced by `filename`. Given times are either in seconds
|
|
|
|
* (Unix epoch time) or as `Date` objects.
|
|
|
|
*
|
|
|
|
* Deno.utimeSync("myfile.txt", 1556495550, new Date());
|
2020-03-02 10:19:42 -05:00
|
|
|
*
|
|
|
|
* Requires `allow-write` permission. */
|
2019-05-01 05:08:12 -04:00
|
|
|
export function utimeSync(
|
|
|
|
filename: string,
|
|
|
|
atime: number | Date,
|
|
|
|
mtime: number | Date
|
|
|
|
): void {
|
2020-02-25 09:14:27 -05:00
|
|
|
sendSync("op_utime", {
|
2019-08-23 01:30:14 -04:00
|
|
|
filename,
|
|
|
|
// TODO(ry) split atime, mtime into [seconds, nanoseconds] tuple
|
|
|
|
atime: toSecondsFromEpoch(atime),
|
|
|
|
mtime: toSecondsFromEpoch(mtime)
|
|
|
|
});
|
2019-05-01 05:08:12 -04:00
|
|
|
}
|
|
|
|
|
2020-03-02 10:19:42 -05:00
|
|
|
/** **UNSTABLE**: needs investigation into high precision time.
|
|
|
|
*
|
|
|
|
* Changes the access and modification times of a file system object
|
2019-05-01 05:08:12 -04:00
|
|
|
* referenced by `filename`. Given times are either in seconds
|
|
|
|
* (Unix epoch time) or as `Date` objects.
|
|
|
|
*
|
|
|
|
* await Deno.utime("myfile.txt", 1556495550, new Date());
|
2020-03-02 10:19:42 -05:00
|
|
|
*
|
|
|
|
* Requires `allow-write` permission. */
|
2019-05-01 05:08:12 -04:00
|
|
|
export async function utime(
|
|
|
|
filename: string,
|
|
|
|
atime: number | Date,
|
|
|
|
mtime: number | Date
|
|
|
|
): Promise<void> {
|
2020-02-25 09:14:27 -05:00
|
|
|
await sendAsync("op_utime", {
|
2019-08-23 01:30:14 -04:00
|
|
|
filename,
|
|
|
|
// TODO(ry) split atime, mtime into [seconds, nanoseconds] tuple
|
|
|
|
atime: toSecondsFromEpoch(atime),
|
|
|
|
mtime: toSecondsFromEpoch(mtime)
|
|
|
|
});
|
2019-05-01 05:08:12 -04:00
|
|
|
}
|