PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
compat.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * compat.c
4  * Reimplementations of various backend functions.
5  *
6  * Portions Copyright (c) 2013-2017, PostgreSQL Global Development Group
7  *
8  * IDENTIFICATION
9  * src/bin/pg_waldump/compat.c
10  *
11  * This file contains client-side implementations for various backend
12  * functions that the rm_desc functions in *desc.c files rely on.
13  *
14  *-------------------------------------------------------------------------
15  */
16 
17 /* ugly hack, same as in e.g pg_controldata */
18 #define FRONTEND 1
19 #include "postgres.h"
20 
21 #include <time.h>
22 
23 #include "utils/datetime.h"
24 #include "lib/stringinfo.h"
25 
26 /* copied from timestamp.c */
29 {
30  pg_time_t result;
31 
32 #ifdef HAVE_INT64_TIMESTAMP
33  result = (pg_time_t) (t / USECS_PER_SEC +
35 #else
36  result = (pg_time_t) (t +
38 #endif
39  return result;
40 }
41 
42 /*
43  * Stopgap implementation of timestamptz_to_str that doesn't depend on backend
44  * infrastructure. This will work for timestamps that are within the range
45  * of the platform time_t type. (pg_time_t is compatible except for possibly
46  * being wider.)
47  *
48  * XXX the return value points to a static buffer, so beware of using more
49  * than one result value concurrently.
50  *
51  * XXX: The backend timestamp infrastructure should instead be split out and
52  * moved into src/common. That's a large project though.
53  */
54 const char *
56 {
57  static char buf[MAXDATELEN + 1];
58  char ts[MAXDATELEN + 1];
59  char zone[MAXDATELEN + 1];
60  time_t result = (time_t) timestamptz_to_time_t(dt);
61  struct tm *ltime = localtime(&result);
62 
63  strftime(ts, sizeof(ts), "%Y-%m-%d %H:%M:%S", ltime);
64  strftime(zone, sizeof(zone), "%Z", ltime);
65 
66 #ifdef HAVE_INT64_TIMESTAMP
67  sprintf(buf, "%s.%06d %s", ts, (int) (dt % USECS_PER_SEC), zone);
68 #else
69  sprintf(buf, "%s.%.6f %s", ts, fabs(dt - floor(dt)), zone);
70 #endif
71 
72  return buf;
73 }
74 
75 /*
76  * Provide a hacked up compat layer for StringInfos so xlog desc functions can
77  * be linked/called.
78  */
79 void
80 appendStringInfo(StringInfo str, const char *fmt,...)
81 {
82  va_list args;
83 
84  va_start(args, fmt);
85  vprintf(fmt, args);
86  va_end(args);
87 }
88 
89 void
90 appendStringInfoString(StringInfo str, const char *string)
91 {
92  appendStringInfo(str, "%s", string);
93 }
94 
95 void
97 {
98  appendStringInfo(str, "%c", ch);
99 }
#define MAXDATELEN
Definition: datetime.h:203
int64 pg_time_t
Definition: pgtime.h:23
#define USECS_PER_SEC
Definition: timestamp.h:106
static struct pg_tm tm
Definition: localtime.c:103
double TimestampTz
Definition: timestamp.h:51
#define SECS_PER_DAY
Definition: timestamp.h:98
static char * buf
Definition: pg_test_fsync.c:65
const char * timestamptz_to_str(TimestampTz dt)
Definition: compat.c:55
Definition: zic.c:91
pg_time_t timestamptz_to_time_t(TimestampTz t)
Definition: compat.c:28
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: compat.c:80
#define UNIX_EPOCH_JDATE
Definition: timestamp.h:184
#define POSTGRES_EPOCH_JDATE
Definition: timestamp.h:185
void appendStringInfoString(StringInfo str, const char *string)
Definition: compat.c:90
void appendStringInfoChar(StringInfo str, char ch)
Definition: compat.c:96