开发者

Storing hour-by-hour availability with PostgreSQL

开发者 https://www.devze.com 2023-04-05 23:14 出处:网络
I had an idea to store hour-by-hour availability using a binary string that I wanted to run by the community for advice.

I had an idea to store hour-by-hour availability using a binary string that I wanted to run by the community for advice.

My idea is generate a binary string that indicates availability for any given hour in the week using "0" for unavailable and "1" for available. I could then query against the binary column with the integer for the "c开发者_JS百科urrent hour" with using the bitwise "and" operator. A string would look like the following

# Open sunday at 6:00am and close at 6:00pm (repeats 0 for remainder of hours)
000000111111111111000000...

Looking at the page at 4:00pm on Sunday would run the bitwise "and" with the previous string and this:

# Sunday at 4:00pm (repeats 0 for remainder of hours)
000000000000000100000000

If the returning bitwise comparison resulted in a value greater than 1, the record is currently available.

I have two questions. The first is which datatype I should store the value as. It'll be 168 bit value at most (7 days * 24 hours). The second is which functions Postgres provides that would be able to accomplish what I mentioned. Something like this:

# String truncated for readability
WHERE some_column & $number_for_current_hour > 0

Thanks for reading! Any feedback is appreciated.


You might just be better off if you store both an "Open Time" and a "Close Time". This will be greatly easier for developers to process. If you have some sort of need for multiple open periods during the day, such as places that are open in the morning, closed at lunch, and then open again in the afternoon, I suggest that you create a table of "open times" with a "Open Time" and "Close Time" as well as foreign key linking back to the original table so that you can store multiple open periods. You may think you are being clever with your bitwise or stuff, but really just making things more complicated. Plus, you can't accommodate for things happening on the half hour, or even finer resolution. Re-reading the question, I see you want to store multiple days. If you need different open times for each day, then my second solution would work well. Have a table like this.

ID (INT), F_ID (INT), DAY_OF_WEEK (INT), OpenTime (Time), CloseTime (Time)

ID is just auto increment for this table, F_ID is a foreign key back to the original table, day of week can be an integer or enum representing day of week, OpenTime and CloseTime are "Time" fields which can store a time, without the date. This should make things much more clear to yourself, and anybody else having to work on the code in the future. Plus it will probably be faster to do lookups, as you can index your time fields. Doing a binary xor is equivalent to using "LIKE '%word%' for seaching strings and should be avoided.


If this was MySQL I would tell you to use the SET datatype, but PostgesSQL doesn't seem to have that. Instead you'll need the BIT datatype, which works, but not as conveniently.

0

精彩评论

暂无评论...
验证码 换一张
取 消