Header And Logo

PostgreSQL
| The world's most advanced open source database.

compat.c

Go to the documentation of this file.
00001 /*-------------------------------------------------------------------------
00002  *
00003  * compat.c
00004  *      Reimplementations of various backend functions.
00005  *
00006  * Portions Copyright (c) 2013, PostgreSQL Global Development Group
00007  *
00008  * IDENTIFICATION
00009  *      contrib/pg_xlogdump/compat.c
00010  *
00011  * This file contains client-side implementations for various backend
00012  * functions that the rm_desc functions in *desc.c files rely on.
00013  *
00014  *-------------------------------------------------------------------------
00015  */
00016 
00017 /* ugly hack, same as in e.g pg_controldata */
00018 #define FRONTEND 1
00019 #include "postgres.h"
00020 
00021 #include <time.h>
00022 
00023 #include "utils/datetime.h"
00024 #include "lib/stringinfo.h"
00025 
00026 /* copied from timestamp.c */
00027 pg_time_t
00028 timestamptz_to_time_t(TimestampTz t)
00029 {
00030     pg_time_t   result;
00031 
00032 #ifdef HAVE_INT64_TIMESTAMP
00033     result = (pg_time_t) (t / USECS_PER_SEC +
00034                  ((POSTGRES_EPOCH_JDATE - UNIX_EPOCH_JDATE) * SECS_PER_DAY));
00035 #else
00036     result = (pg_time_t) (t +
00037                  ((POSTGRES_EPOCH_JDATE - UNIX_EPOCH_JDATE) * SECS_PER_DAY));
00038 #endif
00039     return result;
00040 }
00041 
00042 /*
00043  * Stopgap implementation of timestamptz_to_str that doesn't depend on backend
00044  * infrastructure.  This will work for timestamps that are within the range
00045  * of the platform time_t type.  (pg_time_t is compatible except for possibly
00046  * being wider.)
00047  *
00048  * XXX the return value points to a static buffer, so beware of using more
00049  * than one result value concurrently.
00050  *
00051  * XXX: The backend timestamp infrastructure should instead be split out and
00052  * moved into src/common.  That's a large project though.
00053  */
00054 const char *
00055 timestamptz_to_str(TimestampTz dt)
00056 {
00057     static char buf[MAXDATELEN + 1];
00058     char        ts[MAXDATELEN + 1];
00059     char        zone[MAXDATELEN + 1];
00060     time_t      result = (time_t) timestamptz_to_time_t(dt);
00061     struct tm  *ltime = localtime(&result);
00062 
00063     strftime(ts, sizeof(ts), "%Y-%m-%d %H:%M:%S", ltime);
00064     strftime(zone, sizeof(zone), "%Z", ltime);
00065 
00066 #ifdef HAVE_INT64_TIMESTAMP
00067     sprintf(buf, "%s.%06d %s", ts, (int) (dt % USECS_PER_SEC), zone);
00068 #else
00069     sprintf(buf, "%s.%.6f %s", ts, fabs(dt - floor(dt)), zone);
00070 #endif
00071 
00072     return buf;
00073 }
00074 
00075 /*
00076  * Provide a hacked up compat layer for StringInfos so xlog desc functions can
00077  * be linked/called.
00078  */
00079 void
00080 appendStringInfo(StringInfo str, const char *fmt, ...)
00081 {
00082     va_list     args;
00083 
00084     va_start(args, fmt);
00085     vprintf(fmt, args);
00086     va_end(args);
00087 }
00088 
00089 void
00090 appendStringInfoString(StringInfo str, const char *string)
00091 {
00092     appendStringInfo(str, "%s", string);
00093 }
00094 
00095 void
00096 appendStringInfoChar(StringInfo str, char ch)
00097 {
00098     appendStringInfo(str, "%c", ch);
00099 }