pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Problem in age on a dates interval
6+ messages / 4 participants
[nested] [flat]

* Problem in age on a dates interval
@ 2004-07-16 08:45  Luis Sousa <llsousa@ualg.pt>
  0 siblings, 1 reply; 6+ messages in thread

From: Luis Sousa @ 2004-07-16 08:45 UTC (permalink / raw)
  To: pgsql-sql

Hi all,

I'm using PostgreSQL 7.3.3 on i386-pc-linux-gnu, compiled by GCC gcc 
(GCC) 3.3 (Debian), and I don't understand the results of the following 
queries:


SELECT age('2004-05-14 16:00'::timestamp,'2004-02-18 16:00'::timestamp);
      age      
----------------
 2 mons 25 days

SELECT '2004-02-18 16:00'::timestamp+'2 mons 25 days'::interval;
      ?column?      
---------------------
 2004-05-13 16:00:00

In this case, the age from 2004-05-14 16:00 to 2004-02-18 16:00 is 2 
mons 25 days, but if I add the age to the initial date, it returns one 
day less!?



SELECT age('2004-05-26 16:00'::timestamp,'2004-02-18 16:00'::timestamp);
      age     
---------------
 3 mons 8 days

SELECT '2004-02-18 16:00'::timestamp+'3 mons 8 days'::interval;
      ?column?      
---------------------
 2004-05-26 16:00:00

Here, the age between 2004-05-26 16:00 and 2004-02-18 16:00 is 3 mons 8 
days, and this interval added to the initial date gives the correct result!!

Best regards,
Luis Sousa



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

* Re: Problem in age on a dates interval
@ 2004-07-16 11:52  Theodore Petrosky <tedpet5@yahoo.com>
  parent: Luis Sousa <llsousa@ualg.pt>
  0 siblings, 1 reply; 6+ messages in thread

From: Theodore Petrosky @ 2004-07-16 11:52 UTC (permalink / raw)
  To: Luis Sousa <llsousa@ualg.pt>; pgsql-sql

Luis,

wow.... at first I thought I had my head around a leap
year problem so I advanced your query a year....

testbed=# SELECT age('2005-05-14
16:00'::timestamp,'2005-02-18 16:00'::timestamp);
      age       
----------------
 2 mons 24 days
(1 row)

testbed =# SELECT '2005-02-18 16:00'::timestamp +'2
mons 24 days'::interval;
      ?column?       
---------------------
 2005-05-12 16:00:00
(1 row)

I just thought I would let you know it can get
worse..... :) I don't know how daylight savings time
is playing this problem... but I didn't expect the
problem to grow like that.

Ted

--- Luis Sousa <llsousa@ualg.pt> wrote:
> Hi all,
> 
> I'm using PostgreSQL 7.3.3 on i386-pc-linux-gnu,
> compiled by GCC gcc 
> (GCC) 3.3 (Debian), and I don't understand the
> results of the following 
> queries:
> 
> 
> SELECT age('2004-05-14 16:00'::timestamp,'2004-02-18
> 16:00'::timestamp);
>       age      
> ----------------
>  2 mons 25 days
> 
> SELECT '2004-02-18 16:00'::timestamp+'2 mons 25
> days'::interval;
>       ?column?      
> ---------------------
>  2004-05-13 16:00:00
> 
> In this case, the age from 2004-05-14 16:00 to
> 2004-02-18 16:00 is 2 
> mons 25 days, but if I add the age to the initial
> date, it returns one 
> day less!?
> 
> SELECT age('2004-05-26 16:00'::timestamp,'2004-02-18
> 16:00'::timestamp);
>       age     
> ---------------
>  3 mons 8 days
> 
> SELECT '2004-02-18 16:00'::timestamp+'3 mons 8
> days'::interval;
>       ?column?      
> ---------------------
>  2004-05-26 16:00:00
> 
> Here, the age between 2004-05-26 16:00 and
> 2004-02-18 16:00 is 3 mons 8 
> days, and this interval added to the initial date
> gives the correct result!!
> 



		
__________________________________
Do you Yahoo!?
Yahoo! Mail - 50x more storage than other providers!
http://promotions.yahoo.com/new_mail



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

* Re: Problem in age on a dates interval
@ 2004-07-16 14:34  Tom Lane <tgl@sss.pgh.pa.us>
  parent: Theodore Petrosky <tedpet5@yahoo.com>
  0 siblings, 1 reply; 6+ messages in thread

From: Tom Lane @ 2004-07-16 14:34 UTC (permalink / raw)
  To: Theodore Petrosky <tedpet5@yahoo.com>; +Cc: Luis Sousa <llsousa@ualg.pt>; pgsql-sql

Theodore Petrosky <tedpet5@yahoo.com> writes:
> wow.... at first I thought I had my head around a leap
> year problem so I advanced your query a year....

I think what's going on here is a difference of interpretation about
whether an "M months D days" interval means to add the months first
or the days first.  For instance

2005-02-18 plus 2 months = 2005-04-18, plus 24 days = 2005-05-12

2005-02-18 plus 24 days = 2005-03-14, plus 2 months = 2005-05-14

The timestamp-plus-interval operator is evidently doing addition the
first way, but it looks like age() is calculating the difference in a
way that implicitly corresponds to the second way.

I have some vague recollection that this has come up before, but
I don't recall whether we concluded that age() needs to be changed
or not.  In any case it's not risen to the top of anyone's to-do list,
because I see that age() still acts this way in CVS tip.

			regards, tom lane



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

* Re: Problem in age on a dates interval
@ 2004-07-19 09:00  Luis Sousa <llsousa@ualg.pt>
  parent: Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 1 reply; 6+ messages in thread

From: Luis Sousa @ 2004-07-19 09:00 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Theodore Petrosky <tedpet5@yahoo.com>; pgsql-sql

I worked around this problem returning the difference between the two 
dates, using extract doy from both.
Anyway, this will cause a bug on my code when changing the year. Any ideas?

Best regards,
Luis Sousa

Tom Lane wrote:

>Theodore Petrosky <tedpet5@yahoo.com> writes:
>  
>
>>wow.... at first I thought I had my head around a leap
>>year problem so I advanced your query a year....
>>    
>>
>
>I think what's going on here is a difference of interpretation about
>whether an "M months D days" interval means to add the months first
>or the days first.  For instance
>
>2005-02-18 plus 2 months = 2005-04-18, plus 24 days = 2005-05-12
>
>2005-02-18 plus 24 days = 2005-03-14, plus 2 months = 2005-05-14
>
>The timestamp-plus-interval operator is evidently doing addition the
>first way, but it looks like age() is calculating the difference in a
>way that implicitly corresponds to the second way.
>
>I have some vague recollection that this has come up before, but
>I don't recall whether we concluded that age() needs to be changed
>or not.  In any case it's not risen to the top of anyone's to-do list,
>because I see that age() still acts this way in CVS tip.
>
>			regards, tom lane
>
>
>  
>



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

* Re: Problem in age on a dates interval
@ 2004-07-19 10:46  Alexander M. Pravking <fduch@antar.bryansk.ru>
  parent: Luis Sousa <llsousa@ualg.pt>
  0 siblings, 1 reply; 6+ messages in thread

From: Alexander M. Pravking @ 2004-07-19 10:46 UTC (permalink / raw)
  To: Luis Sousa <llsousa@ualg.pt>; +Cc: Tom Lane <tgl@sss.pgh.pa.us>; Theodore Petrosky <tedpet5@yahoo.com>; pgsql-sql

On Mon, Jul 19, 2004 at 10:00:50AM +0100, Luis Sousa wrote:
> I worked around this problem returning the difference between the two 
> dates, using extract doy from both.
> Anyway, this will cause a bug on my code when changing the year. Any ideas?

Why don't you use the minus operator?

SELECT '2004-05-14 16:00'::timestamp - '2004-02-18 16:00'::timestamp;
 ?column?
----------
 86 days

Or, if you need the age just in days:

SELECT extract(day from '2004-05-14 16:00'::timestamp - '2004-02-18 16:00'::timestamp);
 date_part
-----------
        86

or

SELECT '2004-05-14 16:00'::date - '2004-02-18 16:00'::date;
 ?column?
----------
       86

Note that '2004-05-14 16:00'::date is actually '2004-05-14 00:00'::date,
so the last two are not always equal.


> Tom Lane wrote:
> 
> >Theodore Petrosky <tedpet5@yahoo.com> writes:
> > 
> >
> >>wow.... at first I thought I had my head around a leap
> >>year problem so I advanced your query a year....
> >>   
> >>
> >
> >I think what's going on here is a difference of interpretation about
> >whether an "M months D days" interval means to add the months first
> >or the days first.  For instance
> >
> >2005-02-18 plus 2 months = 2005-04-18, plus 24 days = 2005-05-12
> >
> >2005-02-18 plus 24 days = 2005-03-14, plus 2 months = 2005-05-14
> >
> >The timestamp-plus-interval operator is evidently doing addition the
> >first way, but it looks like age() is calculating the difference in a
> >way that implicitly corresponds to the second way.
> >
> >I have some vague recollection that this has come up before, but
> >I don't recall whether we concluded that age() needs to be changed
> >or not.  In any case it's not risen to the top of anyone's to-do list,
> >because I see that age() still acts this way in CVS tip.
> >
> >			regards, tom lane

-- 
Fduch M. Pravking



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

* Re: Problem in age on a dates interval
@ 2004-07-20 08:17  Luis Sousa <llsousa@ualg.pt>
  parent: Alexander M. Pravking <fduch@antar.bryansk.ru>
  0 siblings, 0 replies; 6+ messages in thread

From: Luis Sousa @ 2004-07-20 08:17 UTC (permalink / raw)
  To: Alexander M. Pravking <fduch@antar.bryansk.ru>; +Cc: Tom Lane <tgl@sss.pgh.pa.us>; Theodore Petrosky <tedpet5@yahoo.com>; pgsql-sql

Yes, that's a much more clever solution than the one I used.
Thanks

Best regards,
Luis Sousa

Alexander M. Pravking wrote:

>On Mon, Jul 19, 2004 at 10:00:50AM +0100, Luis Sousa wrote:
>  
>
>>I worked around this problem returning the difference between the two 
>>dates, using extract doy from both.
>>Anyway, this will cause a bug on my code when changing the year. Any ideas?
>>    
>>
>
>Why don't you use the minus operator?
>
>SELECT '2004-05-14 16:00'::timestamp - '2004-02-18 16:00'::timestamp;
> ?column?
>----------
> 86 days
>
>Or, if you need the age just in days:
>
>SELECT extract(day from '2004-05-14 16:00'::timestamp - '2004-02-18 16:00'::timestamp);
> date_part
>-----------
>        86
>
>or
>
>SELECT '2004-05-14 16:00'::date - '2004-02-18 16:00'::date;
> ?column?
>----------
>       86
>
>Note that '2004-05-14 16:00'::date is actually '2004-05-14 00:00'::date,
>so the last two are not always equal.
>
>
>  
>
>>Tom Lane wrote:
>>
>>    
>>
>>>Theodore Petrosky <tedpet5@yahoo.com> writes:
>>>
>>>
>>>      
>>>
>>>>wow.... at first I thought I had my head around a leap
>>>>year problem so I advanced your query a year....
>>>>  
>>>>
>>>>        
>>>>
>>>I think what's going on here is a difference of interpretation about
>>>whether an "M months D days" interval means to add the months first
>>>or the days first.  For instance
>>>
>>>2005-02-18 plus 2 months = 2005-04-18, plus 24 days = 2005-05-12
>>>
>>>2005-02-18 plus 24 days = 2005-03-14, plus 2 months = 2005-05-14
>>>
>>>The timestamp-plus-interval operator is evidently doing addition the
>>>first way, but it looks like age() is calculating the difference in a
>>>way that implicitly corresponds to the second way.
>>>
>>>I have some vague recollection that this has come up before, but
>>>I don't recall whether we concluded that age() needs to be changed
>>>or not.  In any case it's not risen to the top of anyone's to-do list,
>>>because I see that age() still acts this way in CVS tip.
>>>
>>>			regards, tom lane
>>>      
>>>
>
>  
>



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


end of thread, other threads:[~2004-07-20 08:17 UTC | newest]

Thread overview: 6+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2004-07-16 08:45 Problem in age on a dates interval Luis Sousa <llsousa@ualg.pt>
2004-07-16 11:52 ` Theodore Petrosky <tedpet5@yahoo.com>
2004-07-16 14:34   ` Tom Lane <tgl@sss.pgh.pa.us>
2004-07-19 09:00     ` Luis Sousa <llsousa@ualg.pt>
2004-07-19 10:46       ` Alexander M. Pravking <fduch@antar.bryansk.ru>
2004-07-20 08:17         ` Luis Sousa <llsousa@ualg.pt>

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