Re: now() vs 'epoch'::timestamp

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: James Cloos <cloos(at)jhcloos(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: now() vs 'epoch'::timestamp
Date: 2015-04-01 19:17:59
Message-ID: 26071.1427915879@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

James Cloos <cloos(at)jhcloos(dot)com> writes:
> I've for some time used:
> (now()::timestamp without time zone - 'epoch'::timestamp without time zone)::reltime::integer

> to get the current seconds since the epoch. The results are consistant
> with date +%s.

> (Incidently, is there a better way in 9.4?)

> But I found the 'epoch'::timestamp + $THAT_VALUE::reltime was off.

> I consitantly get 1970-01-01 06:00 plus a fraction of a second from:

> select now() - ((now()::timestamp without time zone - 'epoch'::timestamp without time zone)::reltime::integer)::reltime;

"reltime" doesn't have fractional-second precision, so you lose whatever
part of the original timestamp difference was fractional.

"reltime" is deprecated too, and will go away someday (probably long
before this calculation starts to overflow an int, in 2038), so you
really don't want to be using it.

regards, tom lane

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Bill Moran 2015-04-01 19:36:09 Re: Would like to know how analyze works technically
Previous Message Stephen Frost 2015-04-01 18:59:10 Re: now() vs 'epoch'::timestamp