diff options
Diffstat (limited to 'js/utime.ts')
-rw-r--r-- | js/utime.ts | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/js/utime.ts b/js/utime.ts new file mode 100644 index 000000000..02c423d24 --- /dev/null +++ b/js/utime.ts @@ -0,0 +1,52 @@ +// 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)); +} |