Add FileTime struct.

This allows retrieving a file's creation, modified, and access time
using a Unix stat(1) call.
This commit is contained in:
Kyle 2016-01-18 23:04:42 -08:00
parent 7944be7139
commit c4cd5442e4
1 changed files with 30 additions and 0 deletions

View File

@ -6,6 +6,8 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"time" "time"
"golang.org/x/sys/unix"
) )
var progname = filepath.Base(os.Args[0]) var progname = filepath.Base(os.Args[0])
@ -111,3 +113,31 @@ func Duration(d time.Duration) string {
s += fmt.Sprintf("%dh%s", hours, d) s += fmt.Sprintf("%dh%s", hours, d)
return s 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
}