Re: Determining period between 2 dates

From: Marti Raudsepp <marti(at)juffo(dot)org>
To: Thom Brown <thom(at)linux(dot)com>
Cc: Robert Haas <robertmhaas(at)gmail(dot)com>, PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Determining period between 2 dates
Date: 2011-02-16 17:07:13
Message-ID: AANLkTi=c4VB38DkyiC4VQfA9Ak+Gyy+EJNOe1eHjOFg-@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Wed, Feb 16, 2011 at 18:03, Thom Brown <thom(at)linux(dot)com> wrote:
> For the number of fortnights, that becomes:
>
> select extract(epoch from now() - '2010-01-01 11:45:13'::timestamp)/60/60/24/14;
>
> You'd think with PostgreSQL having such a rich type system, it
> wouldn't need to come to that.  It's just asking for the number of
> intervals between 2 timestamps rather than the number of seconds and
> dividing it to the point you get your answer.

I think a good generic solution would be an interval/interval operator
that returns numeric. Then the above becomes:

SELECT (now() - timestamp '2010-01-01 11:45:13') / interval '2 weeks';

However, looking at the code, it's not so obvious what to do if the
intervals contain months.

Regards,
Marti

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Alvaro Herrera 2011-02-16 17:08:02 pgsql: Cleanup ClusterInfo initialization in pg_upgrade
Previous Message Kevin Grittner 2011-02-16 16:36:16 Re: Determining period between 2 dates