如何在 Nim 中触摸 file/update 它在磁盘上的修改时间?

How to touch a file/update its modification time on disk in Nim?

我想在 Nim 中更新一些文件的修改时间,而不是实际修改它们,就像 Unix touch。我看到有一个 os.getLastModificationTime function, or more broadly os.getFileInfo,但我没有看到相应的设置函数。

如何在 Nim 中触摸文件? (如果平台很重要,遗憾的是我目前正在 Windows 上工作。)

不幸的是,在当前的 Nim 版本 0.18.0 中这是系统特定的。在 posix 你会做:

import posix
utimes("filename", nil)

在下一个版本中,您可以使用独立于平台的 os.setLastModificationTimehttps://github.com/nim-lang/Nim/pull/7543

借用 def- 的回答,这里是一个完整的示例,展示了如何将作为第一个参数传递的文件的文件修改时间更新为 "now":

import os
import times

let file = commandLineParams()[0]
setLastModificationTime(file, now().toTime())

在 posix 系统上的幕后(使用 Nim 0.19)这委托给 posix.utimes() 并将更改 accessmodify、[= 的 3 个文件时间戳14=]。 在 Windows 上 "behind the scenes" 细节会有所不同,阅读源代码我 认为 它只会改变 "last write time",而不是 "last access time"。