Storing hours a merchant is open in postgres

In postgres, I need to store the hours a merchant is open each week. Essentially, the information you’d see posted on a sign in the merchant’s door:

Mondays: Closed
Tuesday-Friday: 9am-1pm, 2pm-6pm
Weekends: 10am-2pm

Some ideas so far:

  1. Break down a week into 30 minute (or even 5 minute) chunks, and use a bit string type to store whether the merchant is open during that chunk.
  2. Use 2-dimensional array type, an array “days”, with each “day” and array of hour ranges when the store was open.

Perhaps relevant is what the data would be used for:

  1. Show the user all the merchants hours, much like the store sign above, on a merchant details page.
  2. Indicate if the store was currently open.
  3. Indicate the amount of time until the store opens next.
  4. Probably other things too…

I’m looking for advice on how to represent this data in postgres.

2

The simplest storage would be:

StoreID        integer (foreign key)
DayOfTheWeek   integer (range 0-6)
OpenTime       time
CloseTime      time
ValidFrom      date (starting date when this record is in use)
ValidThru      date (ending date when this record is in use, NULL for no ending date)

All of the above fields should be required (NOT NULL). ValidThru could be NULL depending on how you want to mark a record as being open ended.

At some point, you will need to consider specialty days (usually related to a holiday) that may need a date field instead of the DayOfTheWeek field. If you do, remember to apply this data after you build the open hours from the storage above.

5

I programmed a system for working out shifts for a fast food outlet where we had the same problem.

I think you have missed the hardest problem which is timezones and daylight savings.

If you only have to print the information then the ‘time without timezone’ data type should be fine, but for any calculation of hours open or similar you should beware edge cases.

We resorted to storing the hour and minute as ints. This gave a clear indication of the ‘printable times’ but prevented any misuse of date time functions.

Where such calculations were required we first converted back to UTC in the relevant timezone in the relevant date.

Also, you may need to consider mutiple opening and closings in a ‘day’. For a full normalised structure you would need:

[Store]
Id
Timezone

[Day]
Id
StartDate
StoreId

[OpenPeriod]
DayId
OpenHour
OpenMinute
CloseHour
CloseMinute

Note: (im remembering more now) ‘days’ can be longer than 24h

Hours are ‘the hour the clock says’ not hour since midnight or anything

2

I would store the data as a series of start and stop times, in which each period can have multiple entries. You can setup the period as a string in which the “period” is anything you want to be, or is a defined day.

Your code will need to ensure that you aren’t entering overlapping data, but reading from this system would be very simple.

table:

field name | field type
------------------------------------------------
period     | string (ex: Monday, T/TH, Weekends)
start time | time   (ex: 01am; 02:30pm)
end time   | time   (ex: 01am; 02:30pm)

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật