From c4cd5442e49164f0e69421447325fade0128ca07 Mon Sep 17 00:00:00 2001 From: Kyle Date: Mon, 18 Jan 2016 23:04:42 -0800 Subject: [PATCH] Add FileTime struct. This allows retrieving a file's creation, modified, and access time using a Unix stat(1) call. --- lib/lib.go | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/lib/lib.go b/lib/lib.go index 1096e0b..cd876c9 100644 --- a/lib/lib.go +++ b/lib/lib.go @@ -6,6 +6,8 @@ import ( "os" "path/filepath" "time" + + "golang.org/x/sys/unix" ) var progname = filepath.Base(os.Args[0]) @@ -111,3 +113,31 @@ func Duration(d time.Duration) string { s += fmt.Sprintf("%dh%s", hours, d) return s } + +// FileTime contains the created, modified, and accessed timestamps +// for a file. +type FileTime struct { + Created time.Time + Modified time.Time + Accessed time.Time +} + +func timeSpecToTime(ts unix.TimeSpec) time.Time { + return time.Unix(ts.Sec, ts.Nsec) +} + +// LoadFileTime returns a FileTime associated with the file. +func LoadFileTime(path string) (FileTime, error) { + var ft = FileTime{} + var st = unix.Stat_t{} + + err := unix.Stat(path, &st) + if err != nil { + return ft, err + } + + ft.Created = timeSpecToTime(st.Ctim) + ft.Modified = timeSpecToTime(st.Mtim) + ft.Accessed = timeSpecToTime(st.Atim) + return ft, nil +}