Skip to content

Commit 6fe04a0

Browse files
authored
Merge branch 'develop' into auth-fix
2 parents b7afb0d + 6266412 commit 6fe04a0

File tree

2 files changed

+85
-3
lines changed

2 files changed

+85
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package timeRangeLib
2+
3+
import (
4+
"testing"
5+
"time"
6+
)
7+
8+
func TestGetWindowForFixedTime(t *testing.T) {
9+
// Define the time range in UTC , as db will be having this time in UTC/GMT
10+
tr := TimeRange{
11+
TimeFrom: time.Date(2024, 10, 14, 9, 0, 0, 0, time.UTC), // 9:00 AM UTC
12+
TimeTo: time.Date(2024, 10, 14, 17, 0, 0, 0, time.UTC), // 5:00 PM UTC
13+
}
14+
15+
// Define the time zones with GMT offsets
16+
locBangui, _ := time.LoadLocation("Africa/Bangui") // GMT+01:00
17+
locParamaribo, _ := time.LoadLocation("America/Paramaribo") // GMT-03:00
18+
zeroTime := time.Time{}
19+
// Define test cases with different time zones
20+
testCases := []struct {
21+
name string
22+
targetTime time.Time
23+
expectedStart time.Time
24+
expectedEnd time.Time
25+
}{
26+
27+
{
28+
name: "Target time in Africa/Bangui (GMT+01:00), within range",
29+
targetTime: time.Date(2024, 10, 14, 11, 30, 0, 0, locBangui), // 11:30 AM GMT+01 (within range)
30+
expectedStart: time.Date(2024, 10, 14, 10, 0, 0, 0, locBangui), // 10:00 AM GMT+01 (adjusted)
31+
expectedEnd: time.Date(2024, 10, 14, 18, 0, 0, 0, locBangui), // 6:00 PM GMT+01 (adjusted)
32+
},
33+
{
34+
name: "Target time in Africa/Bangui (GMT+01:00), before range",
35+
targetTime: time.Date(2024, 10, 14, 8, 30, 0, 0, locBangui), // 11:30 AM GMT+01 (before range)
36+
expectedStart: time.Date(2024, 10, 14, 10, 0, 0, 0, locBangui), // 10:00 AM GMT+01 (adjusted)
37+
expectedEnd: time.Date(2024, 10, 14, 18, 0, 0, 0, locBangui), // 6:00 PM GMT+01 (adjusted)
38+
},
39+
{
40+
name: "Target time in Africa/Bangui (GMT+01:00), after the range (Outside)",
41+
targetTime: time.Date(2024, 10, 14, 19, 30, 0, 0, locBangui), // 7:30 AM GMT+01 (outside)
42+
expectedStart: time.Time{}, // zeroTime
43+
expectedEnd: time.Time{}, // zeroTime
44+
},
45+
46+
{
47+
name: "Target time in America/Paramaribo (GMT-03:00), within range",
48+
targetTime: time.Date(2024, 10, 14, 07, 00, 0, 0, locParamaribo), // 7:00 AM GMT-03 (within range)
49+
expectedStart: time.Date(2024, 10, 14, 06, 00, 0, 0, locParamaribo), // 6:00 AM GMT-03 (adjusted)
50+
expectedEnd: time.Date(2024, 10, 14, 14, 00, 0, 0, locParamaribo), // 2:00 PM GMT-03 (adjusted)
51+
},
52+
{
53+
name: "Target time in America/Paramaribo (GMT-03:00), before range",
54+
targetTime: time.Date(2024, 10, 14, 04, 00, 0, 0, locParamaribo), // 4:00 AM GMT-03 (within range)
55+
expectedStart: time.Date(2024, 10, 14, 06, 00, 0, 0, locParamaribo), // 6:00 AM GMT-03 (adjusted)
56+
expectedEnd: time.Date(2024, 10, 14, 14, 00, 0, 0, locParamaribo), // 2:00 PM GMT-03 (adjusted)
57+
},
58+
{
59+
name: "Target time in America/Paramaribo (GMT-03:00), after the range (Outside)",
60+
targetTime: time.Date(2024, 10, 14, 14, 30, 0, 0, locParamaribo), // 2:30 PM GMT-03 (outside)
61+
expectedStart: zeroTime, // zeroTime
62+
expectedEnd: zeroTime, // zeroTime
63+
},
64+
}
65+
66+
// Loop through the test cases
67+
for _, tc := range testCases {
68+
t.Run(tc.name, func(t *testing.T) {
69+
windowStart, windowEnd := tr.getWindowForFixedTime(tc.targetTime)
70+
if windowStart != tc.expectedStart || windowEnd != tc.expectedEnd {
71+
t.Errorf("Failed %s: got start=%v, end=%v; want start=%v, end=%v",
72+
tc.name, windowStart, windowEnd, tc.expectedStart, tc.expectedEnd)
73+
}
74+
})
75+
}
76+
}

common-lib/timeRangeLib/parser.go

+9-3
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ func (tr TimeRange) GetTimeRangeWindow(targetTime time.Time) (nextWindowEdge tim
3636
}
3737
return windowStart, false, nil
3838
}
39-
4039
func (tr TimeRange) getWindowForTargetTime(targetTime time.Time) (time.Time, time.Time, error) {
4140

4241
if tr.Frequency == Fixed {
@@ -92,10 +91,17 @@ func (tr TimeRange) currentTimeMinusWindowDuration(targetTime time.Time, duratio
9291
return targetTime.Add(-1 * duration)
9392
}
9493

94+
// getWindowForFixedTime calculates the time window for a fixed frequency.
95+
// Before comparing times, it is essential to ensure they are in the same time zone.
96+
// The input targetTime is already converted to the correct time zone,(means the time zone in which they have saved mentioned explicitly)
97+
// while tr.TimeFrom and tr.TimeTo are in GMT. Therefore, to compare them accurately, (in case of Fixed it happens)
98+
// we convert the time range to match the time zone of targetTime.
9599
func (tr TimeRange) getWindowForFixedTime(targetTime time.Time) (time.Time, time.Time) {
96100
var windowStartOrEnd time.Time
97-
if targetTime.After(tr.TimeTo) {
101+
timeFromInLocation := tr.TimeFrom.In(targetTime.Location())
102+
timeToInLocation := tr.TimeTo.In(targetTime.Location())
103+
if targetTime.After(timeToInLocation) {
98104
return windowStartOrEnd, windowStartOrEnd
99105
}
100-
return tr.TimeFrom, tr.TimeTo
106+
return timeFromInLocation, timeToInLocation
101107
}

0 commit comments

Comments
 (0)