-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathroute.go
64 lines (54 loc) · 1.42 KB
/
route.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
package konturtransferbot
import (
"fmt"
"strings"
"time"
)
// Route is a sorted sequence of departure times
type Route []Departure
// Departure is a single departure time
type Departure struct {
time.Time
Comment string
}
// UnmarshalJSON is a custom unmarshaler function for time, which works for both JSON and YAML
func (d *Departure) UnmarshalJSON(departure []byte) error {
cleanString := strings.Trim(string(departure), "\"")
parts := strings.SplitN(cleanString, " ", 2)
var err error
d.Time, err = time.Parse("15:04", parts[0])
if err != nil {
return err
}
if len(parts) > 1 {
d.Comment = parts[1]
}
return nil
}
func (r Route) String() string {
var result string
for _, departure := range r {
result += departure.Format("15:04")
if departure.Comment != "" {
result += " " + departure.Comment
}
result += "\n"
}
return result
}
// StringWithDivider prints current time inside a route schedule
func (r Route) StringWithDivider(now time.Time) string {
nowReset := time.Date(0, time.January, 1, now.Hour(), now.Minute(), 0, 0, &time.Location{})
var result string
for i := range r {
if i > 0 && (r[i].After(nowReset) || r[i].Equal(nowReset)) && r[i-1].Before(nowReset) {
result += fmt.Sprintf("——— сейчас %s ———\n", nowReset.Format("15:04"))
}
result += r[i].Format("15:04")
if r[i].Comment != "" {
result += " " + r[i].Comment
}
result += "\n"
}
return result
}