File: ftime_bsd.go

package info (click to toggle)
golang-github-kisom-goutils 0.0~git20161101.0.858c9cb-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 396 kB
  • sloc: makefile: 6
file content (37 lines) | stat: -rw-r--r-- 723 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// +build freebsd darwin netbsd

package lib

import (
	"time"

	"golang.org/x/sys/unix"
)

// FileTime contains the changed, modified, and accessed timestamps
// for a file.
type FileTime struct {
	Changed  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.Changed = timeSpecToTime(st.Ctimespec)
	ft.Modified = timeSpecToTime(st.Mtimespec)
	ft.Accessed = timeSpecToTime(st.Atimespec)
	return ft, nil
}