I am trying to add a feature on one of my applications. Here is a sample:
Company ABC
Business hours:
Sunday closed
Monday 7am - 5pm
Tuesday 7am - 5pm
Wednesday 7am - 5pm
Thursday 7am - 5pm
Friday 7am - 5pm
Saturday closed
Company DEF
Sunday 10am - 12pm
Monday 8am - 4pm
Tuesday 8am - 4pm
Wednesday 8am - 4pm
Thursday 8am - 4pm
Friday 8am - 4pm
Saturday closed
Now, what I want to do is to have my users be able to search/filter the business hours. For example, only return businesses who are open during Monday 2PM, or Saturday 1PM, or Monday any time.
My idea is to use datetime in SQL Server, but it appears I may need to create 7 columns for each day (Sunday to Saturday) and different columns for Starting time to open to Closing time to open.
The next idea is to make these columns searchable such that I will be able to filter them. for example, if a user searches for Monday 12PM, the search will be Monday (open time) <= (user search, 12pm) <= (close time).
So basically, it will be similar to:
SELECT *
FROM Companies
WHERE [user_search_time] IS "BETWEEN open_time AND close_time WHERE day=[user_search_day]
I'm having a hard time designing this, let alone how to implement it.
Technology used is C#/ASP.Net and SQL Server 2008.
The problem is how to design/implement this feature using the technologies mentioned above.
Any idea/tips will be appreciated. Thanks!
Might want to push this to codereview, or programmers. I think you are on the right path, but instead of adding columns, add a new table. called business_hours
BUSINESS_HOURS
business (company fk)
open_time (datetime)
close_time (datetime)
day (M,T,W,TH,F,S,Sun) // you can do this however u want 1-7 would work as well
Then your query is almost the same as what you have.
SELECT *
FROM
business_hours
WHERE
business = user_company_id
[user_search_time] IS BETWEEN open_time AND close_time and
day=[user_search_day]
Benefits to a table is it's more denormalized and you could support hours that have breaks in them. I could suport 12-2 as well as 5-8. You would just insert a new record and it would work. I am not a big fan of doing all day by doing a range from 12AM to 12PM but it will work, and keep your design simple.
Related
Background: where I park my car, during the winter it is cleaning day once a week (=parking is not allowed) so I have to move the car before that to avoid getting a ticket. I want to create an iCal reminder about that but the pattern is very complicated.
The season starts Nov 1 and ends May 15.
Only weekdays are cleaning nights.
There are two repeating patterns, yearly (Nov to May) and every 4/6 days (see the examples below for details)
Examples:
I am parked on a street where Wednesday is the cleaning day (i.e., "Wednesday street") so Tuesday evening I move the car to a street that just was cleaned (i.e., a "Tuesday street"). Every week the day I have to move the car is moved forward one day. The repeating pattern is "every 6th day".
However, if I have parked on a Monday street, the situation is different - then I want to move the car on the Friday, not the Sunday. Suddenly the repeating pattern is "every 4th day".
I have read this question Can iCal schedule an event for the first weekday after BYMONTHDAY if BYMONTHDAY is a weekend? and have figured out a couple things:
I can set BYMONTH=11,12,1,2,3,4,5 to limit it to Nov to May.
BYDAY=MO,TU,WE,TH,FR limits it to weekdays
but after that I am stuck. I have been playing around at http://recurrence-expansion-service.appspot.com/ with different combinations but so far without success.
How do I limit it to mid-May?
How do I write a rule that "skips" the weekends when I have parked on a Monday street?
How do I achieve two repeating patterns (a yearly pattern - Nov to May - and a 4/6 day pattern)?
I want to get first day of week for specificid ZonedDateTime in NodaTime.
But week starts Saturday in Persian calendar and Monday in Gregorian calendar.
How can I get first day of week based on calendar of ZonedDateTime?
We don't expose that information, because it's not as cut and dried as you expect it to be. Different cultures and contexts use different week rules - for example, while you've stated that the week starts on Monday in the Gregorian calendar, that's context specific. In many contexts Sunday is used as the first day of the week instead.
See the week numbering part of the Wikipedia article on weeks for examples of this.
It sounds like you'll probably want a Dictionary<CalendarSystem, IsoDayOfWeek> or possibly a Dictionary<CalendarSystem, IWeekYearRule> in your application, depending on what you're trying to achieve.
I'm developing an ontology in Protégé that allows you to search for movies in various cinemas according to different criteria (date, start time, actor …).
The problem can be generalized to how to organize any type of event over time. But let's stick with movies. Given a film, I want to organize the shows of the film by giving it a list of values like this: date - start times - price.
Example: FilmX
2018-01-06 4PM 6PM 8PM 10PM 12PM 10$
2018-01-07 4PM 6PM 8PM 10PM 12PM 10$
2018-01-08 4PM 6PM 8PM 10PM 10$ 8$
2018-01-09 4PM 6PM 8PM 10PM 10$ 8$
…
I thought about defining a custom Data Type able to represent this type of information, but I do not think it is the best solution. Especially because I would like to be able to query the ontology using this information efficiently.
Can you give me some advice?
In Brazil my payday happen on every 5th day of month.
But if the 5th day is a saturday/sunday, then the payment will happen on the 4th day.
Same if 4th is saturday/sunday: then the payment will happen on 3rd day. And so on.
Is there any way to create a event that will work with Google Calendar and iCal?
You can achieve that with iCalendar, at least as far as it concerns weekends.
Consider the following rule:
DTSTART;TZID=US-Eastern:20160505T000000
RRULE:FREQ=MONTHLY;BYMONTHDAY=1,2,3,4,5;BYDAY=MO,TU,WE,TH,FR;BYSETPOS=-1
The rule is evaluated as follows:
FREQ=MONTHLY;BYMONTHDAY=1,2,3,4,5: expand to the 1st, 2nd, 3rd, 4th and 5th day of each month
BYDAY=MO,TU,WE,TH,FR: filter by week day, retain only work days/remove weekend days
BYSETPOS=-1: retain only the last day of the set
You can verify the result at http://recurrence-expansion-service.appspot.com/
Here are the first 100 instances.
It's not possible to achieve that for holidays though.
Update:
It's actually sufficient to specify
RRULE:FREQ=MONTHLY;BYMONTHDAY=3,4,5;BYDAY=MO,TU,WE,TH,FR;BYSETPOS=-1
Since the weekend is at most 2 days long, payday must fall on one of the 3rd, 4th or 5th day of the month. Again, this does not take holidays into account.
Holidays always need special consideration. Not only does RRULE not support this kind of logic for arbitrary days, sometimes it's difficult or even impossible to express holidays by an RRULE themselves. In some countries certain holidays are always on a specific day, except when the days falls on a weekend, in which case the holiday is on the Friday before or the Monday after the weekend. Or consider the Easter date and holidays relative to the Easter date, they can't be expressed by an RRULE at all.
Not to forget that some holidays may change every now and then (like "King's days" or "Queen's days" in certain countries).
The best you can do is to manage these manually and add an RDATE and an EXDATE if payday would fall on a holiday according to the RRULE above.
I saw a few examples on stackoverflow on how to design a database table to store business hours but they doesn't answer all my needs. They doesn't support defining different hours depending of the periode of the year and also doesn't support holidays and special time of the year were they can be closed.
My needs
support business hours that overlap 2 days. Example, a bar can open at 6pm and close at 3am
support multiple opening hours in the same day
support dates were they are closed
support different cases where the open/close hours are different during a certain period of time
Scenarios
In general, the store #1 will have these open hours
Monday, 9am to 12pm and 1pm to 5pm
Tuesday, 9am to 12pm and 1pm to 5pm
Wednesday, 9am to 12pm and 1pm to 5pm
Thursday, 9am to 12pm and 1pm to 9pm
Friday, 9am to 12pm and 1pm to 9pm
Saturday, 10am to 5pm
Sunday, closed
During the month of december, the opens hours are different
Monday, 9am to 12pm and 1pm to 9pm
Tuesday, 9am to 12pm and 1pm to 9pm
Wednesday, 9am to 12pm and 1pm to 9pm
Thursday, 9am to 12pm and 1pm to 9pm
Friday, 9am to 12pm and 1pm to 9pm
Saturday, 10am to 5pm
Sunday, 10am to 5pm
They are closed on these dates:
december 25
january 1
And for some reasons, they have specials cases were the open hours can be different:
july 10, 1pm to 9pm
september 20, 1pm to 9pm
My solution so far
StoreId BeginDate EndDate DayOfWeek OpenHour Duration
1 2015-01-01 2015-11-30 2 09:00 180
1 2015-01-01 2015-11-30 2 13:00 240
1 2015-01-01 2015-11-30 3 09:00 180
1 2015-01-01 2015-11-30 3 13:00 240
...
1 2015-12-01 2015-12-31 2 09:00 180
1 2015-12-01 2015-12-31 2 13:00 480
1 2015-12-01 2015-12-31 3 09:00 180
1 2015-12-01 2015-12-31 3 13:00 480
...
The problem that i see
I'm not sure that the BeginDate/EndDate should be in that table. Maybe i should have another table that will define Periods and have a foreign key on the OpenHours table that will link to a period.
Where should i define closed dates (holidays)?
Where should i define a special date where the open/close hours is different? Like an override of what is defined?
Stop thinking about rules. Think about rows.
This is dead simple if you just store open hours. PostgreSQL has particularly good support for this kind of thing.
create table business_hours (
open tstzrange primary key,
exclude using gist (open with &&)
);
The exclusion constraint guarantees no overlapping open hours. If it takes two rows per day, a year's data is little more than 700 rows. 100 years of data is only 70k rows. This is the most flexible option, development and testing time is almost nil, and a minimum-wage clerk can verify that the hours you're about to advertise match the hours you're going to be open.
The normal hours
-- The "normal" hours for the week starting Apr 13, 2015 (a Monday).
insert into business_hours values
-- Mon
(tstzrange('2015-04-13 09:00', '2015-04-13 12:00')),
(tstzrange('2015-04-13 13:00', '2015-04-13 17:00')),
-- Tue
(tstzrange('2015-04-14 09:00', '2015-04-14 12:00')),
(tstzrange('2015-04-14 13:00', '2015-04-14 17:00')),
-- Wed
(tstzrange('2015-04-15 09:00', '2015-04-15 12:00')),
(tstzrange('2015-04-15 13:00', '2015-04-15 17:00')),
-- Thu
(tstzrange('2015-04-16 09:00', '2015-04-16 12:00')),
(tstzrange('2015-04-16 13:00', '2015-04-16 21:00')),
-- Fri
(tstzrange('2015-04-17 09:00', '2015-04-17 12:00')),
(tstzrange('2015-04-17 13:00', '2015-04-17 21:00')),
-- Sat
(tstzrange('2015-04-18 10:00', '2015-04-18 17:00'));
-- Sun
-- Closed.
It should be clear from inserting just the "normal" hours that this kind of table can accommodate any kind of logic, whether good or bad.
You can wrap that kind of statement in a stored function in such a way that you can generate a week, a month, or a year of "normal" hours at one time. Update as needed.
In other dbms, you can use two timestamp columns and some check constraints. Checking for overlapping rows would probably have to be done as an exception report rather than as a constraint enforced by the dbms.
create table business_hours (
opens timestamp not null,
closes timestamp not null,
check (closes > opens),
primary key (opens, closes)
);
Using the pair of columns as a primary key lets the optimizer use index-only scans.
Im working on the same problem, and so far I have designed a similar approach to yours. But in regards to your questions.
Yes, it helps to move the start and end dates to a related table, for reasons seen in #3.
Closed dates could just be when the open hour is null
Default hours would have no start and end dates. All special hours would have defined start and end dates. The app looks up which groups of dates include todays date. It calculates the interval of the start and end of all groups. The smallest interval wins.
BTW, I am probably going to store my open hour by minute of the day, and leave formatting for later...
I would store the opening hours as RFC 5445 RRules and ExRules.
Some libraries have functions to show the rule in English text.
I find it's easy to generate RRules and ExRules using Google Calendar.