-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathweek.go
109 lines (103 loc) · 3.09 KB
/
week.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
package dateparse
import (
"strings"
"time"
)
var (
sunday = `воскресенье|sunday`
monday = `понедельник|monday`
tuesday = `вторник|tuesday`
wednesday = `среду|среда|wednesday`
thursday = `четверг|thursday`
friday = `пятницу|пятница|friday`
saturday = `субботу|суббота|saturday`
weeks = strings.Join([]string{
sunday,
monday,
tuesday,
wednesday,
thursday,
friday,
saturday,
}, "|")
)
var (
shortSunday = `вс|воскр|sun`
shortMonday = `пн|пнд|понед|mon`
shortTuesday = `вт|thu`
shortWednesday = `ср|wed`
shortThursday = `чт|thu`
shortFriday = `пт|fri`
shortSaturday = `сб|sat`
shortWeeks = strings.Join([]string{
shortSunday,
shortMonday,
shortTuesday,
shortWednesday,
shortThursday,
shortFriday,
shortSaturday,
}, "|")
)
func parseWeekDay(s string, opts Opts) time.Time {
date := getDate(opts.Now.Year(), opts.Now.Month(), opts.Now.Day(), opts.TodayEndHour, 0, 0, opts)
if weakDay := parseWeekDays(s); weakDay < 7 {
v := weakDay - int(date.Weekday())
if v < 0 {
date = date.Add(time.Duration(v)*24*time.Hour + 7*24*time.Hour)
} else {
date = date.Add(time.Duration(v) * 24 * time.Hour)
}
}
return date
}
func parseWeekDays(s string) int {
switch {
case strings.Contains(strings.Join([]string{sunday, shortSunday}, "|"), s):
return 0
case strings.Contains(strings.Join([]string{monday, shortMonday}, "|"), s):
return 1
case strings.Contains(strings.Join([]string{tuesday, shortTuesday}, "|"), s):
return 2
case strings.Contains(strings.Join([]string{wednesday, shortWednesday}, "|"), s):
return 3
case strings.Contains(strings.Join([]string{thursday, shortThursday}, "|"), s):
return 4
case strings.Contains(strings.Join([]string{friday, shortFriday}, "|"), s):
return 5
case strings.Contains(strings.Join([]string{saturday, shortSaturday}, "|"), s):
return 6
}
return 7
}
func calculateWeekDuration(m []string, opts Opts, weekPosition int) (time.Time, string) {
timePosition := weekPosition + 1
if weekPosition < 0 {
weekPosition = len(m) - 1
timePosition = weekPosition - 2
}
date := parseWeekDay(m[weekPosition], opts)
switch {
case strings.Contains(durPrefix, m[weekPosition-1]):
date = date.Add(24 * 7 * time.Hour)
}
if len(m) > 3 {
switch {
case strings.Contains(morning, m[timePosition]) && m[timePosition] != "":
if date.Weekday() == opts.Now.Weekday() && opts.Now.Hour() > 10 {
date = date.Add(24 * 7 * time.Hour)
}
return getDate(date.Year(), date.Month(), date.Day(), 10, 0, 0, opts), m[0]
case strings.Contains(evening, m[timePosition]) && m[timePosition] != "":
return date, m[0]
case strings.Contains(noon, m[timePosition]) && m[timePosition] != "":
return getDate(date.Year(), date.Month(), date.Day(), 12, 0, 0, opts), m[0]
case strings.Contains(midnight, m[timePosition]) && m[timePosition] != "":
return getDate(date.Year(), date.Month(), date.Day(), 0, 0, 0, opts), m[0]
}
}
if date.Before(opts.Now) {
date = date.Add(7 * 24 * time.Hour)
}
return date, m[0]
}