Thread: date conversions

date conversions

From
Thad Humphries
Date:
I'm porting some dynamic SQL code from another databases and need to
know how PostgreSQL handles this.  I have looked at utils/date.h,
utils/datetime.h, and utils/timestamp.h but I don't understand which to
use or how.  Can anyone help?

My database used 'timestamp without time zone'.  My application's format
(how date-time will be passed in and out) is

typedef struct {
short    year;
short    month;
short    day;
short    hour;
short    minute;
short    second;
} DateStamp;

In Informix, for example, to convert between DateStamp and Informix
dtime_t I use (less input/error checking):

void
ds2dt( ds, dt )
DateStamp    *ds;
dtime_t        *dt;
{
char        buf[26];

sprintf( buf, "%4.4hd-%2.2hd-%2.2hd %2.2hd:%2.2hd:%2.2hd",
    ds->year, ds->month, ds->day,
    ds->hour, ds->minute, ds->second );

dtcvasc( buf, dt );
}

/* and */

void
dt2ds( dt, ds )
dtime_t        *dt;
DateStamp    *ds;
{
char        buf[26];

memset( (char *) ds, 0, sizeof *ds );

sscanf( buf, "%4hd-%2hd-%2hd %2hd:%2hd:%2hd",
    &ds->year,
    &ds->month,
    &ds->day,
    &ds->hour,
    &ds->minute,
    &ds->second );
}

What's the way to do this in PostgreSQL?
--
------------------------------------------------------------------------
Thad Humphries              "...no religious test shall ever be required
Web Development Manager      as a qualification to any office or public
Phone: 540/675-3015, x225    trust under the United States." -Article VI