Re: question about postgresql time intervals

Lists: pgsql-general
From: Michael Glaesemann <grzm(at)myrealbox(dot)com>
To: lgray(at)unitrends(dot)com
Cc: pgsql-general <pgsql-general(at)postgresql(dot)org>
Subject: Re: question about postgresql time intervals
Date: 2006-03-14 23:58:03
Message-ID: 8B817E7D-C396-4CF3-B82A-BFE8855BC3F5@myrealbox.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

You will get better, faster answers by sending questions to a
PostgreSQL mailing list. By emailing me directly you may not get a
timely response if I don't have time to answer. Others can then
answer and learn from the subsequent discussion. I'm ccing this to
pgsql-general.

On Mar 15, 2006, at 6:45 , Linda wrote:

> Hi, Michael
>
> I have a question about the output format of the INTERVAL type in
> version
> 8.1.1. In previous versions, I could do the following:
>
> select (uptime::varchar)::interval from machine_info;
>
> where uptime is an INTEGER type, the number of seconds the machine
> has been
> up since last reboot. This used to produce output in this format:
> 21 days 02:47:04
>
> Now in v8.1.1, the output format is
> 506:47:04
>
> How can I get the "justified" output as before? Is there some
> setting of
> datestyle that affects the output? I have tried specifying
> "interval day
> to second" but that doesn't work. Using the new justify_hours
> function
> works, but is it possible to do something that will run on older
> versions
> of postgresql?
>
> Thanks,
> Linda
>
> --
> Linda Gray
> Unitrends Corporation
> 803.454.0300 ext. 241
>

justify_hours is also in 8.1 and should do what you want.

test=# select '506:47:04'::interval;
interval
-----------
506:47:04
(1 row)

test=# select justify_hours('506:47:04'::interval);
justify_hours
------------------
21 days 02:47:04
(1 row)

test=# select version();

version
------------------------------------------------------------------------
----------------------------------------------------------------------
PostgreSQL 8.1.3 on powerpc-apple-darwin8.4.0, compiled by GCC
powerpc-apple-darwin8-gcc-4.0.1 (GCC) 4.0.1 (Apple Computer, Inc.
build 5247)
(1 row)

Michael Glaesemann
grzm myrealbox com


From: Linda <lgray(at)unitrends(dot)com>
To: Michael Glaesemann <grzm(at)myrealbox(dot)com>
Cc: pgsql-general <pgsql-general(at)postgresql(dot)org>
Subject: Re: question about postgresql time intervals
Date: 2006-03-15 14:39:13
Message-ID: 200603150939.13763.lgray@unitrends.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

According to the SQL standard, shouldn't this work?

select '506:47:04'::interval day to second ;

Is there a portable way to do this without using justify_hours()?

- Thanks

On Tuesday 14 March 2006 6:58 pm, Michael Glaesemann wrote:
> You will get better, faster answers by sending questions to a
> PostgreSQL mailing list. By emailing me directly you may not get a
> timely response if I don't have time to answer. Others can then
> answer and learn from the subsequent discussion. I'm ccing this to
> pgsql-general.
>
> On Mar 15, 2006, at 6:45 , Linda wrote:
>
> > Hi, Michael
> >
> > I have a question about the output format of the INTERVAL type in
> > version
> > 8.1.1. In previous versions, I could do the following:
> >
> > select (uptime::varchar)::interval from machine_info;
> >
> > where uptime is an INTEGER type, the number of seconds the machine
> > has been
> > up since last reboot. This used to produce output in this format:
> > 21 days 02:47:04
> >
> > Now in v8.1.1, the output format is
> > 506:47:04
> >
> > How can I get the "justified" output as before? Is there some
> > setting of
> > datestyle that affects the output? I have tried specifying
> > "interval day
> > to second" but that doesn't work. Using the new justify_hours
> > function
> > works, but is it possible to do something that will run on older
> > versions
> > of postgresql?
> >
> > Thanks,
> > Linda
> >
> > --
> > Linda Gray
> > Unitrends Corporation
> > 803.454.0300 ext. 241
> >
>
> justify_hours is also in 8.1 and should do what you want.
>
> test=# select '506:47:04'::interval;
> interval
> -----------
> 506:47:04
> (1 row)
>
> test=# select justify_hours('506:47:04'::interval);
> justify_hours
> ------------------
> 21 days 02:47:04
> (1 row)
>
> test=# select version();
>
> version
> ------------------------------------------------------------------------
> ----------------------------------------------------------------------
> PostgreSQL 8.1.3 on powerpc-apple-darwin8.4.0, compiled by GCC
> powerpc-apple-darwin8-gcc-4.0.1 (GCC) 4.0.1 (Apple Computer, Inc.
> build 5247)
> (1 row)
>
>
> Michael Glaesemann
> grzm myrealbox com
>
>
>
>

--
Linda Gray
Unitrends Corporation
803.454.0300 ext. 241


From: Michael Glaesemann <grzm(at)myrealbox(dot)com>
To: lgray(at)unitrends(dot)com
Cc: pgsql-general <pgsql-general(at)postgresql(dot)org>
Subject: Re: question about postgresql time intervals
Date: 2006-03-16 00:25:06
Message-ID: 7BF4399A-4004-4D80-ABFD-2F1DCD1DC289@myrealbox.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general


On Mar 15, 2006, at 23:39 , Linda wrote:

> According to the SQL standard, shouldn't this work?
>
> select '506:47:04'::interval day to second ;

No one has implemented this in PostgreSQL yet.

> Is there a portable way to do this without using justify_hours()?

Not currently that I know of.

Michael Glaesemann
grzm myrealbox com


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Michael Glaesemann <grzm(at)myrealbox(dot)com>
Cc: lgray(at)unitrends(dot)com, pgsql-general <pgsql-general(at)postgresql(dot)org>
Subject: Re: question about postgresql time intervals
Date: 2006-03-16 01:13:11
Message-ID: 11084.1142471591@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

Michael Glaesemann <grzm(at)myrealbox(dot)com> writes:
> On Mar 15, 2006, at 23:39 , Linda wrote:
>> According to the SQL standard, shouldn't this work?
>>
>> select '506:47:04'::interval day to second ;

> No one has implemented this in PostgreSQL yet.

It depends on what you define as "work". 8.1 says

regression=# select '506:47:04'::interval day to second ;
interval
-----------
506:47:04
(1 row)

8.0 and before say

regression=# select '506:47:04'::interval day to second ;
interval
------------------
21 days 02:47:04
(1 row)

because before 8.1 we didn't distinguish intervals of "1 day" and "24 hours"
as being different. But the syntax has been accepted for a long time,
at least back to 7.0.

If there's some specific functionality you're after, you should say what
it is rather than expecting us to guess what you mean.

regards, tom lane


From: Michael Glaesemann <grzm(at)myrealbox(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: lgray(at)unitrends(dot)com, pgsql-general <pgsql-general(at)postgresql(dot)org>
Subject: Re: question about postgresql time intervals
Date: 2006-03-16 08:36:40
Message-ID: E47C65EA-45E5-4B8B-91CB-32783853C837@myrealbox.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general


On Mar 16, 2006, at 10:13 , Tom Lane wrote:

> But the syntax has been accepted for a long time,
> at least back to 7.0.

Well, look at that! Is this documented? Looks like I should install
the DocBook toolchain again.

Michael Glaesemann
grzm myrealbox com


From: Linda <lgray(at)unitrends(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Michael Glaesemann <grzm(at)myrealbox(dot)com>, pgsql-general <pgsql-general(at)postgresql(dot)org>
Subject: Re: question about postgresql time intervals
Date: 2006-03-16 13:49:10
Message-ID: 200603160849.10840.lgray@unitrends.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

On Wednesday 15 March 2006 8:13 pm, Tom Lane wrote:
> Michael Glaesemann <grzm(at)myrealbox(dot)com> writes:
> > On Mar 15, 2006, at 23:39 , Linda wrote:
> >> According to the SQL standard, shouldn't this work?
> >>
> >> select '506:47:04'::interval day to second ;
>
> > No one has implemented this in PostgreSQL yet.
>
> It depends on what you define as "work". 8.1 says
>
> regression=# select '506:47:04'::interval day to second ;
> interval
> -----------
> 506:47:04
> (1 row)
>
> 8.0 and before say
>
> regression=# select '506:47:04'::interval day to second ;
> interval
> ------------------
> 21 days 02:47:04
> (1 row)
>
> because before 8.1 we didn't distinguish intervals of "1 day" and "24
hours"
> as being different. But the syntax has been accepted for a long time,
> at least back to 7.0.
>
> If there's some specific functionality you're after, you should say what
> it is rather than expecting us to guess what you mean.
>

Hi, Tom

Thanks for your reply. I guess you missed the original email. I have an
application that is retrieving "uptime" (an integer number of seconds since
reboot) and recasting it as varchar and then interval type. For example,

select (1824459::varchar)::interval;

Which in previous versions of Postgres returned '21 days 02:47:39'. If I
use "justify_hours" the application will not run on older versions of
Postgres, and will not be portable to other DBs. I thought perhaps some
"datestyle" setting was different or some other factor that I was
overlooking. I am trying to find a generic way to get the same output on
newer versions of PostgreSQL.

Thanks,
Linda

> regards, tom lane
>

--
Linda Gray
Unitrends Corporation
803.454.0300 ext. 241


From: Michael Glaesemann <grzm(at)myrealbox(dot)com>
To: lgray(at)unitrends(dot)com
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, pgsql-general <pgsql-general(at)postgresql(dot)org>
Subject: Re: question about postgresql time intervals
Date: 2006-03-18 03:20:23
Message-ID: 31696DC2-927A-4846-9803-377306F4DA2D@myrealbox.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

I've been having some email problems, so my apologies if this is a
duplicate.

On Mar 16, 2006, at 22:49 , Linda wrote:

> Thanks for your reply. I guess you missed the original email. I
> have an
> application that is retrieving "uptime" (an integer number of
> seconds since
> reboot) and recasting it as varchar and then interval type.
>

Rather than perform this cast, you might want to make your own
function to handle this. Here are a couple (one in PL/pgSQL, the
other SQL). You should be able to use these functions any relatively
modern PostgreSQL installation.

(I find the x_int * interval some_int construct a bit cleaner than
forcing a cast as well.)

create or replace function secs_to_interval(integer)
returns interval
strict
immutable
language plpgsql as '
declare
secs alias for $1;
secs_per_day constant integer default 86400;
begin
return secs / secs_per_day * interval ''1 day'' + secs %
secs_per_day * interval ''1 second'';
end;
';

create or replace function secs_to_interval_sql(integer) returns
interval
strict
immutable
language sql as '
select $1 / 86400 * interval ''1 day'' + $1 % 86400 * interval ''1
second'';
';

test=# select secs_to_interval(1824459), secs_to_interval_sql(1824459);
secs_to_interval | secs_to_interval_sql
------------------+----------------------
21 days 02:47:39 | 21 days 02:47:39
(1 row)

test=# select secs_to_interval(86400), secs_to_interval_sql(86400);
secs_to_interval | secs_to_interval_sql
------------------+----------------------
1 day | 1 day
(1 row)

test=# select secs_to_interval(302), secs_to_interval_sql(302);
secs_to_interval | secs_to_interval_sql
------------------+----------------------
00:05:02 | 00:05:02
(1 row)

test=# select secs_to_interval(1824459 * 2), secs_to_interval_sql
(1824459 * 2);
secs_to_interval | secs_to_interval_sql
------------------+----------------------
42 days 05:35:18 | 42 days 05:35:18
(1 row)

Hope this helps.

Michael Glaesemann
grzm myrealbox com


From: Linda <lgray(at)unitrends(dot)com>
To: Michael Glaesemann <grzm(at)myrealbox(dot)com>
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, pgsql-general <pgsql-general(at)postgresql(dot)org>
Subject: Re: question about postgresql time intervals
Date: 2006-03-20 14:44:38
Message-ID: 200603200944.38496.lgray@unitrends.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

On Friday 17 March 2006 10:20 pm, Michael Glaesemann wrote:
> I've been having some email problems, so my apologies if this is a
> duplicate.
>
> On Mar 16, 2006, at 22:49 , Linda wrote:
>
>
> > Thanks for your reply. I guess you missed the original email. I
> > have an
> > application that is retrieving "uptime" (an integer number of
> > seconds since
> > reboot) and recasting it as varchar and then interval type.
> >
>
> Rather than perform this cast, you might want to make your own
> function to handle this. Here are a couple (one in PL/pgSQL, the
> other SQL). You should be able to use these functions any relatively
> modern PostgreSQL installation.
>
> (I find the x_int * interval some_int construct a bit cleaner than
> forcing a cast as well.)
>
> create or replace function secs_to_interval(integer)
> returns interval
> strict
> immutable
> language plpgsql as '
> declare
> secs alias for $1;
> secs_per_day constant integer default 86400;
> begin
> return secs / secs_per_day * interval ''1 day'' + secs %
> secs_per_day * interval ''1 second'';
> end;
> ';
>
> create or replace function secs_to_interval_sql(integer) returns
> interval
> strict
> immutable
> language sql as '
> select $1 / 86400 * interval ''1 day'' + $1 % 86400 * interval ''1
> second'';
> ';
>
> test=# select secs_to_interval(1824459), secs_to_interval_sql(1824459);
> secs_to_interval | secs_to_interval_sql
> ------------------+----------------------
> 21 days 02:47:39 | 21 days 02:47:39
> (1 row)
>
> test=# select secs_to_interval(86400), secs_to_interval_sql(86400);
> secs_to_interval | secs_to_interval_sql
> ------------------+----------------------
> 1 day | 1 day
> (1 row)
>
> test=# select secs_to_interval(302), secs_to_interval_sql(302);
> secs_to_interval | secs_to_interval_sql
> ------------------+----------------------
> 00:05:02 | 00:05:02
> (1 row)
>
> test=# select secs_to_interval(1824459 * 2), secs_to_interval_sql
> (1824459 * 2);
> secs_to_interval | secs_to_interval_sql
> ------------------+----------------------
> 42 days 05:35:18 | 42 days 05:35:18
> (1 row)
>
> Hope this helps.
>
> Michael Glaesemann
> grzm myrealbox com
>
>
>

Hi, Michael

Thanks for the suggestion! This approach will work on both older and newer
version of PostgreSQL.

Thanks,
Linda

--
Linda Gray
Unitrends Corporation
803.454.0300 ext. 241