Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1i8A1K-0000DO-4h for pgsql-general@arkaria.postgresql.org; Wed, 11 Sep 2019 21:19:22 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1i8A1J-00042h-2l for pgsql-general@arkaria.postgresql.org; Wed, 11 Sep 2019 21:19:21 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1i8A1I-00040Z-Ms for pgsql-general@lists.postgresql.org; Wed, 11 Sep 2019 21:19:20 +0000 Received: from out5-smtp.messagingengine.com ([66.111.4.29]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1i8A1E-00052B-Sm for pgsql-general@postgresql.org; Wed, 11 Sep 2019 21:19:20 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id 986632251F; Wed, 11 Sep 2019 17:19:13 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute6.internal (MEProxy); Wed, 11 Sep 2019 17:19:13 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= subject:to:cc:references:from:message-id:date:mime-version :in-reply-to:content-type:content-transfer-encoding; s=fm3; bh=5 StaRFVqYD3fIrFK1IrhPhEyKmUEYW2LVR9C5zeKVNM=; b=bMe1efOOWo1GsZ9lz Mks2Ai5EfaEy+r3MNBTOtQEE3paMiaAFLEjEEVWqasVK4oIGmmr6oFzlKSNIyJPi OeOwUXUIT7pl98iai50zT9hKHXEZ21C+DEqsIj5isBxHr5Hv/UQGJb33M1teS/8B 7M3Vuyxocevm4La3pDlacwopobB5vG35Kt1u6aY7VH0MdgPKtnw8Q40M1qJNOeyv GJsjDdNJPzzx+dAMibBaDj6ZxE6JtzxcEFxk9hBTw/B51xX60SAwCyHvafpXgCJO 4YJhtCiOMpyfDqaTZ2UvLwJ58CJh19ZRVFGzCv8+MuIp4RqrNmBvwfPglYJD2utu XRERA== 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=fm3; bh=5StaRFVqYD3fIrFK1IrhPhEyKmUEYW2LVR9C5zeKV NM=; b=nhnWUcak0NPtNlnk5QulZ2eWfU/cyEFI/V5yGbCpJyXdrNH55SQavjr+F E8fCBC6fIB2I6m+OhMhhZuGw6/DwIzUHywKyExnghXuGpm1+tOSVcLVz+YY8fuHG FKvn6sTwL1aw3lTeH2CBmLMLCNAhPjzAEb7uX4XGGlDYzYy1q7CsPG8JSDpc5hNV V/bsvJm8KsXrSVauCurIvliZvO9tYWMwDrV4Bm9kaMu93glsjL2dbJIdLGQf1BEL p+SbTY3yD/GEtSLkDsS/gUyWfyVIslKEU6Up937cZv18euJtmEhPoQ6AlxyBp+4o O+17NyijXGOvAyR0ZV2rJnlTQo1lA== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedufedrtdefgddufeegucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepuffvfhfhkffffgggjggtgfesthejredttdefjeenucfhrhhomheptegurhhi rghnucfmlhgrvhgvrhcuoegrughrihgrnhdrkhhlrghvvghrsegrkhhlrghvvghrrdgtoh hmqeenucfkphepudejgedrvddurdduvdelrddujeehnecurfgrrhgrmhepmhgrihhlfhhr ohhmpegrughrihgrnhdrkhhlrghvvghrsegrkhhlrghvvghrrdgtohhmnecuvehluhhsth gvrhfuihiivgeptd X-ME-Proxy: Received: from [192.168.1.3] (174-21-129-175.tukw.qwest.net [174.21.129.175]) by mail.messagingengine.com (Postfix) with ESMTPA id C5D9FD60057; Wed, 11 Sep 2019 17:19:12 -0400 (EDT) Subject: Re: How to reformat output of "age()" function To: Francisco Olarte , David Gauthier Cc: Postgres General References: From: Adrian Klaver Message-ID: Date: Wed, 11 Sep 2019 14:19:12 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.7.2 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 9/11/19 9:34 AM, Francisco Olarte wrote: > David: > > On Wed, Sep 11, 2019 at 5:39 PM David Gauthier wrote: >> How can I change the default output of the "age" function to be, for example, in minutes? >> E.g. >> dvdb=> select age('2019-09-11 09:00:00','2019-09-09 11:00:00'); >> age >> ---------------- >> 1 day 22:00:00 >> (1 row) >> I want the equivalent of that time delta in minutes. > > Some answers have already been posted, but also consider what you are > doing. Intervals have three components, months, days, seconds for a > reason, ( ignoring leap seconds for now ) not all days have 24h ( > daylight saving time changes ) and not all months have 28/29/30/31 > days. IIRC interval normalization for epoch assumes all months have 30 > days, all days have 24 hours. > > If you want to know the elapsed minutes between two timestamps, it > might be better to do it directly, extract the epoch from both ( > seconds ), substract, divide by 60 truncating/rounding if you need to. > > This is what happens in one case on my timezone ( Europe/Madrid ): > > > test=# select '2019.11.20 20:00:00'::timestamptz as end, '2019.06.10 > 10:00:00'::timestamptz as start; > end | start > ------------------------+------------------------ > 2019-11-20 20:00:00+01 | 2019-06-10 10:00:00+02 > (1 row) > > test=# select age('2019.11.20 20:00:00'::timestamptz, '2019.06.10 > 10:00:00'::timestamptz) as interval; > interval > ------------------------- > 5 mons 10 days 10:00:00 > (1 row) > > test=# select extract(epoch from age('2019.11.20 > 20:00:00'::timestamptz, '2019.06.10 10:00:00'::timestamptz)) as > interval_seconds; > interval_seconds > ------------------ > 13860000 > (1 row) > > test=# select extract(epoch from '2019.11.20 20:00:00'::timestamptz) - > extract(epoch from '2019.06.10 10:00:00'::timestamptz) as > elapsed_seconds; > elapsed_seconds > ----------------- > 14122800 > (1 row) Epoch is not the issue, age() is. Leave age() out of it: set timezone = 'Europe/Madrid'; test=# select extract(epoch from '2019.11.20 20:00:00'::timestamptz) - test-# extract(epoch from '2019.06.10 10:00:00'::timestamptz) as test-# elapsed_seconds; elapsed_seconds ----------------- 14122800 test=# select extract(epoch from '2019.11.20 20:00:00'::timestamptz -'2019.06.10 10:00:00'::timestamptz) as elapsed_seconds; elapsed_seconds ----------------- 14122800 (1 row) > > Francisco Olarte. > > > -- Adrian Klaver adrian.klaver@aklaver.com