File: times.go

package info (click to toggle)
golang-github-djherbis-times 1.0.1%2Bgit20170215.d25002f-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 124 kB
  • sloc: makefile: 2
file content (79 lines) | stat: -rw-r--r-- 1,634 bytes parent folder | download | duplicates (2)
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// Package times provides a platform-independent way to get atime, mtime, ctime and btime for files.
package times

import (
	"os"
	"time"
)

// Get returns the Timespec for the given FileInfo
func Get(fi os.FileInfo) Timespec {
	return getTimespec(fi)
}

// Stat returns the Timespec for the given filename.
func Stat(name string) (Timespec, error) {
	if hasPlatformSpecificStat {
		if ts, err := platformSpecficStat(name); err == nil {
			return ts, nil
		}
	}

	fi, err := os.Stat(name)
	if err != nil {
		return nil, err
	}
	return getTimespec(fi), nil
}

// Timespec provides access to file times.
// ChangeTime() panics unless HasChangeTime() is true and
// BirthTime() panics unless HasBirthTime() is true.
type Timespec interface {
	ModTime() time.Time
	AccessTime() time.Time
	ChangeTime() time.Time
	BirthTime() time.Time
	HasChangeTime() bool
	HasBirthTime() bool
}

type atime struct {
	v time.Time
}

func (a atime) AccessTime() time.Time { return a.v }

type ctime struct {
	v time.Time
}

func (ctime) HasChangeTime() bool { return true }

func (c ctime) ChangeTime() time.Time { return c.v }

type mtime struct {
	v time.Time
}

func (m mtime) ModTime() time.Time { return m.v }

type btime struct {
	v time.Time
}

func (btime) HasBirthTime() bool { return true }

func (b btime) BirthTime() time.Time { return b.v }

type noctime struct{}

func (noctime) HasChangeTime() bool { return false }

func (noctime) ChangeTime() time.Time { panic("ctime not available") }

type nobtime struct{}

func (nobtime) HasBirthTime() bool { return false }

func (nobtime) BirthTime() time.Time { panic("birthtime not available") }