Porting from mysql to psql (UNIX_TIMESTAMP()?)

Started by Zlatko Calusicover 25 years ago4 messagesgeneral
Jump to latest
#1Zlatko Calusic
zlatko@iskon.hr

Hi!

As subject says, we are currently porting all of our data, programs
and logic from mysql to postgresql. One of the things we have yet to
resolve is how to replace mysql's UNIX_TIMESTAMP() function we used
extensively in PosgreSQL?

Function works like this in mysql:

mysql> select start from connection limit 1;
+---------------------+
| start |
+---------------------+
| 2000-07-03 20:12:37 |
+---------------------+
1 row in set (0.01 sec)

mysql> select UNIX_TIMESTAMP(start) from connection limit 1;
+-----------------------+
| UNIX_TIMESTAMP(start) |
+-----------------------+
| 962647957 |
+-----------------------+
1 row in set (0.00 sec)

Is there any similar functionality (returning unixish number of
seconds since 1970 from the timestamp field) in PostgreSQL?

I tried all of the available date/time functions, type casting but all
to no avail.

TIA,
--
Zlatko

P.S Is it bad manners crossposting to two pgsql mailing list? Still
new to PostgreSQL, still learning...

#2Rommel B. Abaya
rommel.abaya@ramcargroup.com
In reply to: Zlatko Calusic (#1)
Re: Porting from mysql to psql (UNIX_TIMESTAMP()?)

i thick it's epoch() in PostgreSQL....check your documentation.

Zlatko Calusic wrote:

Show quoted text

Hi!

As subject says, we are currently porting all of our data, programs
and logic from mysql to postgresql. One of the things we have yet to
resolve is how to replace mysql's UNIX_TIMESTAMP() function we used
extensively in PosgreSQL?

Function works like this in mysql:

mysql> select start from connection limit 1;
+---------------------+
| start |
+---------------------+
| 2000-07-03 20:12:37 |
+---------------------+
1 row in set (0.01 sec)

mysql> select UNIX_TIMESTAMP(start) from connection limit 1;
+-----------------------+
| UNIX_TIMESTAMP(start) |
+-----------------------+
| 962647957 |
+-----------------------+
1 row in set (0.00 sec)

Is there any similar functionality (returning unixish number of
seconds since 1970 from the timestamp field) in PostgreSQL?

I tried all of the available date/time functions, type casting but all
to no avail.

TIA,
--
Zlatko

P.S Is it bad manners crossposting to two pgsql mailing list? Still
new to PostgreSQL, still learning...

#3Zlatko Calusic
zlatko@iskon.hr
In reply to: Zlatko Calusic (#1)
Re: Porting from mysql to psql (UNIX_TIMESTAMP()?)

Zlatko Calusic <zlatko@iskon.hr> writes:

Is there any similar functionality (returning unixish number of
seconds since 1970 from the timestamp field) in PostgreSQL?

It's amazing that I spent something like an hour trying to find an
answer, and only ten minutes waiting for the answer to it. :)

Mario Weilguni came up with a date_part('epoch', start) solution, and
that is exactly what I was looking for.

Thanks Mario!
--
Zlatko

#4Tom Lane
tgl@sss.pgh.pa.us
In reply to: Zlatko Calusic (#1)
Re: [SQL] Porting from mysql to psql (UNIX_TIMESTAMP()?)

Zlatko Calusic <zlatko@iskon.hr> writes:

Is there any similar functionality (returning unixish number of
seconds since 1970 from the timestamp field) in PostgreSQL?

Sure. You can use date_part, or cast to abstime and thence to integer:

regression=# select now();
now
------------------------
2000-09-09 12:55:50-04
(1 row)

regression=# select date_part('epoch',now());
date_part
-----------
968518563
(1 row)

regression=# select now()::abstime::int4;
?column?
-----------
968518585
(1 row)

To go the other way (integer seconds to timestamp), use the cast
method in reverse:

regression=# select 968518585 :: int4 :: abstime :: timestamp;
?column?
------------------------
2000-09-09 12:56:25-04
(1 row)

(there's probably a cleaner way to do this, but that works ...)

regards, tom lane