Converting seconds past midnight to a time

Started by Karl O. Pincover 20 years ago3 messagesgeneral
Jump to latest
#1Karl O. Pinc
kop@meme.com

Hi,

What is the best way to convert an integer number of
seconds past midnight into a time? I can't seem to
figure out a way that does not involve casts to strings,
which seems wasteful.

On a related note is there some reason why
interval + int
does not result in the interval plus int number
of seconds? That would seem the sane approach.
So sane that there must be some reason why it's
not already done.

Karl <kop@meme.com>
Free Software: "You don't pay back, you pay forward."
-- Robert A. Heinlein

#2Tom Lane
tgl@sss.pgh.pa.us
In reply to: Karl O. Pinc (#1)
Re: Converting seconds past midnight to a time

"Karl O. Pinc" <kop@meme.com> writes:

What is the best way to convert an integer number of
seconds past midnight into a time?

Intermediate 'interval' value seems to work:

regression=# select (99.44 * '1 second'::interval)::time;
time
-------------
00:01:39.44
(1 row)

On a related note is there some reason why
interval + int
does not result in the interval plus int number
of seconds?

Again, multiplying the number by '1 sec'::interval would get you there.

regards, tom lane

#3Bruce Momjian
bruce@momjian.us
In reply to: Tom Lane (#2)
Re: [GENERAL] Converting seconds past midnight to a time

Tom Lane wrote:

"Karl O. Pinc" <kop@meme.com> writes:

What is the best way to convert an integer number of
seconds past midnight into a time?

Intermediate 'interval' value seems to work:

regression=# select (99.44 * '1 second'::interval)::time;
time
-------------
00:01:39.44
(1 row)

On a related note is there some reason why
interval + int
does not result in the interval plus int number
of seconds?

Again, multiplying the number by '1 sec'::interval would get you there.

I have applied the following documentation patch to HEAD and 8.1.X.
It is unified diff because those are easier with single-line changes
like this, where the surrounding text isn't as significant.

I also fixed an example '1 day' - '1 hour' which now computes
differently, and added trailing zero seconds, which is our display
default.

-- 
  Bruce Momjian                        |  http://candle.pha.pa.us
  pgman@candle.pha.pa.us               |  (610) 359-1001
  +  If your life is a hard drive,     |  13 Roberts Road
  +  Christ can be your backup.        |  Newtown Square, Pennsylvania 19073

Attachments:

/rtmp/difftext/plainDownload+21-15