Re: unix timestamp - Mailing list pgsql-general

From Hugh Mandeville
Subject Re: unix timestamp
Date
Msg-id 9lhc98$24s3$1@news.tht.net
Whole thread Raw
In response to unix timestamp  ("Ben-Nes Nimrod" <nimrod@canaan.co.il>)
List pgsql-general
> how can i use unix timestamp as a data type?

the datetime data type should work.  you can find info about it at

  http://www.postgresql.org/idocs/index.php?functions-datetime.html
  http://www.postgresql.org/idocs/index.php?datatype-datetime.html

here is some example code

CREATE TABLE test (
    id    integer   PRIMARY KEY,
    mytime datetime
);


#include <time.h>
...
time_t mytime = time (NULL);
printf ("time to be inserted into database: %s\n", ctime (&mytime));

/* insert row setting the time.  if you want to insert the current time you
can pass 'now' to a datetime field */
sprintf (sql_str, "INSERT INTO test (id, mytime) VALUES (1, '%s')", ctime
(&mytime));
res = PQexec(dbconn, sql_str);
...

/* get row extracting the time since the epoch */
sprintf (sql_str, "SELECT id, extract (epoch from mytime) FROM test WHERE
oid = %d", PQoidValue(res));
...
mytime = atoi(PQgetvalue(res, 0, 1));

printf ("time retrieved from database: %s\n", ctime(&mytime));




pgsql-general by date:

Previous
From: "Nilesh Kumar Jain"
Date:
Subject: Querry optimisation in Postgres SQL
Next
From: Mark McWilliams
Date:
Subject: How do you recover a postgres db?