-
Notifications
You must be signed in to change notification settings - Fork 0
/
days.go
100 lines (91 loc) · 1.84 KB
/
days.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
90
91
92
93
94
95
96
97
98
99
100
package main
import (
"embed"
"encoding/json"
"errors"
"io/ioutil"
"log"
"strconv"
"sync"
"time"
)
//go:embed config
var Config embed.FS
//var year *Years
var years map[int]*Years
var loadLocker sync.RWMutex
func init() {
loadLocker = sync.RWMutex{}
years = make(map[int]*Years)
//year = new(Years)
//for i := 0; i < 12; i++ {
// year[i] = make(map[int]Day)
//}
}
func GetByNow() (*Day, error) {
now := time.Now()
year, month, day := now.Date()
loadLocker.RLock()
y, ok := years[year]
loadLocker.RUnlock()
if !ok {
//从文件加载
if t, err := loadConfig(year); err != nil {
log.Printf("加载配置文件异常 err:%s", err)
return nil, errors.New("日期配置加载异常")
} else {
y = t
}
}
m := y[month]
d, ok := m[day]
if ok {
return &d, nil
}
w := time.Date(year, month, day, 8, 0, 0, 0, time.Local).Weekday()
today := &Day{
Year: year,
Month: int(month),
Day: day,
Weekday: int(w),
Date: now.Format("2006-01-02"),
Work: true,
}
if w == time.Sunday || w == time.Saturday {
today.Work = false
return today, nil
}
return today, nil
}
func loadConfig(year int) (*Years, error) {
defer loadLocker.Unlock()
loadLocker.Lock()
if y, ok := years[year]; ok {
return y, nil
}
path := "config/" + strconv.Itoa(year) + ".json"
if f, err := Config.Open(path); err != nil {
return nil, err
} else {
bytes, err := ioutil.ReadAll(f)
if err != nil {
return nil, err
}
ret := make(map[int][]Day)
if err := json.Unmarshal(bytes, &ret); err != nil {
return nil, err
}
ty := new(Years)
for month, days := range ret {
m := make(map[int]Day)
for _, d := range days {
d.Year = year
d.Month = month
d.Weekday = int(time.Date(d.Year, time.Month(d.Month), d.Day, 0, 0, 0, 0, time.Local).Weekday())
m[d.Day] = d
}
ty[month] = m
}
return ty, nil
}
}