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 1kMgEp-000692-3C for pgsql-general@arkaria.postgresql.org; Sun, 27 Sep 2020 23:37:52 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kMgEo-0007os-13 for pgsql-general@arkaria.postgresql.org; Sun, 27 Sep 2020 23:37:50 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kMgEn-0007ol-Fr for pgsql-general@lists.postgresql.org; Sun, 27 Sep 2020 23:37:49 +0000 Received: from sonic306-22.consmr.mail.ne1.yahoo.com ([66.163.189.84]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1kMgEi-0006BI-Eg for pgsql-general@lists.postgresql.org; Sun, 27 Sep 2020 23:37:48 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com; s=s2048; t=1601249862; bh=fKXihFVD/6JgwtKIdZ53xAwY4ZqKrVEDQ6ru1/g1dfc=; h=Subject:From:In-Reply-To:Date:Cc:References:To:From:Subject; b=Z9zIEPEEvqGVEgdHk5C5RZk/ufEIpZwLhN4bskOkLmLROpw34SQ57IsA5/KhT5uy+9HuE1hleH3ynyKSRmBo+GfFR4CfKa8yHEfVs4CobU66JAU3bJbNCgQv7Jc6ZvBD/3JGZyAIzbXXsDJuczy3gpcrtyUa7Z2Zgtw8kOZ4QUlfrVVK86UCMB94ple9tWUWnpkM/CbHM0QNmK54ZM4GsKvJ4PrAYv5VI5ES05yI6JUHOm3xmbSKzfxeGvPIsnb0ATvVC3S8dBI9F6rfbVQSfAPho/atnd74yFhdVLm/8RGX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yu.JYxOVT7yjbrCvnLShu8yfs1elUJt5zQQ9aawXpGb3JW3DOSGdfEf8k0RqvDGLm2llFP250L2G bUhbrdpJgClWguysJgO9ByUlNTnLqzIfCbaEfo1XgLh_bmSpY6.elAk7VLFR7huoClhxghAtiCDg XV7FJXOkj2HkR62rVnExZVdUu3so5C1I6Hlnq7Z8vtCtelDZTMiXiw4oe86FQjgIeG5RSoorHpbh 1V2rm4blJBx0IWr.ggt8SmMD3ciOxh1ChYVJ_Ww_IHLyD0ZGuY.CRks7ZjqhkESYC_frVa55z5Rc uL3O7dDZGH2HNe68bBmoYj.mPMIc9Bsp9PTioyImpHgS2moR7Z4_pxSH6rMYM7aemGMtvsNlQpD. GD6m7k3l6lshtoWTLYbiw.QTLE_rA9J1jaIiAG1X7NRjplyLgp4Rz1MSxe7f69znF3U5LXkwEl9a jgbRkEmKyT2zw6B3oi7p_JmXyVmeEhqCEKSwq4.njjK7wIbAJKte_ok0esWjrDTAWXH1nQsx4Aqx r Received: from sonic.gate.mail.ne1.yahoo.com by sonic306.consmr.mail.ne1.yahoo.com with HTTP; Sun, 27 Sep 2020 23:37:42 +0000 Received: by smtp422.mail.ne1.yahoo.com (VZM Hermes SMTP Server) with ESMTPA ID 471857dfbcc6fcfecc3b89c027851c95; Sun, 27 Sep 2020 23:37:41 +0000 (UTC) Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (1.0) Subject: Re: Returning timestamp with timezone at specified timezone irrespective of client timezone From: Greg Smith X-Mailer: iPhone Mail (16G102) In-Reply-To: <239b4c15-7c0f-4e8c-947f-38195ff56a1e@www.fastmail.com> Date: Sun, 27 Sep 2020 18:37:39 -0500 Cc: pgsql-general@lists.postgresql.org Content-Transfer-Encoding: quoted-printable Message-Id: References: <3af9fdd2-b622-426a-8e1e-aeab1bee8493@www.fastmail.com> <0c452adb-9047-09ec-7086-e804bf381c69@aklaver.com> <239b4c15-7c0f-4e8c-947f-38195ff56a1e@www.fastmail.com> To: "aNullValue (Drew Stemen)" Content-Length: 4440 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Is it really a requirement to hold the datetime in the database actually in t= he specified time zone ? Usual practice is to hold UTC only and convert when= necessary to user-configured (or specified) or column-specified time zone p= erhaps only when transferring to/from the db or when otherwise necessary. An= y time zones that have daylight savings will also have a problem when calcul= ating datetime differences when crossing the daylight savings boundary. UTC d= oesn=E2=80=99t have this problem.=20 Can you refactor to only store UTC and the desired time zone, then convert t= o that time zone when needed? Also, what programming language outside of SQL are you using (if any)? Greg S. > On Sep 27, 2020, at 5:39 PM, aNullValue (Drew Stemen) wrote: >=20 > At 2020-09-27T18:31:49-04:00, Adrian Klaver se= nt: >>> On 9/27/20 2:16 PM, aNullValue (Drew Stemen) wrote: >>> Hello, >>>=20 >>> I've attempted to obtain help with this problem from several other=20 >>> places, but numerous individuals recommended I ask this mailing list. >>>=20 >>> What I need is for the ability to return a timestamp with timezone,=20 >>> using the UTC offset that corresponds to a column-defined timezone,=20 >>> irrespective of the client/session configured timezone. >>>=20 >>> I have three columns in a table: >>> Timezone: 'US/Eastern' >>> Date: 2020-10-31 >>> Time: 08:00 >>>=20 >>> The output I'm able to find includes these possibilities: >>> '2020-10-31 08:00:00' >>> '2020-10-31 12:00:00+00' >>>=20 >>> Whereas what I actually need is: >>> '2020-10-31 08:00:00-05' >>>=20 >>> Using the postgresql session-level timezone configuration won't work=20 >>> because I need multiple timezones to be handled in a single set. >>>=20 >>> Example code follows. I'm not using to_char in the examples as I likely=20= >>> would in the production code, but I haven't found any way that it could=20= >>> be helpful here regardless. >>>=20 >>> --------------------------------------------------- >>>=20 >>> SET TIME ZONE 'UTC'; >>>=20 >>> CREATE TABLE loc >>> ( >>> id serial not null, >>> timezone text not null, >>> loc_date date NOT NULL, >>> loc_time text NOT NULL, >>> CONSTRAINT loc_pkey PRIMARY KEY (id), >>> CONSTRAINT loc_loc_time_check CHECK (loc_time ~=20 >>> '(^(2[0-3]|[01][0-9]|[0-9]):?([0-5][0-9]):?([0-5][0-9])?$)|(^(1[012]|0[1= -9]|[1-9]):[0-5][0-9]=20 >>> [AaPp][Mm]$)'::text) >>> ) >>> ; >>>=20 >>> INSERT INTO loc (timezone, loc_date, loc_time) VALUES >>> ('US/Eastern', '2020-10-31', '08:00'), >>> ('US/Eastern', '2020-11-03', '08:00'), >>> ('US/Central', '2020-10-31', '08:00'), >>> ('US/Central', '2020-11-03', '08:00'); >>>=20 >>> SELECT * >>> , timezone(l.timezone, l.loc_date + l.loc_time::time without time zone)=20= >>> tswtz >>> , (l.loc_date + l.loc_time::time without time zone) tswotz >>> FROM loc l >>> ORDER BY timezone, loc_date, loc_time >>> ; >>>=20 >>> --------------------------------------------------- >>>=20 >>> id | timezone | loc_date | loc_time | tswtz | = =20 >>> tswotz >>> ----+------------+------------+----------+------------------------+-----= ---------------- >>> 7 | US/Central | 2020-10-31 | 08:00 | 2020-10-31 13:00:00+00 |=20 >>> 2020-10-31 08:00:00 >>> 8 | US/Central | 2020-11-03 | 08:00 | 2020-11-03 14:00:00+00 |=20 >>> 2020-11-03 08:00:00 >>> 5 | US/Eastern | 2020-10-31 | 08:00 | 2020-10-31 12:00:00+00 |=20 >>> 2020-10-31 08:00:00 >>> 6 | US/Eastern | 2020-11-03 | 08:00 | 2020-11-03 13:00:00+00 |=20 >>> 2020-11-03 08:00:00 >>> (4 rows) >>>=20 >>> What I actually need is, in example id=3D7, '2020-10-31 08:00:00-05'. >>>=20 >>> Is this even possible? Several people have proposed that I write a=20 >>> custom function to do this on a per-row basis, which... I suppose I can=20= >>> do... I'm just blown away that this isn't something that just works "out= =20 >>> of the box". >>>=20 >>=20 >> Something like?: >>=20 >> select '2020-10-31' || ' 08:00 ' || utc_offset from pg_timezone_names=20 >> where name =3D 'US/Eastern'; >> ?column? >> ---------------------------- >> 2020-10-31 08:00 -04:00:00 >>=20 >>=20 >> --=20 >> Adrian Klaver >> adrian.klaver@aklaver.com >>=20 >=20 > The problem there is that the value of utc_offset in pg_timezone_names is c= orrect only as of the current point in time, and not as of the date/time val= ues in the row. >=20 >=20