pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Adrian Klaver <adrian.klaver@aklaver.com>
To: David Gauthier <davegauthierpg@gmail.com>
Cc: pgsql-general <pgsql-general@postgresql.org>
Subject: Re: timestamp (military) at time zone without the suffix
Date: Wed, 11 Jul 2018 14:31:52 -0700
Message-ID: <652f88fe-af0f-4f9c-0ef1-ee814b8a05ac@aklaver.com> (raw)
In-Reply-To: <CAMBRECCbS0RmW24yzAssx54oPT+AgL8bOe0SSdZ0MNX2OyhhwA@mail.gmail.com>
References: <CAMBRECB=k1CkGjvY-nn=WMtcVFM8f_88D6MK+Bt=jCbHVOpbWA@mail.gmail.com>
	<CAKFQuwa1UR9pAr+ZSo=C5FUAMdv0yNd432eA0EQF5cJ8i7RQaQ@mail.gmail.com>
	<CAMBRECBnkee2fG3kWFCFFRfk=Eq40obKkzOL1181H_phZhRtRA@mail.gmail.com>
	<CAKFQuwY8Z-vnPMRncLu+V+byjVi7a3pHs1p=0kjQQ74cmBmsgg@mail.gmail.com>
	<CAMBRECBdRyvbFZQFEMx0QqgWsduJV+vG3xJ+6FZTB2CexscYiQ@mail.gmail.com>
	<84450581-6f59-cf88-ee2c-eaa03f9bba87@aklaver.com>
	<CAMBRECCbS0RmW24yzAssx54oPT+AgL8bOe0SSdZ0MNX2OyhhwA@mail.gmail.com>

On 07/11/2018 02:21 PM, David Gauthier wrote:

Pleas reply to list also.
Ccing list.

> Table columns have already been defined with timestamp datatype.  The 
> on;y way I know of to fix this is to...
> 
> 1) add a new column as timestamptz called 'tmp' (whatever)
> 2) update tmp with the value in the timestamp collumn perhaps using "at 
> time zone 'utc' "
> 3) drop the original timestamp column
> 4) recreate the column with the same name but with data type timestamptz
> 5) Move all the records over to this column (from tmp)
> 6) drop the tmp column.
> 
> There would be some needed downtime to do this of course.
> 
> Is there an easier way?

create table ts_test(id int, ts_fld timestamp);

insert into ts_test values (1, now()), (2, now() - interval '1 day');

test_(aklaver)> select * from ts_test ;
  id |           ts_fld
----+----------------------------
   1 | 2018-07-11 14:24:43.960989
   2 | 2018-07-10 14:24:43.960989
(2 rows)

Assuming the timestamp values where at UTC:

alter table ts_test alter COLUMN ts_fld type timestamptz using ts_fld at 
time zone 'UTC';

test_(aklaver)> select * from ts_test ; 
 

  id |            ts_fld 
 
 

----+------------------------------- 
 
 

   1 | 2018-07-11 07:28:17.279899-07 
 
 

   2 | 2018-07-10 07:28:17.279899-07

The above depends on you knowing what the timestamps in the timestamp 
field where entered as. I would test first.

See below for more info:

https://www.postgresql.org/docs/10/static/sql-altertable.html

> 
> On Wed, Jul 11, 2018 at 5:14 PM, Adrian Klaver 
> <adrian.klaver@aklaver.com <mailto:adrian.klaver@aklaver.com>> wrote:
> 
>     On 07/11/2018 01:34 PM, David Gauthier wrote:
> 
>         Thanks Everyone, they all work, but TL's seems to be the simplest...
>         select current_timestamp(0) at time zone 'utc'
> 
>         I'm kinda stuck with the timestamp data type (vs timestamptz). 
>         Wondering if I can stick with that.
> 
> 
>     The above is at little unclear. Can you change the data type or not?
>     If you can your life will be a lot easier if you change it to
>     timestamptz.
> 
> 
>         One last question...
>         I want to store the current UTC date/time in the DB.  Does PG
>         unconditionally store something like UTC, then let the queries
>         figure out how they want to look at it (with "at time zone" and
>         "to_char()" etc...) ?  Or do I have to intentionally store the
>         UTC value somehow?
> 
> 
>     Per:
> 
>     https://www.postgresql.org/docs/10/static/datatype-datetime.html
>     <https://www.postgresql.org/docs/10/static/datatype-datetime.html;
> 
>     "For timestamp with time zone, the internally stored value is always
>     in UTC (Universal Coordinated Time, traditionally known as Greenwich
>     Mean Time, GMT). An input value that has an explicit time zone
>     specified is converted to UTC using the appropriate offset for that
>     time zone. If no time zone is stated in the input string, then it is
>     assumed to be in the time zone indicated by the system's TimeZone
>     parameter, and is converted to UTC using the offset for the timezone
>     zone.
> 
>     When a timestamp with time zone value is output, it is always
>     converted from UTC to the current timezone zone, and displayed as
>     local time in that zone. To see the time in another time zone,
>     either change timezone or use the AT TIME ZONE construct (see
>     Section 9.9.3).
> 
>     Conversions between timestamp without time zone and timestamp with
>     time zone normally assume that the timestamp without time zone value
>     should be taken or given as timezone local time. A different time
>     zone can be specified for the conversion using AT TIME ZONE."
> 
> 
>         Right now the code is just inserting and updating records using
>         "localtimestamp(0)".
> 
> 
> 
> 
>         On Wed, Jul 11, 2018 at 4:11 PM, David G. Johnston
>         <david.g.johnston@gmail.com <mailto:david.g.johnston@gmail.com>
>         <mailto:david.g.johnston@gmail.com
>         <mailto:david.g.johnston@gmail.com>>> wrote:
> 
>              On Wednesday, July 11, 2018, David Gauthier
>              <davegauthierpg@gmail.com <mailto:davegauthierpg@gmail.com>
>         <mailto:davegauthierpg@gmail.com
>         <mailto:davegauthierpg@gmail.com>>> wrote:
> 
>                  OK, the "to_char" gets rid of the timezone extension. 
>         But the
>                  times still don't make sense.
> 
>                  When I go to store this in a DB, I want to store the
>         UTC time.         How d I do that ?
> 
>              Use the data type that represents exactly that,
>         timestamptz.  Using
>              the timestamp data type is generally not what you want even
>         if you
>              can get the manipulation logic figured out.
> 
>              David J.
> 
> 
> 
> 
>     -- 
>     Adrian Klaver
>     adrian.klaver@aklaver.com <mailto:adrian.klaver@aklaver.com>
> 
> 


-- 
Adrian Klaver
adrian.klaver@aklaver.com




view thread (13+ messages)

Message-ID: <652f88fe-af0f-4f9c-0ef1-ee814b8a05ac@aklaver.com>
Permalink:  ../652f88fe-af0f-4f9c-0ef1-ee814b8a05ac@aklaver.com/
Also on:    postgresql.org/message-id/652f88fe-af0f-4f9c-0ef1-ee814b8a05ac@aklaver.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: adrian.klaver@aklaver.com, davegauthierpg@gmail.com
  Subject: Re: timestamp (military) at time zone without the suffix
  In-Reply-To: <652f88fe-af0f-4f9c-0ef1-ee814b8a05ac@aklaver.com>
  References: <CAMBRECB=k1CkGjvY-nn=WMtcVFM8f_88D6MK+Bt=jCbHVOpbWA@mail.gmail.com> <CAKFQuwa1UR9pAr+ZSo=C5FUAMdv0yNd432eA0EQF5cJ8i7RQaQ@mail.gmail.com> <CAMBRECBnkee2fG3kWFCFFRfk=Eq40obKkzOL1181H_phZhRtRA@mail.gmail.com> <CAKFQuwY8Z-vnPMRncLu+V+byjVi7a3pHs1p=0kjQQ74cmBmsgg@mail.gmail.com> <CAMBRECBdRyvbFZQFEMx0QqgWsduJV+vG3xJ+6FZTB2CexscYiQ@mail.gmail.com> <84450581-6f59-cf88-ee2c-eaa03f9bba87@aklaver.com> <CAMBRECCbS0RmW24yzAssx54oPT+AgL8bOe0SSdZ0MNX2OyhhwA@mail.gmail.com> <652f88fe-af0f-4f9c-0ef1-ee814b8a05ac@aklaver.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox