Skip to content

Commit b870ded

Browse files
committed
fixed am/pm logic
1 parent f9877c0 commit b870ded

1 file changed

Lines changed: 7 additions & 46 deletions

File tree

src/frontend/src/utils/design-review.utils.ts

Lines changed: 7 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -54,43 +54,9 @@ export const HeatmapColors = ['#D9D9D9', '#C1E0C1', '#9BE89B', '#7AE47A', '#45EF
5454

5555
export const NUMBER_OF_TIME_SLOTS = enumToArray(REVIEW_TIMES).length * enumToArray(DAY_NAMES).length;
5656

57-
const monthToIndex = (month: MONTH_NAMES) => {
58-
const monthToIndexMap: Map<MONTH_NAMES, number> = new Map([
59-
[MONTH_NAMES.January, 0],
60-
[MONTH_NAMES.February, 1],
61-
[MONTH_NAMES.March, 2],
62-
[MONTH_NAMES.April, 3],
63-
[MONTH_NAMES.May, 4],
64-
[MONTH_NAMES.June, 5],
65-
[MONTH_NAMES.July, 6],
66-
[MONTH_NAMES.August, 7],
67-
[MONTH_NAMES.September, 8],
68-
[MONTH_NAMES.October, 9],
69-
[MONTH_NAMES.November, 10],
70-
[MONTH_NAMES.December, 11]
71-
]);
72-
return monthToIndexMap.get(month);
73-
};
74-
75-
const dayToIndex = (weekday: DAY_NAMES) => {
76-
const dayToIndexMap: Map<DAY_NAMES, number> = new Map([
77-
[DAY_NAMES.Sunday, 0],
78-
[DAY_NAMES.Sunday, 1],
79-
[DAY_NAMES.Sunday, 2],
80-
[DAY_NAMES.Sunday, 3],
81-
[DAY_NAMES.Sunday, 4],
82-
[DAY_NAMES.Sunday, 5],
83-
[DAY_NAMES.Sunday, 6]
84-
]);
85-
return dayToIndexMap.get(weekday);
86-
};
87-
8857
const nthWeekday = (year: number, month: MONTH_NAMES, weekday: DAY_NAMES, nth: number) => {
89-
const monthIndex = monthToIndex(month);
90-
const dayIndex = dayToIndex(weekday);
91-
92-
const day = new Date(year, monthIndex!, 1);
93-
const dayDiff = (dayIndex! - day.getDay() + 7) % 7;
58+
const day = new Date(year, month!, 1);
59+
const dayDiff = (weekday! - day.getDay() + 7) % 7;
9460

9561
return new Date(year, month, 1 + dayDiff + (nth - 1) * 7);
9662
};
@@ -136,18 +102,13 @@ const offsetReviewTime = (time: string, offset: number) => {
136102

137103
const AMorPM = (time: string, offset: number) => {
138104
const startTime = Number(time.charAt(0) + (isTwoDigitHour(time) ? time.charAt(1) : ''));
139-
let AMorPM = time.charAt(time.length - 2) + time.charAt(time.length - 1);
140-
141-
const flipAMPM = () => {
142-
if (AMorPM === 'AM') return 'PM';
143-
return 'AM';
144-
};
105+
const AMorPM = time.charAt(time.length - 2) + time.charAt(time.length - 1);
106+
const startTime24Hour = startTime + (AMorPM === 'PM' && startTime !== 12 ? 12 : 0);
145107

146-
// if odd, the AM / PM should be flipped
147-
const doFlip = Math.floor((startTime + offset - 1) / 12) % 2 === 1;
148-
if (doFlip) AMorPM = flipAMPM();
108+
const newTime = startTime24Hour + offset;
109+
const newHour = (newTime + 24) % 24;
149110

150-
return AMorPM;
111+
return newHour >= 12 ? 'PM' : 'AM';
151112
};
152113

153114
export const formatHourInCurrentTimeZone = (time: string) => {

0 commit comments

Comments
 (0)