Skip to content

Commit

Permalink
Fix time zone handling when handling dates in delivery rules
Browse files Browse the repository at this point in the history
  • Loading branch information
viliket committed Sep 16, 2023
1 parent 1cf1c58 commit bc6660a
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions src/utils/passengerInformationMessages.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { differenceInMinutes, format, parseISO, startOfDay } from 'date-fns';
import { utcToZonedTime } from 'date-fns-tz';
import { utcToZonedTime, zonedTimeToUtc } from 'date-fns-tz';
import { orderBy } from 'lodash';

import {
Expand Down Expand Up @@ -277,7 +277,7 @@ const isWithinTimeSpan = (
if (!isWithinStartAndEnd) return false;

const isWithinWeekdays =
!weekDays || weekDays.includes(getCurrentWeekday(now));
!weekDays || weekDays.includes(getCurrentWeekdayInEET(now));
return isWithinWeekdays;
};

Expand Down Expand Up @@ -308,16 +308,17 @@ const isWithinTimeSpanAndHours = (
if (!isWithinStartAndEnd) return false;

const isWithinWeekdays =
weekDays && weekDays.includes(getCurrentWeekday(now));
weekDays && weekDays.includes(getCurrentWeekdayInEET(now));
if (!isWithinWeekdays) return false;

const sameDayStartDateTime = getDateTime(now, startTime);
const sameDayEndDateTime = getDateTime(now, endTime);
return sameDayStartDateTime <= now && now <= sameDayEndDateTime;
};

const getCurrentWeekday = (date: Date): Weekday => {
return format(date, 'EEEE').toUpperCase() as Weekday;
const getCurrentWeekdayInEET = (date: Date): Weekday => {
const dateEET = utcToZonedTime(date, 'Europe/Helsinki');
return format(dateEET, 'EEEE').toUpperCase() as Weekday;
};

/**
Expand All @@ -331,18 +332,19 @@ const getCurrentWeekday = (date: Date): Weekday => {
* @returns A new Date constructed from the given parameters.
*/
const getDateTime = (dateTimeISO: string | Date, timeISOinEET?: string) => {
let dateISO: Date;
let dateTime: Date;
if (dateTimeISO instanceof Date) {
dateISO = dateTimeISO;
dateTime = dateTimeISO;
} else {
dateISO = parseISO(dateTimeISO);
dateTime = parseISO(dateTimeISO);
}
let dateTime = utcToZonedTime(dateISO, 'Europe/Helsinki');
if (timeISOinEET) {
dateTime = utcToZonedTime(dateTime, 'Europe/Helsinki');
const [hours, minutes] = timeISOinEET.split(':').map(Number);
dateTime = startOfDay(dateTime);
dateTime.setHours(hours);
dateTime.setMinutes(minutes);
dateTime = zonedTimeToUtc(dateTime, 'Europe/Helsinki');
}
return dateTime;
};

0 comments on commit bc6660a

Please sign in to comment.