-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathschedule.go
76 lines (62 loc) · 2.32 KB
/
schedule.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
package konturtransferbot
import (
"time"
"github.com/rickar/cal/v2"
"github.com/rickar/cal/v2/ru"
)
// Schedule contains all information on transfer departure times
type Schedule struct {
WorkDayRouteToOffice Route
WorkDayRouteFromOffice Route
SaturdayRouteToOffice Route
SaturdayRouteFromOffice Route
}
// customCalendar создаёт календарь с российскими праздниками
func customCalendar() *cal.BusinessCalendar {
c := cal.NewBusinessCalendar()
// Добавляем российские праздники
c.AddHoliday(
ru.NewYear, // Новый Год (1 января)
ru.ChristmasDay, // Рождество (7 января)
ru.DefenderOfFatherlandDay,
ru.InternationalWomenDay,
ru.SpringAndLabourDay,
ru.VictoryDay,
ru.RussiaDay,
ru.NationalUnityDay,
)
return c
}
// isHoliday проверяет, является ли текущая дата выходным или праздничным днём
func isHoliday(cal *cal.BusinessCalendar, date time.Time) bool {
return !cal.IsWorkday(date)
}
// GetToOfficeText returns text representation of full schedule to office
func (s Schedule) GetToOfficeText(now time.Time) (string, string) {
prefix := "*Рейсы в офис*\n\n"
suffix := "\nВ выходные дни трансфера нет"
cal := customCalendar() // инициализация календаря
timeAgnosticRoute := prefix + s.WorkDayRouteToOffice.String() + suffix
if isHoliday(cal, now) {
return timeAgnosticRoute, ""
}
timeSensitiveRoute := prefix + s.WorkDayRouteToOffice.StringWithDivider(now) + suffix
if timeAgnosticRoute == timeSensitiveRoute {
return timeAgnosticRoute, ""
}
return timeSensitiveRoute, timeAgnosticRoute
}
// GetFromOfficeText returns text representation of full schedule from office
func (s Schedule) GetFromOfficeText(now time.Time) (string, string) {
prefix := "*Рейсы из офиса*\n\n"
suffix := "\nВ выходные дни трансфера нет"
timeAgnosticRoute := prefix + s.WorkDayRouteFromOffice.String() + suffix
if isHoliday(cal, now) {
return timeAgnosticRoute, ""
}
timeSensitiveRoute := prefix + s.WorkDayRouteFromOffice.StringWithDivider(now) + suffix
if timeAgnosticRoute == timeSensitiveRoute {
return timeAgnosticRoute, ""
}
return timeSensitiveRoute, timeAgnosticRoute
}