Check opening hours in different timezones - postgresql

My Specs:
Postgres 9.6.6, latest Ubuntu LTS
Server Timezone is GMT
A table with two columns that shows store opening and closing times, with each timezone.
Here's the table:
ShopId OpenAt CloseAt
1 09:00:00 -08 17:00:00 -08
2 09:30:00 -05 17:30:00 -05
3 08:00:00 -11 15:00:00 -11
4 10:00:00 +07 15:30:00 +07
What I need to know is if at moment (at my current GMT time), the shop is open. Taking into consideration that Saturday and Sunday it's closed.
I'm digging around and I got something like:
SELECT ((OpenAt,CloseAt) OVERLAPS(NOW())) AND ISODOW < 6
with no luck...
Thanks
Perez

Try this :
SELECT ((date_trunc('day',nowAtShopLocation)+"OpenAt"::time, date_trunc('day',nowAtShopLocation)+"CloseAt"::time) OVERLAPS(nowAtShopLocation,nowAtShopLocation)) and EXTRACT (ISODOW FROM nowAtShopLocation) <6
from (
select *,now() AT TIME ZONE 'UTC'+(EXTRACT(TIMEZONE_HOUR FROM "OpenAt")||' hour')::interval nowAtShopLocation from your_table
) a

Related

How to filter if start and end timestamp is between other start and end timestamp

Image of database table
This is a database table. I have a reservation start and reservation end timestamp, it is from 2022-04-18 13:00:00 to 2022-04-18 18:00:00 (can be more). So, my problem is how to fetch this data, if i want to check if in this for example timestamp 2022-04-18 14:00:00 to 2022-04-18 15:00:00 is in this timestamp which is in 2022-04-18 13:00:00 to 2022-04-18 18:00:00.
Example:
Reservation start 2022-04-18 10:00:00
Reservation end 2022-04-18 15:00:00
And i want to check this interval 2022-04-18 11:00:00 - 2022-04-18 13:00:00
It should filter from database and output this
reservation_id user_email desk_id reservation_start reservation_end
13 test#test.com 1 2022-04-18 10:00:00 2022-04-18 15:00:00
How to do this? In spring boot jpa, or in postgresql

Extracting epoch from two different days yields same result

If I run this query on my database
SELECT EXTRACT('epoch' FROM age('2021-01-01'::date, '2019-12-01'::date))
The result is 34149600.
But then if I try with 2019-12-02 (one day more)
SELECT EXTRACT('epoch' FROM age('2021-01-01'::date, '2019-12-02'::date))
The result is exactly the same 34149600!
As if the seconds passed from 02 december 2019 to 01 january 2021 are the same as the seconds passed from 01 december 2019 to 01 january 2021.
Why is this? I've already tried the above code with timezones on 00:00:00+00 timezone for all dates (for 1st january 2021, 1st december 2019 and 2nd december 2021), and it gives the same result
Obviously, I would have expected the epoch to be different, around 3600*24 of difference (seconds in a day).
The similar results come from the age() function which returns an interval with years, months, days. In such an interval, 1 month = 30 days, so their conversions in seconds are similar.
You will get the expected result with
SELECT extract(epoch from ('2021-01-01'::timestamp - '2019-12-01'::timestamp)) => 34300800.000000
SELECT extract(epoch from ('2021-01-01'::timestamp - '2019-12-02'::timestamp)) => 34214400.000000

Postgresql Week number same for Jan and Dec dates

In PostgreSQL, I am trying to convert timestamps into week numbers using the query below.
select user_id,
time_stamp,
date(time_stamp),
EXTRACT(YEAR FROM time_stamp) as Year,
EXTRACT(MONTH FROM time_stamp) as Month,
EXTRACT(WEEK FROM time_stamp) as Week
from user_engagement
user_id | time_stamp | date | year | month | week
---------+---------------------+------------+------+-------+------
6282 | 2013-01-01 14:29:35 | 2013-01-01 | 2013 | 1 | 1
6282 | 2013-01-02 14:29:35 | 2013-01-02 | 2013 | 1 | 1
6282 | 2013-12-30 14:29:35 | 2013-12-30 | 2013 | 12 | 1
6282 | 2013-12-31 14:29:35 | 2013-12-31 | 2013 | 12 | 1
I am surprised to see that the week numbers of the dates in Jan and Dec of the year 2013 are the same. Could someone explain the logic here?
I think the documentation explains this pretty well:
week
The number of the ISO 8601 week-numbering week of the year. By
definition, ISO weeks start on Mondays and the first week of a year
contains January 4 of that year. In other words, the first Thursday of
a year is in week 1 of that year.
In the ISO week-numbering system, it is possible for early-January
dates to be part of the 52nd or 53rd week of the previous year, and
for late-December dates to be part of the first week of the next year.
For example, 2005-01-01 is part of the 53rd week of year 2004, and
2006-01-01 is part of the 52nd week of year 2005, while 2012-12-31 is
part of the first week of 2013. It's recommended to use the isoyear
field together with week to get consistent results.

Datetime format change the number of returned rows in PostgreSQL

I really dont get this...
This first statement below returns the expected rows - ONLY rows which have a date which are BEFORE/SMALLER than...
select matchdate, price, size, issell from matchmsg
where matchdate <= '2015-01-17 00:00:00.000000+01' order by matchdate asc
The code below return rows with dates up to the 2015-01-21...!?
select matchdate, price, size, issell from matchmsg
where matchdate <= 'Sat Jan 17 2015 00:00:00 GMT+0100' order by matchdate asc
PS: I use PostgresSQL, NodeJS and NPM Moment...BUT the result is from the PostgreSQL tool pgAminIII...so - it has nothing to do with my own code...!
matchdate in the DB is a "datetime with time zone" like:
"2015-01-16 00:00:22.491956+01"
PostgreSQL does not understand dates of the format Sat Jan 17 2015 00:00:00 GMT+0100 but it's probably trying its best. Here are tables of the dates and time formats it does understand. A complete explanation can be found here.
You'll have to convert Sat Jan 17 2015 00:00:00 GMT+0100 to a format Postgres understands. It is very close to the HTTP date format.

Add month constraint to date_trunc by week

I am trying to truncate several dates of a certain month to the start of the week, except the ones that start in the previous month.
Example:
Wednesday 5 Dec 2012 should be truncated to Monday 3 Dec 2012
Saturday 1 Dec 2012 should not be truncated to Monday 26 Nov 2012
For truncating the dates I am using:
date_trunc('week', dates.d)
The problem is that Saturday 1 Dec 2012 still gets truncated to Monday 26 Nov 2012
Can I add a constraint to it, so that it takes the month into account?
Thanks in advance :)
NOTE: I am using version 9.2
GREATEST(date_trunc('week', dates.d), date_trunc('month', dates.d))