Code

RRTimeslice: Updated output format to <range> #<seq>/<num>.
authorSebastian Harl <sh@tokkee.org>
Tue, 30 Oct 2012 06:55:42 +0000 (07:55 +0100)
committerSebastian Harl <sh@tokkee.org>
Tue, 30 Oct 2012 06:55:42 +0000 (07:55 +0100)
That is, format the timeslice in a range format (cf. tstzrange in PostgreSQL
9.2). I suppose this is easier to read for humans, even though it's a bit
longer. Also, include the number of timeslices in addition to the sequence
number as well. This way, we're able to unambiguously parse the output.

src/rrtimeslice.c

index 4f151d27b1b3f93b60164bc9759ddfb718396b46..6c135ec893ec865a6a18313d491e088c708dab71 100644 (file)
@@ -44,7 +44,7 @@
 #include <utils/array.h>
 #include <utils/datetime.h>
 #include <utils/timestamp.h>
-#include <miscadmin.h> /* DateStyle, IntervalStyle */
+#include <miscadmin.h> /* DateStyle */
 
 #ifdef HAVE_INT64_TIMESTAMP
 #      define TSTAMP_TO_INT64(t) (t)
@@ -337,8 +337,8 @@ rrtimeslice_out(PG_FUNCTION_ARGS)
        char *tz_str = NULL;
 
        char  ts_str[MAXDATELEN + 1];
-       char  buf_ts[MAXDATELEN + 1];
        char  buf_l[MAXDATELEN + 1];
+       char  buf_u[MAXDATELEN + 1];
        char *result;
 
        int32 len = 0;
@@ -353,35 +353,32 @@ rrtimeslice_out(PG_FUNCTION_ARGS)
        tslice = PG_GETARG_RRTIMESLICE_P(0);
 
        if (TIMESTAMP_NOT_FINITE(tslice->tstamp)
-                       || (timestamp2tm(tslice->tstamp, &tz, &tm, &fsec, &tz_str, NULL) != 0))
+                       || timestamp2tm(tslice->tstamp, &tz, &tm, &fsec, &tz_str, NULL))
                ereport(ERROR, (
                                        errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
                                        errmsg("invalid (non-finite) timestamp")
                                ));
-       else
-               EncodeDateTime(&tm, fsec, &tz, &tz_str, DateStyle, buf_ts);
 
-       if (! rrtimeslice_get_spec(tslice->tsid, &len, &num)) {
-               Interval interval;
+       EncodeDateTime(&tm, fsec, &tz, &tz_str, DateStyle, buf_u);
 
-               memset(&interval, 0, sizeof(interval));
-               interval.time = len * USECS_PER_SEC;
-               fsec = 0;
+       if (! rrtimeslice_get_spec(tslice->tsid, &len, &num)) {
+               TimestampTz lower = tslice->tstamp - (len * USECS_PER_SEC);
 
-               if (interval2tm(interval, &tm, &fsec))
+               if (timestamp2tm(lower, &tz, &tm, &fsec, &tz_str, NULL))
                        ereport(ERROR, (
-                                               errmsg("could not convert interval to tm")
+                                               errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
+                                               errmsg("invalid (non-finite) lower timestamp")
                                        ));
 
-               EncodeInterval(&tm, fsec, IntervalStyle, buf_l);
+               EncodeDateTime(&tm, fsec, &tz, &tz_str, DateStyle, buf_l);
        }
        else {
                strncpy(buf_l, "ERR", sizeof(buf_l));
                buf_l[sizeof(buf_l) - 1] = '\0';
        }
 
-       snprintf(ts_str, sizeof(ts_str), "%s -%s (#%i)",
-                       buf_ts, buf_l, tslice->seq);
+       snprintf(ts_str, sizeof(ts_str), "(%s, %s] #%i/%i",
+                       buf_l, buf_u, tslice->seq, num);
 
        result = pstrdup(ts_str);
        PG_RETURN_CSTRING(result);