有谁知道如何检查文件访问日期和时间?该函数返回修改的日期和时间,我需要一些东西,将访问的日期时间与当前的日期和时间进行比较.
tux*_*21b 12
您可以使用os.Stat来获取FileInfo结构,该结构还包含上次访问时间(以及上次修改时间和最后一次状态更改时间).
info, err := os.Stat("example.txt")
if err != nil {
// TODO: handle errors (e.g. file not found)
}
// info.Atime_ns now contains the last access time
// (in nanoseconds since the unix epoch)
Run Code Online (Sandbox Code Playgroud)
之后,您可以使用time.Nanconds来获取当前时间(也就是自unix时代以来的纳秒,1970年1月1日00:00:00 UTC).要以纳秒为单位获取持续时间,只需减去这两个值:
duration := time.Nanoseconds() - info.Atime_ns
Run Code Online (Sandbox Code Playgroud)
或者,在Stat之后你也可以做
statinfo.ModTime()
Run Code Online (Sandbox Code Playgroud)
如果你需要它,你也可以在它上面使用Format(),例如用于网络服务器
请参阅https://gist.github.com/alexisrobert/982674
通过投射os.FileInfo到*syscall.Stat_t:
package main
import ( "fmt"; "log"; "os"; "syscall"; "time" )
func main() {
for _, arg := range os.Args[1:] {
fileinfo, err := os.Stat(arg)
if err != nil {
log.Fatal(err)
}
atime := fileinfo.Sys().(*syscall.Stat_t).Atim
fmt.Println(time.Unix(atime.Sec, atime.Nsec))
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11167 次 |
| 最近记录: |