-
Notifications
You must be signed in to change notification settings - Fork 921
/
context.go
70 lines (50 loc) · 1.02 KB
/
context.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
package rules
import "time"
type Context struct {
Text string
// accumulator of relative values
Duration time.Duration
// Aboslute values
Year, Month, Weekday, Day, Hour, Minute, Second *int
Location *time.Location
}
func (c *Context) Time(t time.Time) (time.Time, error) {
if t.IsZero() {
t = time.Now()
}
if c.Duration != 0 {
t = t.Add(c.Duration)
}
year, month, day := t.Date()
if c.Year != nil {
year = *c.Year
}
if c.Month != nil {
month = time.Month(*c.Month)
}
if c.Day != nil {
day = *c.Day
}
hour, min, sec := t.Clock()
if c.Hour != nil {
hour = *c.Hour
}
if c.Minute != nil {
min = *c.Minute
}
if c.Second != nil {
sec = *c.Second
}
loc := t.Location()
if c.Location != nil {
loc = c.Location
}
t = time.Date(year, month, day, hour,
min, sec, 0, loc)
if c.Weekday != nil {
diff := int(time.Weekday(*c.Weekday) - t.Weekday())
t = time.Date(t.Year(), t.Month(), t.Day()+diff, t.Hour(),
t.Minute(), t.Second(), t.Nanosecond(), t.Location())
}
return t, nil
}