Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1miUZq-0006Jq-VU for pgsql-general@arkaria.postgresql.org; Thu, 04 Nov 2021 04:42:15 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1miUZp-00041n-19 for pgsql-general@arkaria.postgresql.org; Thu, 04 Nov 2021 04:42:13 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1miUZn-00041c-JH for pgsql-general@lists.postgresql.org; Thu, 04 Nov 2021 04:42:12 +0000 Received: from out1-smtp.messagingengine.com ([66.111.4.25]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1miUZi-0000u3-W9 for pgsql-general@lists.postgresql.org; Thu, 04 Nov 2021 04:42:10 +0000 Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailout.nyi.internal (Postfix) with ESMTP id 7D2975C00B6; Thu, 4 Nov 2021 00:42:03 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute4.internal (MEProxy); Thu, 04 Nov 2021 00:42:03 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= message-id:date:mime-version:subject:to:cc:references:from :in-reply-to:content-type:content-transfer-encoding; s=fm1; bh=f 9wKcsbJqSF/tZ76H3OFscD+VCz0w8VyyXTLtCTMC9M=; b=HZWPSGdacbcI/7JKZ bQtCgOX/ysHAT5SCZ5aEeYAiEnkhamdM6AODTdbm3hppCfa6yRDBjnnw1Zu6vSfv yQf2+oMo+jPxX+6yf/NBqjH7LD3kfiKrooHn6JmQtEEA9FThczUsLUe1FWboh12G 0eGuv8vLVn05c71+dHEvRt5LJrXojqzLMBy1cswAtHk/sa9Vj9bFUQ8cDPjNRn97 AlJoN9/F4OdiA+nEmuelOzlJMSoFpsKLL/1m2Tr2l3BjyesYhZqS3LtBpb53tOMF +OnvSUHAi+CxkP+ZW7WJzzCycr5DGhd1BbXgN/wUdL7uOnWZeJC3pKplm1cTii97 gq1lg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm1; bh=f9wKcsbJqSF/tZ76H3OFscD+VCz0w8VyyXTLtCTMC 9M=; b=jZcnuPQZzrBcANLGTuYG32DEhWg4NPPhRVNYoU5WNG4YKIS+I959uGAt8 04qfUo6BOKMkOo7cXVeKEOY8Z8A/zDTCz8JtevfjPz+l4qBLTDiNudyzMZrCEgp5 7Apm7L5w+S55JoXQ+ZsLIT8ljXtlmxIvDD3ArbzuEChh/QZuBELb7ioInN2a3uNc UxeAaMx1SQ5zg7PUFKLZ9XWWBCTAB2YGSmrpC0zbCfrW+Qv/Ta2jaLjAmlVKoH4x s5DWZLMPyuL3rRbHtuhLmUeN0QzAEC20511y8RPby/kHqu11RpZCR3HzeLd9QUNH aeALl6Tb/3fdNldrlRqRaS0kY2k0g== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvuddrtdefgdejudcutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenuc fjughrpefkffggfgfuvfhfhfgjtgfgsehtkeertddtfeejnecuhfhrohhmpeetughrihgr nhcumfhlrghvvghruceorggurhhirghnrdhklhgrvhgvrhesrghklhgrvhgvrhdrtghomh eqnecuggftrfgrthhtvghrnhepudfgueelgeevfffhgeeljeehhfettdegieelgefgheef ffefveegjedtueetffdtnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrg hilhhfrhhomheprggurhhirghnrdhklhgrvhgvrhesrghklhgrvhgvrhdrtghomh X-ME-Proxy: Received: by mail.messagingengine.com (Postfix) with ESMTPA; Thu, 4 Nov 2021 00:42:02 -0400 (EDT) Message-ID: Date: Wed, 3 Nov 2021 21:42:01 -0700 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.2.1 Subject: Re: to_date() and to_timestamp() with negative years Content-Language: en-US To: Bryn Llewellyn Cc: Tom Lane PostgreSQL , pgsql-general list References: <138332.1635962086@sss.pgh.pa.us> <08E63AD5-8E6D-4140-AE1C-9DC22324A287@yugabyte.com> <72204b8f-41b2-0004-b09b-842f8c57477f@aklaver.com> <0B75398B-91CC-4074-B77A-390AA157685B@yugabyte.com> <265331.1635978234@sss.pgh.pa.us> <745E3531-6791-4964-AF68-BC82FC2EC864@yugabyte.com> <30ba14d1-31fc-31ac-aaff-ac42589d7a07@aklaver.com> <1EA95C37-F94C-4914-A7B1-E5C7FC65CA95@yugabyte.com> From: Adrian Klaver In-Reply-To: <1EA95C37-F94C-4914-A7B1-E5C7FC65CA95@yugabyte.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 11/3/21 19:12, Bryn Llewellyn wrote: >> /adrian.klaver@aklaver.com wrote:/ >> > Back to the point about separators, the "Current" doc has this bullet: > > « > A separator (a space or non-letter/non-digit character) in the template > string of to_timestamp and to_date matches any single separator in > the input string or is skipped, unless the FX option is used... > » > > (There's no such explanation in the Version 11 doc—but never mind that.) > I read this to mean that a space IS a viable separator. And yet Tom's > "nope, the space doesn't count [as a separator]" claims the opposite. > The bullet's wording, by the way, says that the minus sign is a > separator. But yet it can also be taken to confer the meaning "BC" to a > year. No wonder I'm confused. > > Elsewhere the "Current" doc says that runs of two or more spaces have > the same effect as a single space (in the absence of FX or FM complexity). > > No wonder that examples like I showed sometimes produce the wrong > results, even after more than one round of tinkering with the C > implementation. Try this test (the to-be-converted text has runs of five > spaces, and the template has runs of ten spaces): It comes down to determining what is a separator and what is the negative sign. > > select to_date('     1950     02     14', 'YYYY          MM          DD'); > select to_date('     -1950     02     14', 'YYYY          MM          DD'); The row above returns: select to_date(' -1950 02 14', 'YYYY MM DD'); to_date --------------- 1950-02-14 BC As you state below. Change it to: select to_date(' -1950 02 14', ' YYYY MM DD'); to_date ------------ 1950-02-14 and you don't get the BC as -1950 is taken as separator(-)1950 not negative(-)1950. > > select to_date('     14     02     1950', 'DD          MM          YYYY'); > select to_date('     14     02     -1950', 'DD          MM          YYYY'); The above returns: select to_date(' 14 02 -1950', 'DD MM YYYY'); to_date ------------ 1950-02-14 Change it to: select to_date(' 14 02 -1950', 'DD MMYYYY'); to_date --------------- 1950-02-14 BC and you get BC as -1950 is taken as negative(-)1950 not separator(-)1950. Not sure how this can be handled in a totally predictable way given the unpredictable ways in which datetime strings are formatted? The only thing I can say it is it points out that when working with datetimes settling on a standard format is your best defense against unpredictable results. > > "-1950" is taken as "BC" in the second to_date() but it is not so taken > in the fourth to_date(). > -- Adrian Klaver adrian.klaver@aklaver.com