如何在 Go 中获取文件的上次访问日期和时间?

发布于 2024-12-18 03:43:38 字数 64 浏览 4 评论 0 原文

有谁知道如何检查文件访问日期和时间?该函数返回修改后的日期和时间,我需要将访问的日期时间与当前日期和时间进行比较。

Does anyone know how to check for a file access date and time? The function returns the modified date and time and I need something that compares the accessed date time to the current date and time.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

稀香 2024-12-25 03:43:38

您可以使用 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)

之后,您可以使用 time.Nanoseconds 获取当前时间(也以纳秒为单位) unix 纪元,1970 年 1 月 1 日 00:00:00 UTC)。要获得以纳秒为单位的持续时间,只需减去这两个值:

duration := time.Nanoseconds() - info.Atime_ns

You can use os.Stat to get a FileInfo struct which also contains the last access time (as well as the last modified and the last status change time).

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)

After that, you can use time.Nanoseconds to get the current time (also in nanoseconds since the unix epoch, January 1, 1970 00:00:00 UTC). To get the duration in nanoseconds, just subtract those two values:

duration := time.Nanoseconds() - info.Atime_ns
岁月静好 2024-12-25 03:43:38

通过将 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))
    }
}

By casting os.FileInfo to *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))
    }
}
鲜血染红嫁衣 2024-12-25 03:43:38

或者,在 Stat 之后,您也可以执行

statinfo.ModTime()

此外,您可以在其上使用 Format(),如果您需要它,例如对于网络服务器,

请参阅 https://gist.github.com/alexisrobert/982674

Alternatively, after the Stat you can also do

statinfo.ModTime()

Also you can use Format() on it, should you need it eg for a webserver

see https://gist.github.com/alexisrobert/982674

手心的温暖 2024-12-25 03:43:38

对于 Windows

syscall.Win32FileAttributeData

info, _ := os.Stat("test.txt")
fileTime := info.Sys().(*syscall.Win32FileAttributeData).LastAccessTime
aTime := time.Unix(0, fileTime.Nanoseconds())

示例

package main

import (
    "fmt"
    "log"
    "os"
    "syscall"
    "time"
)

func main() {
    info, _ := os.Stat("./test.txt")
    fileTime := info.Sys().(*syscall.Win32FileAttributeData).LastAccessTime
    // _ = info.Sys().(*syscall.Win32FileAttributeData).CreationTime
    // _ = info.Sys().(*syscall.Win32FileAttributeData).LastWriteTime
    fileAccessTime := time.Unix(0, fileTime.Nanoseconds())

    // Compare
    // t2, _ := time.Parse("2006/01/02 15:04:05 -07:00:00", "2023/02/08 13:18:00 +08:00:00")
    now := time.Now()
    log.Println(fileAccessTime)
    log.Println(now.Add(-20 * time.Minute))
    if fileAccessTime.After(now.Add(-20 * time.Minute)) {
        fmt.Println("You accessed this file 20 minutes ago.")
    }
}

Linux

看到这个 答案

For windows

syscall.Win32FileAttributeData

info, _ := os.Stat("test.txt")
fileTime := info.Sys().(*syscall.Win32FileAttributeData).LastAccessTime
aTime := time.Unix(0, fileTime.Nanoseconds())

Example

package main

import (
    "fmt"
    "log"
    "os"
    "syscall"
    "time"
)

func main() {
    info, _ := os.Stat("./test.txt")
    fileTime := info.Sys().(*syscall.Win32FileAttributeData).LastAccessTime
    // _ = info.Sys().(*syscall.Win32FileAttributeData).CreationTime
    // _ = info.Sys().(*syscall.Win32FileAttributeData).LastWriteTime
    fileAccessTime := time.Unix(0, fileTime.Nanoseconds())

    // Compare
    // t2, _ := time.Parse("2006/01/02 15:04:05 -07:00:00", "2023/02/08 13:18:00 +08:00:00")
    now := time.Now()
    log.Println(fileAccessTime)
    log.Println(now.Add(-20 * time.Minute))
    if fileAccessTime.After(now.Add(-20 * time.Minute)) {
        fmt.Println("You accessed this file 20 minutes ago.")
    }
}

Linux

see this answer

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文