Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1bF12X-0003VM-Dx for pgsql-hackers@arkaria.postgresql.org; Mon, 20 Jun 2016 15:23:05 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1bF12W-0006o7-Vu for pgsql-hackers@arkaria.postgresql.org; Mon, 20 Jun 2016 15:23:05 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1bF12W-0006o1-De for pgsql-hackers@postgresql.org; Mon, 20 Jun 2016 15:23:04 +0000 Received: from newmail.postgrespro.ru ([93.174.131.138] helo=mail.postgrespro.ru) by makus.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1bF12T-0008UU-CP for pgsql-hackers@postgresql.org; Mon, 20 Jun 2016 15:23:03 +0000 Received: from localhost (localhost [127.0.0.1]) by mail.postgrespro.ru (Postfix) with ESMTP id A939621C8EC6; Mon, 20 Jun 2016 18:22:59 +0300 (MSK) X-Virus-Scanned: Debian amavisd-new at postgrespro.ru Received: from mail.postgrespro.ru ([127.0.0.1]) by localhost (mail.postgrespro.ru [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id Yo1PZacVaxQW; Mon, 20 Jun 2016 18:22:58 +0300 (MSK) Received: from [192.168.27.30] (unknown [192.168.27.1]) by mail.postgrespro.ru (Postfix) with ESMTPSA id 2C0BC21C8EB9; Mon, 20 Jun 2016 18:22:58 +0300 (MSK) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=postgrespro.ru; s=mail; t=1466436178; bh=dNvTS4zZWbndmOmlF4D6X1bXZURvQdaHlade02abj/M=; h=Subject:To:References:From:Date:In-Reply-To; b=KBmQCtpwKWcPJlsHjMRBqkXetWWeBzMTjEvsPUxZoQlCrW5vugUqFv3Yl5e9Z7G64 Z0G5xQA8crywfJCnmT2YKjWKP0J2uL520yc5mW3Zz0eB9J2KCfC6kIf3eZQ1J7ridy v6/7z4qFhE2yM9c+le1rgR4zmKESmopL08kttDmg= Subject: Re: Bug in to_timestamp(). To: amul sul , PostgreSQL-development References: <1873520224.1784572.1465833145330.JavaMail.yahoo.ref@mail.yahoo.com> <1873520224.1784572.1465833145330.JavaMail.yahoo@mail.yahoo.com> From: Alex Ignatov Message-ID: Date: Mon, 20 Jun 2016 18:22:59 +0300 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:45.0) Gecko/20100101 Thunderbird/45.1.1 MIME-Version: 1.0 In-Reply-To: <1873520224.1784572.1465833145330.JavaMail.yahoo@mail.yahoo.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit X-Pg-Spam-Score: -3.4 (---) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-hackers Precedence: bulk Sender: pgsql-hackers-owner@postgresql.org On 13.06.2016 18:52, amul sul wrote: > Hi, > > It's look like bug in to_timestamp() function when format string has more whitespaces compare to input string, see below: > > Ex.1: Two white spaces before HH24 whereas one before input time string > > postgres=# SELECT TO_TIMESTAMP('2016-06-13 15:43:36', 'YYYY/MM/DD HH24:MI:SS'); > to_timestamp > ------------------------ > 2016-06-13 05:43:36-07 <— incorrect time > (1 row) > > > > Ex.2: One whitespace before YYYY format string > > postgres=# SELECT TO_TIMESTAMP('2016/06/13 15:43:36', ' YYYY/MM/DD HH24:MI:SS'); > to_timestamp > ------------------------------ > 0016-06-13 15:43:36-07:52:58 <— incorrect year > (1 row) > > > > If there are one or more consecutive whitespace in the format, we should skip those as long as we could get an actual field. > Thoughts? > Thanks & Regards, > Amul Sul > > From docs about to_timestamp() ( https://www.postgresql.org/docs/9.5/static/functions-formatting.html) "These functions interpret input liberally, with minimal error checking. While they produce valid output, the conversion can yield unexpected results. For example, input to these functions is not restricted by normal ranges, thus to_date('20096040','YYYYMMDD') returns 2014-01-17 rather than causing an error. Casting does not have this behavior." And it wont stop on some simple whitespace. By using to_timestamp you can get any output results by providing illegal input parameters values: postgres=# SELECT TO_TIMESTAMP('2016-06-13 99:99:99', 'YYYYMMDD HH24:MI:SS'); to_timestamp ------------------------ 2016-01-06 14:40:39+03 (1 row) Alex Ignatov Postgres Professional: http://www.postgrespro.com The Russian Postgres Company -- Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-hackers