2020-01-02 15:13:47 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2020-03-08 08:09:22 -04:00
|
|
|
import { sendSync, sendAsync } from "./ops/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
|
2020-03-06 11:29:23 -05:00
|
|
|
* object referenced by `path`. Given times are either in seconds (UNIX epoch
|
|
|
|
* time) or as `Date` objects.
|
2019-05-01 05:08:12 -04:00
|
|
|
*
|
|
|
|
* 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(
|
2020-03-06 11:29:23 -05:00
|
|
|
path: string,
|
2019-05-01 05:08:12 -04:00
|
|
|
atime: number | Date,
|
|
|
|
mtime: number | Date
|
|
|
|
): void {
|
2020-02-25 09:14:27 -05:00
|
|
|
sendSync("op_utime", {
|
2020-03-06 11:29:23 -05:00
|
|
|
path,
|
2019-08-23 01:30:14 -04:00
|
|
|
// 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
|
2020-03-06 11:29:23 -05:00
|
|
|
* referenced by `path`. Given times are either in seconds (UNIX epoch time)
|
|
|
|
* or as `Date` objects.
|
2019-05-01 05:08:12 -04:00
|
|
|
*
|
|
|
|
* 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(
|
2020-03-06 11:29:23 -05:00
|
|
|
path: string,
|
2019-05-01 05:08:12 -04:00
|
|
|
atime: number | Date,
|
|
|
|
mtime: number | Date
|
|
|
|
): Promise<void> {
|
2020-02-25 09:14:27 -05:00
|
|
|
await sendAsync("op_utime", {
|
2020-03-06 11:29:23 -05:00
|
|
|
path,
|
2019-08-23 01:30:14 -04:00
|
|
|
// TODO(ry) split atime, mtime into [seconds, nanoseconds] tuple
|
|
|
|
atime: toSecondsFromEpoch(atime),
|
|
|
|
mtime: toSecondsFromEpoch(mtime)
|
|
|
|
});
|
2019-05-01 05:08:12 -04:00
|
|
|
}
|