From 65250d77beec81f9d336302c4b185dcd14518917 Mon Sep 17 00:00:00 2001 From: Pieter Vander Vennet Date: Wed, 18 Oct 2023 02:08:49 +0200 Subject: [PATCH] Fix: fix #1675 --- src/UI/OpeningHours/OpeningHours.ts | 82 ++++++++++++++++++------ src/UI/OpeningHours/OpeningHoursInput.ts | 1 + 2 files changed, 64 insertions(+), 19 deletions(-) diff --git a/src/UI/OpeningHours/OpeningHours.ts b/src/UI/OpeningHours/OpeningHours.ts index 8bf3b195ee..cd62fdd794 100644 --- a/src/UI/OpeningHours/OpeningHours.ts +++ b/src/UI/OpeningHours/OpeningHours.ts @@ -289,6 +289,14 @@ export class OH { * rules[0].startHour // => 11 * rules[3].endHour // => 19 * + * const rules = OH.ParseRule("Mo 20:00-02:00"); + * rules.length // => 2 + * rules[0].weekday // => 0 + * rules[0].startHour // => 20 + * rules[0].endHour // => 0 + * rules[1].weekday // => 1 + * rules[1].startHour // => 0 + * rules[1].endHour // => 2 */ public static ParseRule(rule: string): OpeningHour[] { try { @@ -414,14 +422,14 @@ export class OH { } /* - This function converts a number of ranges (generated by OpeningHours.js) into all the hours of day that a change occurs. - E.g. - Monday, some business is opended from 9:00 till 17:00 - Tuesday from 9:30 till 18:00 - Wednesday from 9:30 till 12:30 - This function will extract all those moments of change and will return 9:00, 9:30, 12:30, 17:00 and 18:00 - This list will be sorted - */ +This function converts a number of ranges (generated by OpeningHours.js) into all the hours of day that a change occurs. +E.g. +Monday, some business is opended from 9:00 till 17:00 +Tuesday from 9:30 till 18:00 +Wednesday from 9:30 till 12:30 +This function will extract all those moments of change and will return 9:00, 9:30, 12:30, 17:00 and 18:00 +This list will be sorted +*/ public static allChangeMoments( ranges: { isOpen: boolean @@ -507,9 +515,9 @@ export class OH { } /* - Calculates when the business is opened (or on holiday) between two dates. - Returns a matrix of ranges, where [0] is a list of ranges when it is opened on monday, [1] is a list of ranges for tuesday, ... - */ +Calculates when the business is opened (or on holiday) between two dates. +Returns a matrix of ranges, where [0] is a list of ranges when it is opened on monday, [1] is a list of ranges for tuesday, ... +*/ public static GetRanges( oh: any, from: Date, @@ -560,6 +568,9 @@ export class OH { return values } + /** + * OH.parseHHMM("12:30") // => {hours: 12, minutes: 30} + */ private static parseHHMM(hhmm: string): { hours: number; minutes: number } { if (hhmm === undefined || hhmm == null) { return null @@ -575,6 +586,10 @@ export class OH { return hm } + /** + * OH.ParseHhmmRanges("20:00-22:15") // => [{startHour: 20, startMinutes: 0, endHour: 22, endMinutes: 15}] + * OH.ParseHhmmRanges("20:00-02:15") // => [{startHour: 20, startMinutes: 0, endHour: 2, endMinutes: 15}] + */ private static ParseHhmmRanges(hhmms: string): { startHour: number startMinutes: number @@ -641,24 +656,53 @@ export class OH { endHour: number endMinutes: number }[] - ) { + ): { + weekday: number + startHour: number + startMinutes: number + endHour: number + endMinutes: number + }[] { if ((weekdays ?? null) == null || (timeranges ?? null) == null) { return null } const ohs: OpeningHour[] = [] for (const timerange of timeranges) { + const overMidnight = + !(timerange.endHour === 0 && timerange.endMinutes === 0) && + (timerange.endHour < timerange.startHour || + (timerange.endHour == timerange.startHour && + timerange.endMinutes < timerange.startMinutes)) for (const weekday of weekdays) { - ohs.push({ - weekday: weekday, - startHour: timerange.startHour, - startMinutes: timerange.startMinutes, - endHour: timerange.endHour, - endMinutes: timerange.endMinutes, - }) + if (!overMidnight) { + ohs.push({ + weekday: weekday, + startHour: timerange.startHour, + startMinutes: timerange.startMinutes, + endHour: timerange.endHour, + endMinutes: timerange.endMinutes, + }) + } else { + ohs.push({ + weekday: weekday, + startHour: timerange.startHour, + startMinutes: timerange.startMinutes, + endHour: 0, + endMinutes: 0, + }) + ohs.push({ + weekday: (weekday + 1) % 7, + startHour: 0, + startMinutes: 0, + endHour: timerange.endHour, + endMinutes: timerange.endMinutes, + }) + } } } return ohs } + public static getMondayBefore(d) { d = new Date(d) const day = d.getDay() diff --git a/src/UI/OpeningHours/OpeningHoursInput.ts b/src/UI/OpeningHours/OpeningHoursInput.ts index 78858cc97f..7802fed9a8 100644 --- a/src/UI/OpeningHours/OpeningHoursInput.ts +++ b/src/UI/OpeningHours/OpeningHoursInput.ts @@ -82,6 +82,7 @@ export default class OpeningHoursInput extends InputElement { const rules = valueWithoutPrefix.data?.split(";") ?? [] for (const rule of rules) { if (OH.ParsePHRule(rule) !== null) { + // We found the rule containing the public holiday information ph = rule break }