The problem is: you have two data sources a.Hours and b.Hours, each representing operating periods per day as slices of OperatingTime. Each slice has a sequence of partial objects:
[
{ "open": 28800 },
{ "close": 72000 }
]
So data is encoded as pairs of "open" and "close" objects sequentially in the array.
You want to find the intersection of operating hours per day. For example, if a is open 5:00–9:00 and b open 5:00–8:00 then the intersection is 5:00–8:00.
Step 1: Understand the data structure
The current struct OperatingTime is:
type OperatingTime struct {
Open int `json:"open"`
Close int `json:"close"`
}
But the JSON you receive is split up into array elements that either have "open" or "close", i.e., not both in the same object.
E.g.
"monday": [
{ "open": 28800 },
{ "close": 72000 },
{ "open": 75600 },
{ "close": 82800 }
]
This means the unmarshaling of the full array to []OperatingTime won't work correctly.
Step 2: Parse raw input into time intervals
Because each day's data is a slice of objects each containing only an open or a close key, you should parse them into intervals, e.g.:
type Interval struct {
Open int
Close int
}
Parse each day's slice of map[string]int (or a custom data structure) into an array of Interval.
Step 3: Merge intervals from both APIs into Interval slices
For each day, you will have:
aIntervals []Interval
bIntervals []Interval
You want to compute the intersection intervals between these two interval slices.
Step 4: Intersect two slices of intervals
Intersections of time intervals is a classic problem.
Given two sorted slices of intervals (assume ascending and non-overlapping within each slice), intersection can be done as:
- Using two indexes
i and j, iterate over aIntervals and bIntervals:
- For
aIntervals[i] = [aStart, aEnd] and bIntervals[j] = [bStart, bEnd]
- Calculate the overlap interval as:
start = max(aStart, bStart)
end = min(aEnd, bEnd)
- If
start < end, add [start, end] to result.
- Then advance the pointer with the smallest
end:
- If
aEnd < bEnd => i++
- Else
j++
Step 5: Putting it all together
Example approach:
- Create a helper function to parse each day's raw slice into
[]Interval.
- Create a function
intervalsIntersection(a, b []Interval) []Interval.
- For each day of the week, parse and intersect.
- Output the result as you like.
Example code sketch
package main
import (
"encoding/json"
"fmt"
"log"
"math"
)
type rawTime struct {
Open *int `json:"open,omitempty"`
Close *int `json:"close,omitempty"`
}
type Interval struct {
Open int
Close int
}
// parseRawTimes converts the day's array of open/close objects to Intervals.
// Assumes alternating objects (open then close).
func parseRawTimes(raw []rawTime) ([]Interval, error) {
var intervals []Interval
for i := 0; i < len(raw); {
if raw[i].Open == nil {
return nil, fmt.Errorf("expected open at position %d", i)
}
open := *raw[i].Open
i++
if i >= len(raw) || raw[i].Close == nil {
return nil, fmt.Errorf("expected close after open at position %d", i-1)
}
close := *raw[i].Close
if close <= open {
return nil, fmt.Errorf("close time (%d) must be after open (%d)", close, open)
}
intervals = append(intervals, Interval{Open: open, Close: close})
i++
}
return intervals, nil
}
// intersectIntervals returns intersection of two sorted, non-overlapping Intervals slices
func intersectIntervals(a, b []Interval) []Interval {
var res []Interval
i, j := 0, 0
for i < len(a) && j < len(b) {
start := max(a[i].Open, b[j].Open)
end := min(a[i].Close, b[j].Close)
if start < end {
res = append(res, Interval{Open: start, Close: end})
}
if a[i].Close < b[j].Close {
i++
} else {
j++
}
}
return res
}
func max(a, b int) int {
if a > b {
return a
}
return b
}
func min(a, b int) int {
if a < b {
return a
}
return b
}
func main() {
// here's an example JSON you might get from the APIs for Monday
jsonA := `
[
{"open":28800},
{"close":72000}
]
`
jsonB := `
[
{"open":28800},
{"close":57600}
]
`
var rawA []rawTime
var rawB []rawTime
if err := json.Unmarshal([]byte(jsonA), &rawA); err != nil {
log.Fatal(err)
}
if err := json.Unmarshal([]byte(jsonB), &rawB); err != nil {
log.Fatal(err)
}
aIntervals, err := parseRawTimes(rawA)
if err != nil {
log.Fatal(err)
}
bIntervals, err := parseRawTimes(rawB)
if err != nil {
log.Fatal(err)
}
intersection := intersectIntervals(aIntervals, bIntervals)
fmt.Printf("Intersection intervals: %+v\n", intersection)
}
Output:
Intersection intervals: [{Open:28800 Close:57600}]
Summary
- Parse raw input to pairs of empty struct
{Open} then {Close} into Interval structs.
- For each day, build list of intervals for
a and b.
- Intersect intervals with a two-pointer iteration.
- Return result.
If your JSON structure can be adjusted, it might be easier to have intervals represented as objects with both open and close keys together in each array element.
If you want, I can help you with a full implementation integrating both a.Hours and b.Hours.