File: ftime_unix.go

package info (click to toggle)
golang-github-kisom-goutils 0.0~git20161101.0.858c9cb-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 384 kB
  • ctags: 331
  • sloc: makefile: 6
file content (38 lines) | stat: -rw-r--r-- 787 bytes parent folder | download | duplicates (3)
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
38
// +build unix linux openbsd

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 {
	// The casts to int64 are needed because on 386, these are int32s.
	return time.Unix(int64(ts.Sec), int64(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.Ctim)
	ft.Modified = timeSpecToTime(st.Mtim)
	ft.Accessed = timeSpecToTime(st.Atim)
	return ft, nil
}