mirror of
https://github.com/denoland/deno.git
synced 2024-11-26 16:09:27 -05:00
53 lines
1.8 KiB
TypeScript
53 lines
1.8 KiB
TypeScript
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
||
|
import * as msg from "gen/cli/msg_generated";
|
||
|
import * as flatbuffers from "./flatbuffers";
|
||
|
import * as dispatch from "./dispatch";
|
||
|
import * as util from "./util";
|
||
|
|
||
|
function req(
|
||
|
filename: string,
|
||
|
atime: number | Date,
|
||
|
mtime: number | Date
|
||
|
): [flatbuffers.Builder, msg.Any, flatbuffers.Offset] {
|
||
|
const atimeSec = atime instanceof Date ? Math.floor(+atime / 1000) : atime;
|
||
|
const mtimeSec = mtime instanceof Date ? Math.floor(+mtime / 1000) : mtime;
|
||
|
|
||
|
const builder = flatbuffers.createBuilder();
|
||
|
const filename_ = builder.createString(filename);
|
||
|
const atimeParts = util.splitNumberToParts(atimeSec);
|
||
|
const atimeMS_ = builder.createLong(atimeParts[0], atimeParts[1]);
|
||
|
const mtimeParts = util.splitNumberToParts(mtimeSec);
|
||
|
const mtimeMS_ = builder.createLong(mtimeParts[0], mtimeParts[1]);
|
||
|
|
||
|
const inner = msg.Utime.createUtime(builder, filename_, atimeMS_, mtimeMS_);
|
||
|
return [builder, msg.Any.Utime, inner];
|
||
|
}
|
||
|
|
||
|
/** Synchronously changes the access and modification times of a file system
|
||
|
* object referenced by `filename`. Given times are either in seconds
|
||
|
* (Unix epoch time) or as `Date` objects.
|
||
|
*
|
||
|
* Deno.utimeSync("myfile.txt", 1556495550, new Date());
|
||
|
*/
|
||
|
export function utimeSync(
|
||
|
filename: string,
|
||
|
atime: number | Date,
|
||
|
mtime: number | Date
|
||
|
): void {
|
||
|
dispatch.sendSync(...req(filename, atime, mtime));
|
||
|
}
|
||
|
|
||
|
/** Changes the access and modification times of a file system object
|
||
|
* referenced by `filename`. Given times are either in seconds
|
||
|
* (Unix epoch time) or as `Date` objects.
|
||
|
*
|
||
|
* await Deno.utime("myfile.txt", 1556495550, new Date());
|
||
|
*/
|
||
|
export async function utime(
|
||
|
filename: string,
|
||
|
atime: number | Date,
|
||
|
mtime: number | Date
|
||
|
): Promise<void> {
|
||
|
await dispatch.sendAsync(...req(filename, atime, mtime));
|
||
|
}
|