-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse.go
117 lines (103 loc) · 2.36 KB
/
parse.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// Copyright (c) 2018, dmc ([email protected]),
//
// Authors: dmc,
//
// Distribution:.
package gotask
import (
"errors"
"strconv"
"strings"
"time"
)
var (
// ErrParseTime time format failed
ErrParseTime = errors.New("parse time error")
)
var defaultValue = time.Time{}
type dailyParse struct {
}
func newdailyParse() Parser {
return &dailyParse{}
}
// Parse 接收格式 "hh:mm:ss",返回begintime
func (p *dailyParse) Parse(s string) (time.Time, error) {
tm, err := time.Parse("15:04:05", s)
if err != nil {
return defaultValue, ErrParseTime
}
now := time.Now()
t := time.Date(now.Year(), now.Month(), now.Day(), tm.Hour(), tm.Minute(), tm.Second(), 0, now.Location())
return t, nil
}
type monthlyParse struct {
}
func newmonthlyParse() Parser {
return &monthlyParse{}
}
// Parse 接收格式 dd hh:mm:ss dd为每月几号,如果需要每月最后一天 dd=-1
func (p *monthlyParse) Parse(s string) (res time.Time, err error) {
s2 := strings.SplitN(s, " ", 2)
if len(s2) != 2 {
err = ErrParseTime
return
}
var dt time.Time
dt, err = newdailyParse().Parse(s2[1])
if err != nil {
return
}
var day int
if day, err = strconv.Atoi(s2[0]); err != nil {
err = ErrParseTime
return
}
if day < 0 || day > 31 {
err = ErrParseTime
return
}
now := time.Now()
step := 0
t := time.Date(now.Year(), now.Month(), day, dt.Hour(), dt.Minute(), dt.Second(), 0, now.Location())
if t.Day() != day {
step++
t = t.AddDate(0, step, 0)
}
return t, nil
}
type yearlyParse struct {
}
func newyearlyParse() Parser {
return &yearlyParse{}
}
// Parse 接收格式 MM-dd hh:mm:ss dd为每月几号,如果需要每月最后一天 dd=-1
func (p *yearlyParse) Parse(s string) (res time.Time, err error) {
ss := strings.SplitN(s, "-", 2)
if len(ss) != 2 {
err = ErrParseTime
return
}
var mt time.Time
mt, err = newmonthlyParse().Parse(ss[1])
if err != nil {
return
}
month, err := strconv.Atoi(ss[0])
if err != nil {
err = ErrParseTime
return
}
if month < 1 || month > 12 {
err = ErrParseTime
return
}
if month != 2 && mt.Day() != 29 {
return time.Date(mt.Year(), time.Month(month), mt.Day(), mt.Hour(), mt.Minute(), mt.Second(), 0, mt.Location()), nil
}
year := mt.Year()
for !(year%4 == 0 && year%100 != 0) {
year++
}
res = time.Date(year, time.Month(month), mt.Day(), mt.Hour(), mt.Minute(), mt.Second(), 0, mt.Location())
return
}