Fix_Century_V1_PG.patch
text/x-patch
Filename: Fix_Century_V1_PG.patch
Type: text/x-patch
Part: 0
Message:
Re: Rectifying wrong Date outputs
Patch
Same data as JSON:
GET /api/v1/attachments/:id/patch
the parsed metadata as JSON — format, series position, per-file stats; never the diff bytes.
API reference →
Format: unified
| File | + | − |
|---|---|---|
| src/backend/utils/adt/formatting.c | 65 | 21 |
diff --git a/src/backend/utils/adt/formatting.c b/src/backend/utils/adt/formatting.c
index aba1145..ad42126 100644
--- a/src/backend/utils/adt/formatting.c
+++ b/src/backend/utils/adt/formatting.c
@@ -989,6 +989,7 @@ static DCHCacheEntry *DCH_cache_getnew(char *str);
static NUMCacheEntry *NUM_cache_search(char *str);
static NUMCacheEntry *NUM_cache_getnew(char *str);
static void NUM_cache_remove(NUMCacheEntry *ent);
+static int DecideCentury(int *in, int len);
/* ----------
@@ -2733,21 +2734,14 @@ DCH_from_char(FormatNode *node, char *in, TmFromChar *out)
case DCH_IYYY:
from_char_parse_int(&out->year, &s, n);
out->yysz = 4;
+ DecideCentury(&out->year, out->yysz);
s += SKIP_THth(n->suffix);
break;
case DCH_YYY:
case DCH_IYY:
from_char_parse_int(&out->year, &s, n);
out->yysz = 3;
-
- /*
- * 3-digit year: '100' ... '999' = 1100 ... 1999 '000' ...
- * '099' = 2000 ... 2099
- */
- if (out->year >= 100)
- out->year += 1000;
- else
- out->year += 2000;
+ DecideCentury(&out->year, out->yysz);
s += SKIP_THth(n->suffix);
break;
case DCH_YY:
@@ -2755,14 +2749,7 @@ DCH_from_char(FormatNode *node, char *in, TmFromChar *out)
from_char_parse_int(&out->year, &s, n);
out->yysz = 2;
- /*
- * 2-digit year: '00' ... '69' = 2000 ... 2069 '70' ... '99'
- * = 1970 ... 1999
- */
- if (out->year < 70)
- out->year += 2000;
- else
- out->year += 1900;
+ DecideCentury(&out->year, out->yysz);
s += SKIP_THth(n->suffix);
break;
case DCH_Y:
@@ -2770,10 +2757,7 @@ DCH_from_char(FormatNode *node, char *in, TmFromChar *out)
from_char_parse_int(&out->year, &s, n);
out->yysz = 1;
- /*
- * 1-digit year: always +2000
- */
- out->year += 2000;
+ DecideCentury(&out->year, out->yysz);
s += SKIP_THth(n->suffix);
break;
case DCH_RM:
@@ -5161,3 +5145,63 @@ float8_to_char(PG_FUNCTION_ARGS)
NUM_TOCHAR_finish;
PG_RETURN_TEXT_P(result);
}
+
+/*
+ * In case of different Inputs & different Year formats, engine itself
+ * has to decide the century for the given year. This function will take
+ * care of the input & accordingly append the century their if not provided.
+ *
+ * Input = Transformed output
+ * 0 ... 9 = 2000 ... 2009
+ * 00 ... 69 = 2000 ... 2069
+ * 70 ... 99 = 1900 ... 1999
+ * 000 ... 999 = 2000 ... 2999
+ *
+ * In case input is greater than 9999 it throws an error. The basic rule of
+ * the function is " If the format is lesser than the actual value, then it
+ * should throw an error. Only in case of 'YY', we need not throw an error,
+ * unless the year is later than 9999".
+ */
+
+static int DecideCentury(int *in, int len)
+{
+ if (len == 1)
+ {
+ if (*in > 0 && *in < 9)
+ *in += 2000;
+ else
+ ereport(ERROR,
+ (errcode(ERRCODE_INVALID_DATETIME_FORMAT),
+ errmsg("date format picture ends before converting entire input string %d ", len)));
+ }
+ else if (len == 2)
+ {
+ if (*in > 0 && *in < 69)
+ *in += 2000;
+ else if (*in > 70 && *in < 99)
+ *in += 1900;
+ else if (*in > 9999)
+ ereport(ERROR,
+ (errcode(ERRCODE_INVALID_DATETIME_FORMAT),
+ errmsg("date format picture ends before converting entire input string %d ", len)));
+
+ }
+ else if (len == 3)
+ {
+ if (*in > 0 && *in < 999)
+ *in += 2000;
+ else
+ ereport(ERROR,
+ (errcode(ERRCODE_INVALID_DATETIME_FORMAT),
+ errmsg("date format picture ends before converting entire input string %d ", len)));
+ }
+ else if (len == 4)
+ {
+ if (*in > 9999)
+ ereport(ERROR,
+ (errcode(ERRCODE_INVALID_DATETIME_FORMAT),
+ errmsg("date format picture ends before converting entire input string %d ", len)));
+ }
+
+ return 0;
+}