Browse Source

新增 windows、linux、darwin 系统的文件最好访问时间获取的方法,关联讨论见 #161

Signed-off-by: allan716 <[email protected]>
allan716 3 years ago
parent
commit
52eb7b6e79

+ 28 - 0
internal/pkg/get_access_time/darwin.go

@@ -0,0 +1,28 @@
+//go:build darwin
+
+package get_access_time
+
+import (
+	"os"
+	"time"
+)
+
+type OneGetAccessTime struct {
+}
+
+func (d OneGetAccessTime) GetOSName() string {
+	return "darwin"
+}
+
+func (d OneGetAccessTime) GetAccessTime(fileName string) (time.Time, error) {
+
+	// return now time and err if file does not exist
+	// TODO: change time.Now()
+	fi, err := os.Stat(fileName)
+	if os.IsNotExist(err) {
+		return time.Now(), err
+	}
+
+	aTime := fi.Sys().(*syscall.Stat_t).Atimespec
+	return time.Unix(int64(aTime.Sec), int64(aTime.Nsec)), nil
+}

+ 5 - 0
internal/pkg/get_access_time/get_access_time.go

@@ -0,0 +1,5 @@
+package get_access_time
+
+type GetAccessTimeEx struct {
+	OneGetAccessTime
+}

+ 21 - 0
internal/pkg/get_access_time/get_access_time_test.go

@@ -0,0 +1,21 @@
+package get_access_time
+
+import (
+	"github.com/allanpk716/ChineseSubFinder/internal/pkg/unit_test_helper"
+	"path/filepath"
+	"testing"
+)
+
+func TestGetAccessTime_GetAccessTime(t *testing.T) {
+
+	testRootDir := unit_test_helper.GetTestDataResourceRootPath([]string{"sub_parser", "org"}, 4, false)
+	fileFPath := filepath.Join(testRootDir, "[xunlei]_0_C3A5CUsers5CAdministrator5CDesktop5CThe Boss Baby Family Business_S0E0.ass")
+
+	g := GetAccessTimeEx{}
+	println(g.GetOSName())
+	accessTime, err := g.GetAccessTime(fileFPath)
+	if err != nil {
+		t.Fatal(err)
+	}
+	println(accessTime.String())
+}

+ 28 - 0
internal/pkg/get_access_time/linux.go

@@ -0,0 +1,28 @@
+//go:build linux
+
+package get_access_time
+
+import (
+	"os"
+	"time"
+)
+
+type OneGetAccessTime struct {
+}
+
+func (d OneGetAccessTime) GetOSName() string {
+	return "linux"
+}
+
+func (d OneGetAccessTime) GetAccessTime(fileName string) (time.Time, error) {
+
+	// return now time and err if file does not exist
+	fi, err := os.Stat(fileName)
+	if os.IsNotExist(err) {
+		return time.Now(), err
+	}
+
+	aTime := fi.Sys().(*syscall.Stat_t).Atim
+	return my_util.Second2Time(aTime.Nanoseconds() / 1e9), nil
+
+}

+ 32 - 0
internal/pkg/get_access_time/windows.go

@@ -0,0 +1,32 @@
+//go:build windows
+
+package get_access_time
+
+import (
+	"github.com/allanpk716/ChineseSubFinder/internal/pkg/my_util"
+	"os"
+	"syscall"
+	"time"
+)
+
+type OneGetAccessTime struct {
+}
+
+func (d OneGetAccessTime) GetOSName() string {
+	return "windows"
+}
+
+func (d OneGetAccessTime) GetAccessTime(fileName string) (time.Time, error) {
+
+	// return now time and err if file does not exist
+	fi, err := os.Stat(fileName)
+	if os.IsNotExist(err) {
+		return time.Now(), err
+	}
+	// get last access time for different platform
+	// https://studygolang.com/topics/6270
+	// https://github.com/golang/go/commit/bd75468a089c8ad38bcb1130c4ed7d2703ef85c1
+	// https://github.com/golang/go/issues/31735
+	aTime := fi.Sys().(*syscall.Win32FileAttributeData).LastAccessTime
+	return my_util.Second2Time(aTime.Nanoseconds() / 1e9), nil
+}

+ 5 - 1
internal/pkg/my_util/util.go

@@ -349,6 +349,10 @@ func Time2Duration(inTime time.Time) time.Duration {
 	return time.Duration(Time2SecondNumber(inTime) * math.Pow10(9))
 }
 
+func Second2Time(sec int64) time.Time {
+	return time.Unix(sec, 0)
+}
+
 // ReplaceSpecString 替换特殊的字符
 func ReplaceSpecString(inString string, rep string) string {
 	return regex_things.RegMatchSpString.ReplaceAllString(inString, rep)
@@ -531,4 +535,4 @@ func GetNowTimeString() (string, int, int, int) {
 func GenerateAccessToken() string {
 	u4 := uuid.New()
 	return u4.String()
-}
+}