summaryrefslogtreecommitdiff
path: root/js/utime.ts
blob: 02c423d2495683a311ed85bb524ed8f048123bed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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));
}