time to integer

Started by Soma Interestingabout 25 years ago5 messagesgeneral
Jump to latest
#1Soma Interesting
dfunct@telus.net

I'm trying to write a function with pl/pgsql that takes member_id (int4)
and adds this to now() converted to seconds. Is there an existing function
to do the conversion of a timestamp to seconds?

#2Tom Lane
tgl@sss.pgh.pa.us
In reply to: Soma Interesting (#1)
Re: time to integer

Soma Interesting <dfunct@telus.net> writes:

I'm trying to write a function with pl/pgsql that takes member_id (int4)
and adds this to now() converted to seconds. Is there an existing function
to do the conversion of a timestamp to seconds?

date_part('epoch', timestamp) produces a Unix-style seconds count.

regards, tom lane

#3Peter Eisentraut
peter_e@gmx.net
In reply to: Soma Interesting (#1)
Re: time to integer

Soma Interesting writes:

I'm trying to write a function with pl/pgsql that takes member_id (int4)
and adds this to now() converted to seconds. Is there an existing function
to do the conversion of a timestamp to seconds?

date_part('epoch', value) /* seconds since 1970 */

--
Peter Eisentraut peter_e@gmx.net http://yi.org/peter-e/

#4Soma Interesting
dfunct@telus.net
In reply to: Tom Lane (#2)
Re: time to integer

At 02:27 PM 3/6/2001 -0500, you wrote:

date_part('epoch', timestamp) produces a Unix-style seconds count.

regards, tom lane

Ok, thanks I misunderstood the directions and thought I had to use epoch
with the timestamp function somehow :)

this is my trigger and function:

CREATE FUNCTION oned_member_num() RETURNS OPAQUE AS '
BEGIN
NEW.member_num := NEW.id + date_part('epoch', timestamp
'now');
RETURN new;
END;'
LANGUAGE 'plpgsql';

CREATE TRIGGER oned_member_num
BEFORE INSERT
ON members
FOR EACH ROW
EXECUTE PROCEDURE oned_member_num();

I'm not 100% sure that a pl/pgsql function is the best/only solution here.
Is it overkill, or my only option?

#5Soma Interesting
dfunct@telus.net
In reply to: Soma Interesting (#4)
Fwd: Re: time to integer

I should have mentioned that NEW.id is set to default to nextval('id_seq').

Show quoted text

this is my trigger and function:

CREATE FUNCTION oned_member_num() RETURNS OPAQUE AS '
BEGIN
NEW.member_num := NEW.id + date_part('epoch', timestamp
'now');
RETURN new;
END;'
LANGUAGE 'plpgsql';

CREATE TRIGGER oned_member_num
BEFORE INSERT
ON members
FOR EACH ROW
EXECUTE PROCEDURE oned_member_num();

I'm not 100% sure that a pl/pgsql function is the best/only solution here.
Is it overkill, or my only option?