Skip to content

Commit 39bb75c

Browse files
ryehawkrives
authored andcommitted
parseHours: Handle dayOfYear wraparound
I really don't know if this is anywhere near correct, but it seems to resolve the Jest failures we're getting. Previously, `m`'s dayOfYear was applied to both `open` and `close`, but if `m` was in the next year, then its `dayOfYear` would be low, and `open` and `close` would be freely wrapped around to the beginning of the year. Now, `parseHours` checks if `open` or `close` are six months prior to `m`, and if so, adds `1` year. `m` is used only to provide context to "10am" and such kinds of strings, so I think this is a safe bet. Signed-off-by: Kristofer Rye <[email protected]> Tested-by: Kristofer Rye <[email protected]>
1 parent 9b28f47 commit 39bb75c

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

source/views/building-hours/lib/parse-hours.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import {TIME_FORMAT, CENTRAL_TZ} from './constants'
66

77
type HourPairType = {open: moment, close: moment}
88

9+
// TODO: Do "dayOfYear" handling better so that we don't need to handle wrapping at
10+
// the 6 month mark. (See #3375 for why this function changed.)
911
export function parseHours(
1012
{from: fromTime, to: toTime}: SingleBuildingScheduleType,
1113
m: moment,
@@ -20,9 +22,19 @@ export function parseHours(
2022
let open = moment.tz(fromTime, TIME_FORMAT, true, CENTRAL_TZ)
2123
open.dayOfYear(dayOfYear)
2224

25+
let sixMonthsAgo = moment(m).subtract(6, 'months')
26+
27+
if (open.isBefore(sixMonthsAgo)) {
28+
open.add(1, 'year')
29+
}
30+
2331
let close = moment.tz(toTime, TIME_FORMAT, true, CENTRAL_TZ)
2432
close.dayOfYear(dayOfYear)
2533

34+
if (close.isBefore(sixMonthsAgo)) {
35+
close.add(1, 'year')
36+
}
37+
2638
if (close.isBefore(open)) {
2739
close.add(1, 'day')
2840
}

0 commit comments

Comments
 (0)