/
change_time.go
89 lines (70 loc) · 2.01 KB
/
change_time.go
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
80
81
82
83
84
85
86
87
88
89
package main
import (
"fmt"
"math/rand"
"os"
"path/filepath"
"syscall"
"time"
)
const DistPath string = `C:\home\peipei\2014最新商家资料\鹏荣达开票资料`
const LongTimeFormat string = "2006-01-02 15:04:05"
const AbbrevTimeFormat string = "2006-1-2 15:4:5"
func getFileTime() syscall.Filetime {
loc, _ := time.LoadLocation("Asia/Shanghai")
st, err := time.ParseInLocation(LongTimeFormat,
"2014-06-23 10:09:29", loc)
if err != nil {
fmt.Println(err)
}
et, err := time.ParseInLocation(LongTimeFormat,
"2014-06-23 10:09:30", loc)
if err != nil {
fmt.Println(err)
}
gapTimestamp := et.Unix() - st.Unix()
rand.Seed(time.Now().UnixNano())
randGap := rand.Int63n(gapTimestamp)
distTime := uniformTime(time.Unix(st.Unix()+randGap, 0))
ftime := syscall.NsecToFiletime(distTime.UnixNano())
return ftime
}
func modifyFileTime(path string, ftime syscall.Filetime) error {
fd, err := syscall.Open(path, os.O_RDWR, 0755)
if err != nil {
fmt.Println(path, err)
}
err = syscall.SetFileTime(fd, &ftime, &ftime, &ftime)
if err != nil {
fmt.Println(path, err)
}
syscall.Close(fd)
return err
}
func fileHandler(path string, info os.FileInfo, err error) error {
if info.IsDir() {
fmt.Println(path, "is a dir")
return nil
}
ftime := getFileTime()
modifyFileTime(path, ftime)
return nil
}
func uniformTime(t time.Time) time.Time {
loc, _ := time.LoadLocation("Asia/Shanghai")
rand.Seed(time.Now().UnixNano())
startYmdStr := fmt.Sprintf("%d-%d-%d 09:30:00", t.Year(), t.Month(), t.Day())
endYmdStr := fmt.Sprintf("%d-%d-%d 18:30:00", t.Year(), t.Month(), t.Day())
daySt, _ := time.ParseInLocation(AbbrevTimeFormat, startYmdStr, loc)
dayEt, _ := time.ParseInLocation(AbbrevTimeFormat, endYmdStr, loc)
dayGap := dayEt.Unix() - daySt.Unix()
randGap := rand.Int63n(dayGap)
//fmt.Println(t, startYmdStr, daySt, dayEt, dayGap, randGap)
if t.Unix() < daySt.Unix() || t.Unix() > dayEt.Unix() {
return time.Unix(daySt.Unix()+randGap, 0)
}
return t
}
func main() {
filepath.Walk(DistPath, fileHandler)
}