Skip to content

Commit c59c01f

Browse files
AlexYaroshenkoglaslos
authored andcommitted
- sort by hours
1 parent 8e64223 commit c59c01f

2 files changed

Lines changed: 11 additions & 5 deletions

File tree

openhours.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,9 @@ func merge4(o ...time.Time) (bool, []time.Time) {
222222

223223
func merge(o []time.Time) []time.Time {
224224
sort.SliceStable(o, func(i, j int) bool {
225+
if o[i].Day() == o[j].Day() {
226+
return o[i].Hour() < o[j].Hour()
227+
}
225228
return o[i].Day() < o[j].Day()
226229
})
227230
for i := 0; i < len(o); i += 2 {

openhours_test.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -229,13 +229,16 @@ func TestNew(t *testing.T) {
229229
{"complex = simple", "su-sa 00:00-12:00,12:00-24:00", l, NewMust("", l)},
230230
{"complex = simple", "su-sa 00:00-12:00;su-sa 12:00-24:00", l, NewMust("", l)},
231231
{"time windows order does not matter anymore", "mo-su 00:00-24:00", l, NewMust("", l)},
232-
{"weird times in same sentence", "mo-fr 00:00-15:00,10:00-24:00", l, NewMust("mo-fr 00:00-24:00", l)},
233-
{"weird times in same sentence one contained", "mo-fr 10:00-15:00,00:00-24:00", l, NewMust("mo-fr 00:00-24:00", l)},
234-
{"weird times in different sentence", "mo-fr 00:00-15:00;mo-fr 10:00-24:00", l, NewMust("mo-fr 00:00-24:00", l)},
235-
{"weird times in different sentence contained", "mo-fr 10:00-15:00;mo-fr 00:00-24:00", l, NewMust("mo-fr 00:00-24:00", l)},
236-
{"total chaos", "tu-fr 10:00-15:00;mo 08:00-09:00;mo-fr 00:00-24:00", l, NewMust("mo-fr 00:00-24:00", l)},
237232
{"one day", "mo 10:00-15:00", l, []time.Time{newDate(1, 10, 0, 0, 0, l), newDate(1, 15, 0, 0, 0, l)}},
238233
{"two days", "mo 10:00-15:00;fr 08:00-14:00", l, []time.Time{newDate(1, 10, 0, 0, 0, l), newDate(1, 15, 0, 0, 0, l), newDate(5, 8, 0, 0, 0, l), newDate(5, 14, 0, 0, 0, l)}},
234+
{"week with break", "Tu-Th 10:30-13:00,14:00-24:00", l, []time.Time{
235+
newDate(2, 10, 30, 0, 0, l), newDate(2, 13, 0, 0, 0, l),
236+
newDate(2, 14, 0, 0, 0, l), newDate(2, 24, 0, 0, 0, l),
237+
newDate(3, 10, 30, 0, 0, l), newDate(3, 13, 0, 0, 0, l),
238+
newDate(3, 14, 0, 0, 0, l), newDate(3, 24, 0, 0, 0, l),
239+
newDate(4, 10, 30, 0, 0, l), newDate(4, 13, 0, 0, 0, l),
240+
newDate(4, 14, 0, 0, 0, l), newDate(4, 24, 0, 0, 0, l),
241+
}},
239242
}
240243
for _, tt := range tests {
241244
t.Run(tt.name, func(t *testing.T) {

0 commit comments

Comments
 (0)