In PostgreSQL 11, I am trying to get a weekend time range. From 17:00 Friday to Sunday 17:00.
So far I am able to get a working day by doing
select * from generate_series(date '2021-01-01',date '2021-12-31',interval '1' day) as t(dt) where extract (dow from dt) between 1 and 5;
However, I am have trouble creating 2 columns from start (17:00 Friday) to finish (17:00 Sunday).
Expected output should be something like this:
start stop
2022-10-07 17:00 2022-10-09 17:00
2022-10-14 17:00 2022-10-16 17:00
2022-10-21 17:00 2022-10-23 17:00
To get a series of all hours between 17:00 on Friday and 17:00 on Sunday.
SELECT
*
FROM
generate_series(timestamp '2021-01-01', timestamp '2021-12-31', interval '1' hour) AS t (dt)
WHERE
extract(dow FROM dt) IN (5, 6, 0)
AND CASE WHEN extract(dow FROM dt) = 5 THEN
extract(hour FROM dt) >= 17
WHEN extract(dow FROM dt) = 0 THEN
extract(hour FROM dt) <= 17
ELSE
extract(hour FROM dt) IS NOT NULL
END;
UPDATE
Get two timestamps that represent start and stop of each period Friday 17:00 to Sunday 17:00 over a range of dates.
SELECT
dt + '17:00'::time as start, (dt + '17:00'::time) + '2 days'::interval as stop
FROM
generate_series(date '2022-01-01', date '2022-12-31', interval '1' day) AS t (dt)
WHERE
extract(dow FROM dt) = 5
;
start | stop
-------------------------+-------------------------
01/07/2022 17:00:00 PST | 01/09/2022 17:00:00 PST
01/14/2022 17:00:00 PST | 01/16/2022 17:00:00 PST
01/21/2022 17:00:00 PST | 01/23/2022 17:00:00 PST
01/28/2022 17:00:00 PST | 01/30/2022 17:00:00 PST
02/04/2022 17:00:00 PST | 02/06/2022 17:00:00 PST
02/11/2022 17:00:00 PST | 02/13/2022 17:00:00 PST
02/18/2022 17:00:00 PST | 02/20/2022 17:00:00 PST
02/25/2022 17:00:00 PST | 02/27/2022 17:00:00 PST
03/04/2022 17:00:00 PST | 03/06/2022 17:00:00 PST
03/11/2022 17:00:00 PST | 03/13/2022 17:00:00 PDT
03/18/2022 17:00:00 PDT | 03/20/2022 17:00:00 PDT
03/25/2022 17:00:00 PDT | 03/27/2022 17:00:00 PDT
...
--timestamptz type.
SELECT
(day + interval '17:30') AS start,
(day + interval '17:30' + interval '2 days') AS
END
FROM
generate_series(date '2022-10-01', date '2022-12-31', interval '1' day) _ (day)
WHERE
EXTRACT(ISODOW FROM day) = 5;
--timestamp type.
SELECT
(day + interval '17:30')::timestamp AS start,
(day + interval '17:30' + interval '2 days')::timestamp AS
END
FROM
generate_series(date '2022-10-01', date '2022-12-31', interval '1' day) _ (day)
WHERE
EXTRACT(ISODOW FROM day) = 5;
I do checked the calendar, it works.
i have a generate series of every minute for the whole 24 hours. how about a generate _series showing every hour only in hh24:mi:ss and hh24:mi format.
result should be
00:00
01:00
02:00
:
:
23:00
This query:
SELECT to_char(generate_series(0, 23) * interval '1 hour' + '2019-01-01 00:00:00'::timestamp, 'HH24:MI')
will return:
00:00
01:00
02:00
03:00
...
23:00
Just modify the format string for what you desire.
I am trying to check if the current time if between two predermined times and that the day is Monday, Tuesday, Wednesday or Thursday. Something like:
if day = monday, tuesday, wednesday or thursday
if current_time > 6:00 PM and current_time < 6:15 PM
then do something
Here is what I have so far, the only bit left is setting the two times I want to check between:
weekday = datetime.datetime.today().weekday()
hour = datetime.datetime.time(datetime.datetime.now())
if weekday < 5:
if hour
You may use datetime.weekday() method and attributes: datetime.hour and datetime.minute.
now = datetime.datetime.now()
if now.weekday() < 5 and now.hour == 18 and 0 <= now.minute <= 15:
do_something()
This example takes a base date and adds 7½ hours, 1 day 7½ hours, 2 days 7½ hours, and so on.
use Date::Manip;
use DateTime;
use DateTime::Format::DateManip;
Date::Manip::Date_Init("TZ=America/New_York", "Language=English");
my $otime = DateTime->new(
year => 2013,
month => 3,
day => 4,
hour => 0,
minute => 0,
second => 0,
time_zone => 'America/New_York',
);
my $t1 = UnixDate($otime, "%i:%M %p on %A, %B %e, %Y ");
print "original $t1\n";
for (my $i = 0; $i <= 20; $i++) {
my $dtw = $otime->clone();
$dtw->add(
minutes => (15) * 30,
days => ($i),
);
$t1 = UnixDate($dtw, "%i:%M %p on %A, %B %e, %Y ");
print "$i days $t1\n";
}
When adding 6 days 7½ hours, the result contains an extra hour.
original 12:00 AM on Monday, March 04, 2013
0 days 07:30 AM on Monday, March 04, 2013
1 days 07:30 AM on Tuesday, March 05, 2013
2 days 07:30 AM on Wednesday, March 06, 2013
3 days 07:30 AM on Thursday, March 07, 2013
4 days 07:30 AM on Friday, March 08, 2013
5 days 07:30 AM on Saturday, March 09, 2013
6 days 08:30 AM on Sunday, March 10, 2013 # why 8:30 and not 7:30?
7 days 07:30 AM on Monday, March 11, 2013
8 days 07:30 AM on Tuesday, March 12, 2013
9 days 07:30 AM on Wednesday, March 13, 2013
10 days 07:30 AM on Thursday, March 14, 2013
11 days 07:30 AM on Friday, March 15, 2013
12 days 07:30 AM on Saturday, March 16, 2013
13 days 07:30 AM on Sunday, March 17, 2013
14 days 07:30 AM on Monday, March 18, 2013
15 days 07:30 AM on Tuesday, March 19, 2013
16 days 07:30 AM on Wednesday, March 20, 2013
17 days 07:30 AM on Thursday, March 21, 2013
18 days 07:30 AM on Friday, March 22, 2013
19 days 07:30 AM on Saturday, March 23, 2013
20 days 07:30 AM on Sunday, March 24, 2013
Because Daylight Saving Time begins on March 10, 2013 in the America/New_York timezone. DateTime first adds $i days (to get midnight on March 10) and then adds 450 minutes to get 8:30 AM (because the minute after 1:59 AM on March 10 is 3:00 AM). The order of the parameters to add is not meaningful; see Adding a Duration to a Datetime.
Because it adds days & minutes separately (and processes the days first), the effect only happens on the date when DST actually begins or ends. If you want a particular time, just set it directly instead of using add. Or call add twice, once to add minutes, then again to add days.
i need to get splited intervals and the number of overlapping intervals, eg
basedata:
interval A: startTime 08:00, endTime 12:00
interval B: startTime 09:00, endTime 12:00
interval C: startTime 12:00, endTime 16:00
interval D: startTime 13:00, endTime 14:00
now i have a separate interval from 10:00 to 15:00 and have to determine what intervals are intersected at first. result should be something like:
1: 10:00 - 12:00 ( intersecting with interval A )
2: 10:00 - 12:00 ( intersecting with interval B )
3: 12:00 - 15:00 ( intersecting with interval C )
4: 13:00 - 14:00 ( intersecting with interval D )
this part works fine, the following causes the trouble:
i need some kind of weighting for parallel intervals. this also means, that it can occur that an interval-intersection must be splitted n times, if it's ( partly ) intersected by another one.
in the upper example the expecting result would be:
1: 10:00 - 12:00 -> weightage: 50%
2: 10:00 - 12:00 -> weightage: 50%
3.1: 12:00 - 13:00 -> weightage: 1oo%
3.2: 13:00 - 14:00 -> weightage: 50%
3.3: 14:00 - 15:00 -> weightage: 50%
4: 13:00 - 14:00 -< weightage: 100%
the splitting of interval 3 is caused by the intersecting with interval 4 between 13:00 and 14:00.
sql-server is ms-sql 2008.
thanks for help in advance!
If I understand what you're trying to do correctly, shouldn't your expected result be
1: 10:00 - 12:00 -> weightage: 50%
2: 10:00 - 12:00 -> weightage: 50%
3.1: 12:00 - 13:00 -> weightage: 1oo%
3.2: 13:00 - 14:00 -> weightage: 50%
3.3: 14:00 - 15:00 -> weightage: 50%
4: 13:00 - 14:00 -< weightage: 50%
since 13:00-14:00 is used twice?