All files / fs / unstable_utime.ts

33.33% Branches 1/3
51.61% Lines 16/31
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
 
 
x1
x1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x1
x1
x1
x1
 
x3
x3
 
 
 
 
 
 
 
 
x3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x1
x1
x1
x1
 
x3
x3
 
 
 
 
 
 
 
x3








































I













































I






// Copyright 2018-2025 the Deno authors. MIT license.

import { getNodeFs, isDeno } from "./_utils.ts";
import { mapError } from "./_map_error.ts";

/** Changes the access (`atime`) and modification (`mtime`) times of a file
 * system object referenced by `path`. Given times are either in seconds
 * (UNIX epoch time) or as `Date` objects.
 *
 * Requires `allow-write` permission for the target path
 *
 * @example Usage
 *
 * ```ts
 * import { assert } from "@std/assert"
 * import { utime } from "@std/fs/unstable-utime";
 * import { stat } from "@std/fs/unstable-stat"
 *
 * const newAccessDate = new Date()
 * const newModifiedDate = new Date()
 *
 * const fileBefore = await Deno.stat("README.md")
 * await Deno.utime("README.md", newAccessDate, newModifiedDate)
 * const fileAfter = await Deno.stat("README.md")
 *
 * assert(fileBefore.atime !== fileAfter.atime)
 * assert(fileBefore.mtime !== fileAfter.mtime)
 * ```
 * @tags allow-write
 * @category File System
 * @param path The path to the file to be updated
 * @param atime The new access time
 * @param mtime The new modification time
 */
export async function utime(
  path: string | URL,
  atime: number | Date,
  mtime: number | Date,
): Promise<void> {
  if (isDeno) {
    await Deno.utime(path, atime, mtime);
  } else {
    try {
      await getNodeFs().promises.utimes(path, atime, mtime);
      return;
    } catch (error) {
      throw mapError(error);
    }
  }
}

/** Synchronously changes the access (`atime`) and modification (`mtime`)
 * times of the file stream resource. Given times are either in seconds
 * (UNIX epoch time) or as `Date` objects.
 *
 * Requires `allow-write` permission for the target path
 *
 * @example Usage
 *
 * ```ts
 * import { assert } from "@std/assert"
 * import { utimeSync } from "@std/fs/unstable-utime";
 * import { stat } from "@std/fs/unstable-stat"
 *
 * const newAccessDate = new Date()
 * const newModifiedDate = new Date()
 *
 * const fileBefore = await Deno.stat("README.md")
 * Deno.utimeSync("README.md", newAccessDate, newModifiedDate)
 * const fileAfter = await Deno.stat("README.md")
 *
 * assert(fileBefore.atime !== fileAfter.atime)
 * assert(fileBefore.mtime !== fileAfter.mtime)
 * ```
 * @tags allow-write
 * @category File System
 * @param path The path to the file to be updated
 * @param atime The new access time
 * @param mtime The new modification time
 */
export function utimeSync(
  path: string | URL,
  atime: number | Date,
  mtime: number | Date,
): void {
  if (isDeno) {
    return Deno.utimeSync(path, atime, mtime);
  } else {
    try {
      getNodeFs().utimesSync(path, atime, mtime);
    } catch (error) {
      throw mapError(error);
    }
  }
}