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 +}