agora inbox for pgsql-sql@postgresql.org  
help / color / mirror / Atom feed
Number of days in a tstzrange?
5+ messages / 3 participants
[nested] [flat]

* Number of days in a tstzrange?
@ 2013-10-24 20:46  Craig R. Skinner <skinner@britvault.co.uk>
  0 siblings, 1 reply; 5+ messages in thread

From: skinner@britvault.co.uk @ 2013-10-24 20:46 UTC (permalink / raw)
  To: pgsql-sql

Hi folks,

How can the number of days contained within a range be found? (9.2)

For example, with these timestamp ranges,
			get these (integer) number of days:

tstzrange('2013-10-01 07:00', '2013-10-01 07:15') | 1 (day)
tstzrange('2013-10-01 07:00', '2013-10-01 23:45') | 1 (day)
tstzrange('2013-10-01 02:00', '2013-10-02 23:45') | 2 (days)
tstzrange('2013-10-01 07:00', '2013-10-03 01:00') | 2 (days)
tstzrange('2013-10-01 01:00', '2013-10-03 23:00') | 3 (days)
tstzrange('2013-10-01 23:00', '2013-10-04 01:00') | 4 (days)

In my digging about, I've not found a builtin function for this.

Is is necessary pull out the lower() and upper() timestamp elements,
then get the date interval between them?

Cheers,
-- 
Craig Skinner | http://twitter.com/Craig_Skinner | http://linkd.in/yGqkv7


-- 
Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-sql



^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: Number of days in a tstzrange?
@ 2013-10-24 21:00  Jonathan S. Katz <jonathan.katz@excoventures.com>
  parent: Craig R. Skinner <skinner@britvault.co.uk>
  0 siblings, 1 reply; 5+ messages in thread

From: Jonathan S. Katz @ 2013-10-24 21:00 UTC (permalink / raw)
  To: skinner@britvault.co.uk; +Cc: pgsql-sql

On Oct 24, 2013, at 4:46 PM, Craig R. Skinner wrote:

> Hi folks,
> 
> How can the number of days contained within a range be found? (9.2)
> 
> For example, with these timestamp ranges,
> 			get these (integer) number of days:
> 
> tstzrange('2013-10-01 07:00', '2013-10-01 07:15') | 1 (day)
> tstzrange('2013-10-01 07:00', '2013-10-01 23:45') | 1 (day)
> tstzrange('2013-10-01 02:00', '2013-10-02 23:45') | 2 (days)
> tstzrange('2013-10-01 07:00', '2013-10-03 01:00') | 2 (days)
> tstzrange('2013-10-01 01:00', '2013-10-03 23:00') | 3 (days)
> tstzrange('2013-10-01 23:00', '2013-10-04 01:00') | 4 (days)
> 
> In my digging about, I've not found a builtin function for this.
> 
> Is is necessary pull out the lower() and upper() timestamp elements,
> then get the date interval between them?

Yes, you would have to call lower() and upper() to accomplish that.

Jonathan


-- 
Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-sql



^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: Number of days in a tstzrange?
@ 2013-10-29 11:34  Craig R. Skinner <skinner@britvault.co.uk>
  parent: Jonathan S. Katz <jonathan.katz@excoventures.com>
  0 siblings, 1 reply; 5+ messages in thread

From: skinner@britvault.co.uk @ 2013-10-29 11:34 UTC (permalink / raw)
  To: pgsql-sql

On 2013-10-24 Thu 17:00 PM |, Jonathan S. Katz wrote:
> 
> Yes, you would have to call lower() and upper() to accomplish that.
> 

Here's my final solution inside a function's CUSROR LOOP:

DECLARE
	billable_days	integer;
	...
	..
BEGIN
	...
	...
	SELECT
		-- cast boundries to date
		-- date - date = integer
		upper(billable_item.billable_period)::date -
			lower(billable_item.billable_period)::date
	INTO
		billable_days;

	-- if a subscription is ceased same day it's started,
	-- that day is still chargable, so bump it
	IF billable_days < 1
	THEN
		billable_days := 1;
	END IF;
	...
	...

Thanks,
-- 
Craig Skinner | http://twitter.com/Craig_Skinner | http://linkd.in/yGqkv7


-- 
Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-sql



^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: Number of days in a tstzrange?
@ 2013-10-30 10:40  Craig R. Skinner <skinner@britvault.co.uk>
  parent: Craig R. Skinner <skinner@britvault.co.uk>
  0 siblings, 1 reply; 5+ messages in thread

From: skinner@britvault.co.uk @ 2013-10-30 10:40 UTC (permalink / raw)
  To: pgsql-sql

On 2013-10-29 Tue 11:34 AM |, Craig R. Skinner wrote:
> 
> 	-- if a subscription is ceased same day it's started,
> 	-- that day is still chargable, so bump it
> 	IF billable_days < 1
> 	THEN
> 		billable_days := 1;
> 	END IF;
> 	...
> 	...
> 

This is more accurate:

	-- If a subscription billing_period ends part way through a day,
	IF upper(billable_item.billable_period)::time <> time 'allballs'
	THEN
		-- That partial day is still chargable, so bump it
		billable_days := billable_days + 1;
	END IF;



-- 
Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-sql



^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: Number of days in a tstzrange?
@ 2013-10-30 18:21  Gavin Flower <GavinFlower@archidevsys.co.nz>
  parent: Craig R. Skinner <skinner@britvault.co.uk>
  0 siblings, 0 replies; 5+ messages in thread

From: Gavin Flower @ 2013-10-30 18:21 UTC (permalink / raw)
  To: Craig R. Skinner <skinner@britvault.co.uk>; pgsql-sql

On 30/10/13 23:40, Craig R. Skinner wrote:
> On 2013-10-29 Tue 11:34 AM |, Craig R. Skinner wrote:
>> 	-- if a subscription is ceased same day it's started,
>> 	-- that day is still chargable, so bump it
>> 	IF billable_days < 1
>> 	THEN
>> 		billable_days := 1;
>> 	END IF;
>> 	...
>> 	...
>>
> This is more accurate:
>
> 	-- If a subscription billing_period ends part way through a day,
> 	IF upper(billable_item.billable_period)::time <> time 'allballs'
> 	THEN
> 		-- That partial day is still chargable, so bump it
> 		billable_days := billable_days + 1;
> 	END IF;
>
>
>
even for 1 second over???

I would expect any court of law to set a higher threshold!


Cheers,
Gavin


-- 
Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-sql



^ permalink  raw  reply  [nested|flat] 5+ messages in thread


end of thread, other threads:[~2013-10-30 18:21 UTC | newest]

Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2013-10-24 20:46 Number of days in a tstzrange? Craig R. Skinner <skinner@britvault.co.uk>
2013-10-24 21:00 ` Jonathan S. Katz <jonathan.katz@excoventures.com>
2013-10-29 11:34   ` Craig R. Skinner <skinner@britvault.co.uk>
2013-10-30 10:40     ` Craig R. Skinner <skinner@britvault.co.uk>
2013-10-30 18:21       ` Gavin Flower <GavinFlower@archidevsys.co.nz>

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox