i have a table that lists the opening hours of restaurants. the columns are id, eateries_id, day_of_week, start_time, and end_time. each eatery is represented in the table multiple times because there is a separate entry for each day. see this previous question for more details: determine if a restaurant is open now (like yelp does) using database, php, js
i'm wondering now how to take the data from this table and print it out in a human readable format. for example, instead of saying "M 1-3, T 1-3, W 1-3, Th 1-3, F 1-8" i would like to say "M-Th 1-3, F 1-8". similarly, i want "M 1-3, 5-8" instead of "M 1-3, M 5-8". how might i do this without a brute force method of numerous if statements?
thanks.
Thought I would have a bash at this.
Test Table
CREATE TABLE `opening_hours` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`eateries_id` int(11) DEFAULT NULL,
`day_of_week` int(11) DEFAULT NULL,
`start_time` time DEFAULT NULL,
`end_time` time DEFAULT NULL,
PRIMARY KEY (`id`)
)
Test Data
INSERT INTO `test`.`opening_hours`
(
`eateries_id`,
`day_of_week`,
`start_time`,
`end_time`)
SELECT 2 AS eateries_id, 1 AS day_of_week, '13:00' AS start_time, '15:00' as end_time union all
SELECT 2 AS eateries_id, 1 AS day_of_week, '17:00' AS start_time, '20:00' as end_time union all
SELECT 2 AS eateries_id, 2 AS day_of_week, '13:00' AS start_time, '15:00' as end_time union all
SELECT 2 AS eateries_id, 2 AS day_of_week, '17:00' AS start_time, '20:00' as end_time union all
SELECT 2 AS eateries_id, 3 AS day_of_week, '13:00' AS start_time, '15:00' as end_time union all
SELECT 2 AS eateries_id, 4 AS day_of_week, '13:00' AS start_time, '20:00' as end_time union all
SELECT 2 AS eateries_id, 5 AS day_of_week, '13:00' AS start_time, '15:00' as end_time union all
SELECT 2 AS eateries_id, 6 AS day_of_week, '13:00' AS start_time, '20:00' as end_time union all
SELECT 2 AS eateries_id, 7 AS day_of_week, '13:00' AS start_time, '21:00' as end_time
union all
SELECT 3 AS eateries_id, 1 AS day_of_week, '13:00' AS start_time, '15:00' as end_time union all
SELECT 3 AS eateries_id, 2 AS day_of_week, '13:00' AS start_time, '15:00' as end_time union all
SELECT 3 AS eateries_id, 3 AS day_of_week, '13:00' AS start_time, '15:00' as end_time union all
SELECT 3 AS eateries_id, 4 AS day_of_week, '13:00' AS start_time, '20:00' as end_time union all
SELECT 3 AS eateries_id, 5 AS day_of_week, '13:00' AS start_time, '15:00' as end_time union all
SELECT 3 AS eateries_id, 6 AS day_of_week, '13:00' AS start_time, '20:00' as end_time union all
SELECT 3 AS eateries_id, 7 AS day_of_week, '13:00' AS start_time, '21:00' as end_time
View definition to consolidate opening hours by day
CREATE VIEW `test`.`groupedhours`
AS
select `test`.`opening_hours`.`eateries_id` AS `eateries_id`,
`test`.`opening_hours`.`day_of_week` AS `day_of_week`,
group_concat(concat(date_format(`test`.`opening_hours`.`start_time`,'%l'),' - ',date_format(`test`.`opening_hours`.`end_time`,'%l %p')) order by `test`.`opening_hours`.`start_time` ASC separator ', ') AS `OpeningHours`
from `test`.`opening_hours`
group by `test`.`opening_hours`.`eateries_id`,`test`.`opening_hours`.`day_of_week`
Query to find the 'islands' of contiguous days with the same opening hours (based on one by Itzik Ben Gan)
SET @rownum = NULL;
SET @rownum2 = NULL;
SELECT S.eateries_id,
concat(CASE WHEN
S.day_of_week <> E.day_of_week
THEN
CONCAT(CASE S.day_of_week
WHEN 1 THEN 'Su'
WHEN 2 THEN 'Mo'
WHEN 3 THEN 'Tu'
WHEN 4 THEN 'We'
WHEN 5 THEN 'Th'
WHEN 6 THEN 'Fr'
WHEN 7 THEN 'Sa'
End, ' - ')
ELSE ''
END,
CASE E.day_of_week
WHEN 1 THEN 'Su'
WHEN 2 THEN 'Mo'
WHEN 3 THEN 'Tu'
WHEN 4 THEN 'We'
WHEN 5 THEN 'Th'
WHEN 6 THEN 'Fr'
WHEN 7 THEN 'Sa'
End, ' ', S.OpeningHours) AS `Range`
FROM (
SELECT
A.day_of_week,
@rownum := IFNULL(@rownum, 0) + 1 AS rownum,
A.eateries_id,
A.OpeningHours
FROM `test`.`groupedhours` as A
WHERE NOT EXISTS(SELECT * FROM `test`.`groupedhours` B
WHERE A.eateries_id = B.eateries_id
AND A.OpeningHours = B.OpeningHours
AND B.day_of_week = A.day_of_week -1)
ORDER BY eateries_id,day_of_week) AS S
JOIN (
SELECT
A.day_of_week,
@rownum2 := IFNULL(@rownum2, 0) + 1 AS rownum,
A.eateries_id,
A.OpeningHours
FROM `test`.`groupedhours` as A
WHERE NOT EXISTS(SELECT * FROM `test`.`groupedhours` B
WHERE A.eateries_id = B.eateries_id
AND A.OpeningHours = B.OpeningHours
AND B.day_of_week = A.day_of_week + 1)
ORDER BY eateries_id,day_of_week) AS E
ON S.eateries_id = E.eateries_id AND
S.OpeningHours = S.OpeningHours AND
S.rownum = E.rownum
Results
eateries_id Range
2 Su - Mo 1 - 3 PM, 5 - 8 PM
2 Tu 1 - 3 PM
2 We 1 - 8 PM
2 Th 1 - 3 PM
2 Fr 1 - 8 PM
2 Sa 1 - 9 PM
3 Su - Tu 1 - 3 PM
3 We 1 - 8 PM
3 Th 1 - 3 PM
3 Fr 1 - 8 PM
3 Sa 1 - 9 PM
You want to union a bunch of intervals for each day. Stick to 24h format (actually convert it to seconds first I guess) until you have to convert it to a human-friendlier format.
http://pyinterval.googlecode.com/svn/trunk/html/index.html
The trouble is that when you allow seconds ... a restaurant which closes 1 second earlier will be missed :( Perhaps you need to allow 15 or 5 -minute increments. Round the data in DB if you have to. So, the approach is: using an interval data structure, union all intervals for a given day together. Now reverse the dictionary. Instead of mapping days to intervals, map intervals to days. Now find a way to represent those groups of days intelligently. For instance, set(1,2,3)
can be displayed as "M-W", so I would suggest: for every power set of the set {1,2,3,4,5,6,7}
(or {1,2,3,4,5}
) find the best human representation (by hand). Now hard-code this logic -save it into a dictionary which maps a sorted string (this is important) such as "1235" to a human representation such as "M-W,F". Displaying 1-3, 5-8 is easy, once you work with an interval object as is described in the link above. Good luck! Let me know what problems you run into.
EDIT:
This is not the best example that they have (does not show union of overlapping intervals), but you care about the "|" operator
unioned:
>>> interval[1, 4] | interval[2, 5]
interval([1.0, 5.0])
>>> interval[1, 2] | interval[4, 5]
interval([1.0, 2.0], [4.0, 5.0])
You could just implement this class yourself, but it might be prone to bugs.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With