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 1i8Q03-0003wC-1P for pgsql-general@arkaria.postgresql.org; Thu, 12 Sep 2019 14:23:07 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1i8Q01-0004TS-SE for pgsql-general@arkaria.postgresql.org; Thu, 12 Sep 2019 14:23:05 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1i8Q00-0004TF-62 for pgsql-general@lists.postgresql.org; Thu, 12 Sep 2019 14:23:05 +0000 Received: from wout3-smtp.messagingengine.com ([64.147.123.19]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.92) (envelope-from ) id 1i8Pzw-0005ZJ-Tm for pgsql-general@postgresql.org; Thu, 12 Sep 2019 14:23:03 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.west.internal (Postfix) with ESMTP id 395D263F; Thu, 12 Sep 2019 10:22:59 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute6.internal (MEProxy); Thu, 12 Sep 2019 10:22:59 -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 pTOEctLN4sWC8xiFqj/PcouVd4n/Omn5Gr465p2hvw=; b=QWsFnGWaDbwXhqzCg 0qE5LvXvP1CRW6Bu+9AZ8/0RWRHLe5dKMb06urYhdvqaw0njndkq1rYi/xz1Q8X+ jRnwukMzXdgOInf48IHu2B+acSLTNOrEQsc2qO7Tgy+6kwxd8Da/lcj6fayvbuht 7BfmNFC3LkTuor7FI8UuiG9+HBWHR9pDi/tsiXYA3sa0uoclumdMOr/GvaZX8lrp Dvj32BguI1y7wfa+VsbqgZmBCsjZqMCtJRMML8PMqigzEFinl1B/LlJDzSWi8wdo ZlUOvnYre/+RRv6hF6sH2qOkHi5UxdTC5UeDamv3Jfi63xAySzvEesUkPBygPrks K+MQw== 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=5pTOEctLN4sWC8xiFqj/PcouVd4n/Omn5Gr465p2h vw=; b=TdzY1BoZH6wBw13Y+J2fK5GyOrXnT24VcLoOOr41vRxKbdz/OEB6i2G7+ yMmMMBUKnbxZQ75fKiwRGH9pwc1XYw4jKWdWnmCPpIP3ow8K6S9ko1+0P3lIKXoY JauDGsu+MIXEDU2aE52dva/euBXKf0+73HwBtHm59OTne3zZlLA9+845G6hrUhyy C+DgyYNN5zWByy6x0Jw4M0WI2t/8bs24R5XYM8d3dcJv2aSQZJjp/o7yoNHDR51p 5nh2cuDcoCC7Yt2DzeFEZ/c5mowIbUnfPPIZ+oS215lT8/E9AqbiZEMfAr9UI5cj fu4gRZwQaYimeJHg+m5gXysS3MHwA== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedufedrtdehgdejgecutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenuc fjughrpefuvfhfhffkffgfgggjtgfgsehtkeertddtfeejnecuhfhrohhmpeetughrihgr nhcumfhlrghvvghruceorggurhhirghnrdhklhgrvhgvrhesrghklhgrvhgvrhdrtghomh eqnecuffhomhgrihhnpehpohhsthhgrhgvshhqlhdrohhrghenucfkphepudejgedrvddu rdduvdelrddujeehnecurfgrrhgrmhepmhgrihhlfhhrohhmpegrughrihgrnhdrkhhlrg hvvghrsegrkhhlrghvvghrrdgtohhmnecuvehluhhsthgvrhfuihiivgeptd 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 CEAA4D60057; Thu, 12 Sep 2019 10:22:57 -0400 (EDT) Subject: Re: How to reformat output of "age()" function To: Francisco Olarte Cc: David Gauthier , Postgres General References: From: Adrian Klaver Message-ID: Date: Thu, 12 Sep 2019 07:22:57 -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: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 9/12/19 4:50 AM, Francisco Olarte wrote: > Adrian: > > On Wed, Sep 11, 2019 at 11:19 PM Adrian Klaver > wrote: >> On 9/11/19 9:34 AM, Francisco Olarte wrote: > ... >>> 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. > .... >>> 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: > > Well, I did not point to age as the problem assuming it was clear in > the examples I gave. Two usages of epoch, one with other without age. > >> 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) > > Ah, I see your point now. The problem is age and substraction gives > different invervals. > >> cdrs=# select '2019.11.20 20:00:00'::timestamptz -'2019.06.10 10:00:00'::timestamptz as diff_interval; > diff_interval > ------------------- > 163 days 11:00:00 > (1 row) > > cdrs=# select age('2019.11.20 20:00:00'::timestamptz, '2019.06.10 > 10:00:00'::timestamptz) as age_interval; > age_interval > ------------------------- > 5 mons 10 days 10:00:00 > (1 row) > > And I see it still translate it to normalized days. > > I still prefer substracting epoch values, as the 163d/11h seems really > tricky to work with, and it took me a time to parse & diff your > queries against mines, they all looked the same. It seems like PG does Because they are the same: https://www.postgresql.org/docs/11/functions-datetime.html "Subtraction of dates and timestamps can also be complex. One conceptually simple way to perform subtraction is to convert each value to a number of seconds using EXTRACT(EPOCH FROM ...), then subtract the results; this produces the number of seconds between the two values. This will adjust for the number of days in each month, timezone changes, and daylight saving time adjustments. Subtraction of date or timestamp values with the “-” operator returns the number of days (24-hours) and hours/minutes/seconds between the values, making the same adjustments. The age function returns years, months, days, and hours/minutes/seconds, performing field-by-field subtraction and then adjusting for negative field values. The following queries illustrate the differences in these approaches. The sample results were produced with timezone = 'US/Eastern'; there is a daylight saving time change between the two dates used: SELECT EXTRACT(EPOCH FROM timestamptz '2013-07-01 12:00:00') - EXTRACT(EPOCH FROM timestamptz '2013-03-01 12:00:00'); Result: 10537200 SELECT (EXTRACT(EPOCH FROM timestamptz '2013-07-01 12:00:00') - EXTRACT(EPOCH FROM timestamptz '2013-03-01 12:00:00')) / 60 / 60 / 24; Result: 121.958333333333 SELECT timestamptz '2013-07-01 12:00:00' - timestamptz '2013-03-01 12:00:00'; Result: 121 days 23:00:00 SELECT age(timestamptz '2013-07-01 12:00:00', timestamptz '2013-03-01 12:00:00'); Result: 4 mons " > date substraction by substracting epochs and converting to a > normalized interval, I prefer to go to number of seconds directly when > working with "elapsed time"as otherwise I get confused. I mean > something like this ( using it just to show the differences ): > > cdrs=# select (extract(epoch from '2019.11.20 20:00:00'::timestamptz) > - extract(epoch from '2019.06.10 10:00:00'::timestamptz)) * '1 > second'::interval as pure_seconds_interval; > pure_seconds_interval > ----------------------- > 3923:00:00 > (1 row) > > A third representation! which gives the same result for epoch, but I'm > not sure it does for arithmetic....( tested it, it does not ) > > I thought substraction would give me that, clearly it does not ( both > give the same when using epoch, as lacking tz info it has to assume > something, and it seems to assume no dst changes ). See doc information above. It uses the SET timezone. See below for more information: https://www.postgresql.org/docs/11/datatype-datetime.html#DATATYPE-TIMEZONES It does deal with DST changes. See the example above. > > timestamp / interval arithmetic is really a hairy thing. ( As shown > below, start point carefully taken to avoid crossing dsts ) > > > cdrs=# select x, '2019.11.20 20:00:00'::timestamptz + x::interval as > sum from ( values ('5 mons 10 days 10:00:00'),('163 days > 11:00:00'),('3923:00:00')) as v(x); > x | sum > -------------------------+------------------------ > 5 mons 10 days 10:00:00 | 2020-05-01 06:00:00+02 > 163 days 11:00:00 | 2020-05-02 07:00:00+02 > 3923:00:00 | 2020-05-02 08:00:00+02 > (3 rows) > > > Francisco Olarte. > -- Adrian Klaver adrian.klaver@aklaver.com