Skip to content

Commit 1fcd0ed

Browse files
committed
handling full week and date alignment
1 parent b2764df commit 1fcd0ed

2 files changed

Lines changed: 109 additions & 94 deletions

File tree

openhours.go

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,18 @@ import (
99
"time"
1010
)
1111

12+
const (
13+
Monday int = iota + 1
14+
Tuesday
15+
Wednesday
16+
Thursday
17+
Friday
18+
Saturday
19+
Sunday
20+
)
21+
1222
var (
13-
weekDays = map[string]int{"su": 0, "mo": 1, "tu": 2, "we": 3, "th": 4, "fr": 5, "sa": 6}
23+
weekDays = map[string]int{"mo": Monday, "tu": Tuesday, "we": Wednesday, "th": Thursday, "fr": Friday, "sa": Saturday, "su": Sunday}
1424

1525
// Errors
1626
ErrInvalidFormat error = errors.New("invalid format")
@@ -20,11 +30,11 @@ var (
2030
type OpenHours []time.Time
2131

2232
func newDate(day, hour, min, sec, nsec int, loc *time.Location) time.Time {
23-
return time.Date(2017, 1, day, hour, min, sec, nsec, loc)
33+
return time.Date(2018, 1, day, hour, min, sec, nsec, loc)
2434
}
2535

2636
func newDateFromTime(t time.Time) time.Time {
27-
return newDate(int(t.Weekday()), t.Hour(), t.Minute(), t.Second(), t.Nanosecond(), t.Location())
37+
return newDate(t.Day(), t.Hour(), t.Minute(), t.Second(), t.Nanosecond(), t.Location())
2838
}
2939

3040
// Match returns true if the time t is in the open hours
@@ -115,15 +125,13 @@ func (o OpenHours) Add(from, to time.Time) OpenHours {
115125
return o
116126
}
117127

118-
var weekdays = map[int]string{0: "Sunday", 1: "Monday", 2: "Tuesday", 3: "Wednesday", 4: "Thursday", 5: "Friday", 6: "Saturday"}
119-
120128
func (o OpenHours) String() []string {
121129
str := []string{}
122130
if len(o) == 0 {
123131
return str
124132
}
125133
for i := 1; i <= len(o)-1; i += 2 {
126-
str = append(str, fmt.Sprintf("%s %s - %s", weekdays[o[i-1].Day()], o[i-1].Format("15:04"), o[i].Format("15:04")))
134+
str = append(str, fmt.Sprintf("%s %s - %s", o[i-1].Weekday(), o[i-1].Format("15:04"), o[i].Format("15:04")))
127135
}
128136
return str
129137
}
@@ -161,7 +169,12 @@ func simplifyDays(str string) []int {
161169
to += 7
162170
}
163171
for i := from; i <= to; i++ {
164-
days[i%7] = struct{}{}
172+
switch i % 7 {
173+
case 0:
174+
days[7] = struct{}{}
175+
default:
176+
days[i%7] = struct{}{}
177+
}
165178
}
166179
continue
167180
}

0 commit comments

Comments
 (0)