Skip to content

Commit 9e30339

Browse files
committed
feat: adds the Times method
1 parent 1498c54 commit 9e30339

File tree

4 files changed

+99
-0
lines changed

4 files changed

+99
-0
lines changed

path.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"path/filepath"
1616
"runtime"
1717
"strings"
18+
"time"
1819

1920
"github.com/shirou/gopsutil/v4/disk"
2021
)
@@ -574,6 +575,10 @@ type Usage struct {
574575
UsedPercent float64
575576
}
576577

578+
func (p Path) Times() (created, modified, accessed time.Time) {
579+
return getTimes(string(p))
580+
}
581+
577582
func (p Path) Usage() (u Usage, err error) {
578583
var s *disk.UsageStat
579584
if s, err = disk.Usage(string(p)); err != nil {

time_darwin.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//go:build darwin
2+
3+
package ppath
4+
5+
import (
6+
"os"
7+
"syscall"
8+
"time"
9+
)
10+
11+
func getTimes(path string) (created, modified, accessed time.Time) {
12+
info, err := os.Stat(path)
13+
if err != nil {
14+
return
15+
}
16+
modified = info.ModTime()
17+
created = modified
18+
19+
if stat, ok := info.Sys().(*syscall.Stat_t); ok {
20+
accessed = time.Unix(int64(stat.Atimespec.Sec), int64(stat.Atimespec.Nsec))
21+
if stat.Birthtimespec.Sec != 0 {
22+
created = time.Unix(int64(stat.Birthtimespec.Sec), int64(stat.Birthtimespec.Nsec))
23+
} else if stat.Ctimespec.Sec != 0 {
24+
created = time.Unix(int64(stat.Ctimespec.Sec), int64(stat.Ctimespec.Nsec))
25+
}
26+
}
27+
return
28+
}

time_linux.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//go:build linux
2+
3+
package ppath
4+
5+
import (
6+
"os"
7+
"syscall"
8+
"time"
9+
)
10+
11+
func getTimes(path string) (created, modified, accessed time.Time) {
12+
info, err := os.Stat(path)
13+
if err != nil {
14+
return
15+
}
16+
modified = info.ModTime()
17+
created = modified
18+
19+
if stat, ok := info.Sys().(*syscall.Stat_t); ok {
20+
accessed = time.Unix(int64(stat.Atim.Sec), int64(stat.Atim.Nsec))
21+
if stat.Ctim.Sec != 0 {
22+
created = time.Unix(int64(stat.Ctim.Sec), int64(stat.Ctim.Nsec))
23+
}
24+
}
25+
return
26+
}

time_windows.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//go:build windows
2+
3+
package ppath
4+
5+
import (
6+
"time"
7+
8+
"golang.org/x/sys/windows"
9+
)
10+
11+
func getTimes(path string) (created, modified, accessed time.Time) {
12+
handle, err := openHandle(path)
13+
if err != nil {
14+
return
15+
}
16+
defer windows.CloseHandle(handle)
17+
18+
var cTime, aTime, wTime windows.Filetime
19+
err = windows.GetFileTime(handle, &cTime, &aTime, &wTime)
20+
if err != nil {
21+
return
22+
}
23+
return time.Unix(0, cTime.Nanoseconds()), time.Unix(0, wTime.Nanoseconds()), time.Unix(0, aTime.Nanoseconds())
24+
}
25+
26+
func openHandle(path string) (windows.Handle, error) {
27+
pointer, err := windows.UTF16PtrFromString(path)
28+
if err != nil {
29+
return 0, windows.ERROR_ABANDONED_WAIT_0
30+
}
31+
return windows.CreateFile(
32+
pointer,
33+
windows.GENERIC_READ,
34+
windows.FILE_SHARE_READ,
35+
nil,
36+
windows.OPEN_EXISTING,
37+
windows.FILE_ATTRIBUTE_NORMAL,
38+
0,
39+
)
40+
}

0 commit comments

Comments
 (0)