I was playing with TO_TIMESTAMP() and I noticed a weird result:
postgres=# select to_timestamp('20170-07-24 21:59:57.12345678', 'yyyy-mm-dd
Even though the "us" token is supposed to be restricted to 000000-999999 it
looks like the microseconds was calculated as 12.345678.
Digging into the code, I found inconsistencies in
from_char_parse_int_len(). From formatting.c:
! * Read a single integer from the source string, into the int pointed
! * 'dest'. If 'dest' is NULL, the result is discarded.
! * In fixed-width mode (the node does not have the FM suffix), consume
! * 'len' characters. However, any leading whitespace isn't counted in
! static int
! from_char_parse_int_len(int *dest, char **src, const int len,
! if (S_FM(node->suffix) || is_next_separator(node))
! * This node is in Fill Mode, or the next node is known to be a
! * non-digit value, so we just slurp as many characters as we
! errno = 0;
! result = strtol(init, src, 10);
! * We need to pull exactly the number of characters given in
! * of the string, and convert those.
! char *last;
! if (used < len)
So the function prologue disagrees with the code. In the first condition
strtol() can consume more than 'len' digits. In the else, we error out if
we don't have exactly 'len' characters.
What's the proper behaviour here?