提问人:Mirou 提问时间:5/12/2023 最后编辑:Mirou 更新时间:5/13/2023 访问量:58
解析时间表以将时间范围组合在一起以确定开盘和收盘时间
Parse timetable to group timeframes together to determine open and close times
问:
我有这个对象,我从一个 API 中得到它有一个特定日期的餐厅时间表:
restaurantTimetable := []string {
"2023-05-27T11:00:00",
"2023-05-27T11:30:00",
"2023-05-27T12:00:00",
"2023-05-27T12:30:00",
"2023-05-27T13:00:00",
"2023-05-27T13:30:00",
"2023-05-27T14:00:00",
"2023-05-27T18:00:00",
"2023-05-27T18:30:00",
"2023-05-27T19:00:00",
"2023-05-27T19:30:00",
"2023-05-27T20:00:00",
"2023-05-27T20:30:00",
"2023-05-27T21:00:00",
}
我想将其解析为与此类似的内容,以便我们有当天的开放和关闭时间:
[
{
"open": {
"time": "1100",
"hours": 11,
"minutes": 0,
"nextDate": 0
},
"close": {
"time": "1400",
"hours": 14,
"minutes": 0,
"nextDate": 0
}
},
{
"open": {
"time": "1800",
"hours": 18,
"minutes": 0,
"nextDate": 0
},
"close": {
"time": "2100",
"hours": 21,
"minutes": 0,
"nextDate": 0
}
}
]
这就是我到目前为止所做的,但我没有得到正确的结果,我不确定到底出了什么问题:
func ParseTimetable(timetable []string) ([]map[string]map[string]interface{}, error) {
const layout = "2006-01-02T15:04:05"
slots := make([]map[string]map[string]interface{}, 0)
for i := 0; i+1 < len(timetable); i += 2 {
startSlot := timetable[i]
endSlot := timetable[i+1]
// parse start and end times as time.Time objects
startTime, err := time.Parse(layout, startSlot)
if err != nil {
return nil, err
}
endTime, err := time.Parse(layout, endSlot)
if err != nil {
return nil, err
}
startHour, startMinute, _ := startTime.Clock()
endHour, endMinute, _ := endTime.Clock()
slot := map[string]map[string]interface{}{
"open": {
"time": fmt.Sprintf("%02d%02d", startHour, startMinute),
"hours": startHour,
"minutes": startMinute,
"nextDate": 0,
},
"close": {
"time": fmt.Sprintf("%02d%02d", endHour, endMinute),
"hours": endHour,
"minutes": endMinute,
"nextDate": 0,
},
}
slots = append(slots, slot)
}
return slots, nil
}
我不确定我做错了什么,如果有人能在这里指导,我将不胜感激!
答:
-1赞
Mirou
5/13/2023
#1
我设法像这样解决了它:
func parseTimetable(timetable []string, day int) ([]map[string]map[string]interface{}, error) {
const layout = "2006-01-02T15:04:05"
slots := make([]map[string]map[string]interface{}, 0)
startSlot := timetable[0]
endSlot := ""
for i := 1; i < len(timetable); i += 1 {
timeSlot, err := time.Parse(layout, timetable[i])
if err != nil {
return nil, err
}
previousTimeSlot, err := time.Parse(layout, timetable[i-1])
if err != nil {
return nil, err
}
if timeSlot == previousTimeSlot.Add(30*time.Minute) && i+1 < len(timetable) {
continue
}
if i == len(timetable)-1 {
endSlot = timetable[i]
} else {
endSlot = timetable[i-1]
}
startTime, err := time.Parse(layout, startSlot)
if err != nil {
return nil, err
}
endTime, err := time.Parse(layout, endSlot)
if err != nil {
return nil, err
}
startHour, startMinute, _ := startTime.Clock()
endHour, endMinute, _ := endTime.Clock()
slot := map[string]map[string]interface{}{
"open": {
"day": day,
"time": fmt.Sprintf("%02d%02d", startHour, startMinute),
"hours": startHour,
"minutes": startMinute,
"nextDate": 0,
},
"close": {
"day": day,
"time": fmt.Sprintf("%02d%02d", endHour, endMinute),
"hours": endHour,
"minutes": endMinute,
"nextDate": 0,
},
}
slots = append(slots, slot)
startSlot = timetable[i]
endSlot = ""
}
return slots, nil
}
我不确定这是否是最好的解决方案,但目前它有效,如果有人对如何优化它有任何建议,请告诉我。
评论