Re: unix timestamp

From: "Hugh Mandeville" <hughmandeville(at)hotmail(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: unix timestamp
Date: 2001-08-16 21:01:00
Message-ID: 9lhc98$24s3$1@news.tht.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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));

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2001-08-16 21:51:24 Re: Killing inactive connections
Previous Message Dr. Evil 2001-08-16 20:41:10 Re: Storing images in PG?