Skip to content

Commit

Permalink
Fix timezone bug when comparing dates in the mentor attendance calendar
Browse files Browse the repository at this point in the history
  • Loading branch information
smartspot2 committed Dec 5, 2024
1 parent 4f38857 commit 955843e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { DateTime } from "luxon";
import randomWords from "random-words";
import React, { useState, useEffect } from "react";

import { DEFAULT_TIMEZONE } from "../../utils/datetime";
import {
useSectionAttendances,
useUpdateStudentAttendancesMutation,
Expand Down Expand Up @@ -124,9 +125,15 @@ const MentorSectionAttendance = ({ sectionId }: MentorSectionAttendanceProps): R
if (selectedOccurrence === null) {
// only update selected occurrence if it has not been set before

// compute the start of the current day; the DB works in the timezone specified by DEFAULT_TIMEZONE,
// so we must convert to the DB timezone before setting the start of the day.
// there's no need to convert back here, since we only use this DateTime for comparison
const curDayStart = DateTime.now().setZone(DEFAULT_TIMEZONE).startOf("day");

// filter for future occurrences
const now = DateTime.now().startOf("day");
const futureOccurrences = newSortedOccurrences.filter(occurrence => DateTime.fromISO(occurrence.date) >= now);
const futureOccurrences = newSortedOccurrences.filter(
occurrence => DateTime.fromISO(occurrence.date, { zone: DEFAULT_TIMEZONE }) >= curDayStart
);
// set to the most recent future occurrence
setSelectedOcurrence(futureOccurrences[futureOccurrences.length - 1]);
newAttendances = newOccurrenceMap.get(newSortedOccurrences[0]?.id)?.attendances;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { DateTime, Info } from "luxon";
import React, { useEffect, useState } from "react";

import { DEFAULT_TIMEZONE } from "../../../utils/datetime";

import LeftArrow from "../../../../static/frontend/img/angle-left-solid.svg";
import RightArrow from "../../../../static/frontend/img/angle-right-solid.svg";

Expand Down Expand Up @@ -35,7 +37,7 @@ export const CalendarMonth = ({
useEffect(() => {
if (selectedOccurrence != null) {
// upon change of the selected occurence, make sure the calendar also matches
const selectedDateTime = DateTime.fromISO(selectedOccurrence);
const selectedDateTime = DateTime.fromISO(selectedOccurrence, { zone: DEFAULT_TIMEZONE });
if (curMonth !== selectedDateTime.month) {
setCurMonth(selectedDateTime.month);
}
Expand Down

0 comments on commit 955843e

Please sign in to comment.