pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
selecting timestamp
5+ messages / 4 participants
[nested] [flat]

* selecting timestamp
@ 2018-02-27 20:16 chris <chrisk@pgsqlrocket.com>
  2018-02-27 20:25 ` Re: selecting timestamp Adrian Klaver <adrian.klaver@aklaver.com>
  2018-02-27 20:26 ` Re: selecting timestamp David G. Johnston <david.g.johnston@gmail.com>
  2018-02-27 20:33 ` Re: selecting timestamp Joe Conway <mail@joeconway.com>
  0 siblings, 3 replies; 5+ messages in thread

From: chris @ 2018-02-27 20:16 UTC (permalink / raw)
  To: pgsql-general


What is the best way of selecting current timestamp in UTC?

SELECT
CURRENT_TIMESTAMP   as ct1
,timezone('UTC',CURRENT_TIMESTAMP) as ct2
,timezone('utc',now()) as ct3
,CURRENT_TIMESTAMP at time zone 'UTC' as ct4
,NOW() at time zone 'utc' as ct5

^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: selecting timestamp
  2018-02-27 20:16 selecting timestamp chris <chrisk@pgsqlrocket.com>
@ 2018-02-27 20:25 ` Adrian Klaver <adrian.klaver@aklaver.com>
  2 siblings, 0 replies; 5+ messages in thread

From: Adrian Klaver @ 2018-02-27 20:25 UTC (permalink / raw)
  To: chris <chrisk@pgsqlrocket.com>; pgsql-general

On 02/27/2018 12:16 PM, chris wrote:
> 
> What is the best way of selecting current timestamp in UTC?
> 
> SELECT
> CURRENT_TIMESTAMP   as ct1

Well the above would depend on your database having its time zone
set to UTC.

> ,timezone('UTC',CURRENT_TIMESTAMP) as ct2
> ,timezone('utc',now()) as ct3
> ,CURRENT_TIMESTAMP at time zone 'UTC' as ct4
> ,NOW() at time zone 'utc' as ct5

The rest would do the job. The question becomes where are you planning 
on calling these and what is tine you are looking for? See the below for 
more information:

https://www.postgresql.org/docs/10/static/functions-datetime.html#FUNCTIONS-DATETIME-CURRENT

Basically the functions act differently in transactions, which will 
affect the time returned.


-- 
Adrian Klaver
adrian.klaver@aklaver.com




^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: selecting timestamp
  2018-02-27 20:16 selecting timestamp chris <chrisk@pgsqlrocket.com>
@ 2018-02-27 20:26 ` David G. Johnston <david.g.johnston@gmail.com>
  2018-02-28 21:15   ` Re: selecting timestamp chris <chrisk@pgsqlrocket.com>
  2 siblings, 1 reply; 5+ messages in thread

From: David G. Johnston @ 2018-02-27 20:26 UTC (permalink / raw)
  To: chris <chrisk@pgsqlrocket.com>; +Cc: pgsql-general

On Tue, Feb 27, 2018 at 1:16 PM, chris <chrisk@pgsqlrocket.com> wrote:

>
> What is the best way of selecting current timestamp in UTC?
>

​You
​ haven't​
 define
​d​
criteria upon which to judge - and the list below is not exhaustive
​ (but sufficiently so)​
​

>
> SELECT
> CURRENT_TIMESTAMP   as ct1
>

standard conforming, ​assumes server configured for UTC
​


> ,timezone('UTC',CURRENT_TIMESTAMP) as ct2
> ,timezone('utc',now()) as ct3
>

​non-standard, personally non-obvious (the function looks like an
implementation detail that should be avoided)​

,CURRENT_TIMESTAMP at time zone 'UTC' as ct4
>

​This -​

standard conforming and doesn't require assumptions about the calling
environment

,NOW() at time zone 'utc' as ct5
>

​non-standard but frequently used; no semantic different compared to the
previous entry
​
David J.

^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: selecting timestamp
  2018-02-27 20:16 selecting timestamp chris <chrisk@pgsqlrocket.com>
  2018-02-27 20:26 ` Re: selecting timestamp David G. Johnston <david.g.johnston@gmail.com>
@ 2018-02-28 21:15   ` chris <chrisk@pgsqlrocket.com>
  0 siblings, 0 replies; 5+ messages in thread

From: chris @ 2018-02-28 21:15 UTC (permalink / raw)
  To: David G. Johnston <david.g.johnston@gmail.com>; +Cc: pgsql-general

Thank you everyone for the help.
Sorry there wasn't much detail given originally.

CURRENT_TIMESTAMP at time zone 'UTC' as ct4

Seems to be working well.

Thanks,
Chris

On 02/27/2018 01:26 PM, David G. Johnston wrote:
> On Tue, Feb 27, 2018 at 1:16 PM, chris <chrisk@pgsqlrocket.com 
> <mailto:chrisk@pgsqlrocket.com>>wrote:
>
>
>     What is the best way of selecting current timestamp in UTC?
>
>
> ​You
> ​ haven't​
>  define
> ​d​
> criteria upon which to judge - and the list below is not exhaustive
> ​ (but sufficiently so)​
> ​
>
>
>     SELECT
>     CURRENT_TIMESTAMP   as ct1
>
>
> standard conforming, ​assumes server configured for UTC
> ​
>
>     ,timezone('UTC',CURRENT_TIMESTAMP) as ct2
>     ,timezone('utc',now()) as ct3
>
>
> ​non-standard, personally non-obvious (the function looks like an 
> implementation detail that should be avoided)​
>
>     ,CURRENT_TIMESTAMP at time zone 'UTC' as ct4
>
>
> ​This -​
>
> standard conforming and doesn't require assumptions about the calling 
> environment
>
>     ,NOW() at time zone 'utc' as ct5
>
>
> ​non-standard but frequently used; no semantic different compared to 
> the previous entry
> ​
> David J.
>
>

^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: selecting timestamp
  2018-02-27 20:16 selecting timestamp chris <chrisk@pgsqlrocket.com>
@ 2018-02-27 20:33 ` Joe Conway <mail@joeconway.com>
  2 siblings, 0 replies; 5+ messages in thread

From: Joe Conway @ 2018-02-27 20:33 UTC (permalink / raw)
  To: chris <chrisk@pgsqlrocket.com>; pgsql-general

On 02/27/2018 12:16 PM, chris wrote:
> 
> What is the best way of selecting current timestamp in UTC? 
> 
> SELECT
> CURRENT_TIMESTAMP   as ct1
> ,timezone('UTC',CURRENT_TIMESTAMP) as ct2
> ,timezone('utc',now()) as ct3
> ,CURRENT_TIMESTAMP at time zone 'UTC' as ct4
> ,NOW() at time zone 'utc' as ct5

Depends on whether you want a volatile or stable answer:

\x
SELECT clock_timestamp(),
       clock_timestamp() AT TIME ZONE 'UTC',
       now(),
       now() AT TIME ZONE 'UTC'
FROM generate_series(1,3);
-[ RECORD 1 ]---+------------------------------
clock_timestamp | 2018-02-27 12:30:10.46699-08
timezone        | 2018-02-27 20:30:10.466991
now             | 2018-02-27 12:30:10.466692-08
timezone        | 2018-02-27 20:30:10.466692
-[ RECORD 2 ]---+------------------------------
clock_timestamp | 2018-02-27 12:30:10.467017-08
timezone        | 2018-02-27 20:30:10.467017
now             | 2018-02-27 12:30:10.466692-08
timezone        | 2018-02-27 20:30:10.466692
-[ RECORD 3 ]---+------------------------------
clock_timestamp | 2018-02-27 12:30:10.467023-08
timezone        | 2018-02-27 20:30:10.467023
now             | 2018-02-27 12:30:10.466692-08
timezone        | 2018-02-27 20:30:10.466692

HTH,

Joe

-- 
Crunchy Data - http://crunchydata.com
PostgreSQL Support for Secure Enterprises
Consulting, Training, & Open Source Development

Attachments:

  signature.asc (application/pgp-signature, 818B) download

^ permalink  raw  reply  [nested|flat] 5+ messages in thread


end of thread, other threads:[~2018-02-28 21:15 UTC | newest]

Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2018-02-27 20:16 selecting timestamp chris <chrisk@pgsqlrocket.com>
2018-02-27 20:25 ` Adrian Klaver <adrian.klaver@aklaver.com>
2018-02-27 20:26 ` David G. Johnston <david.g.johnston@gmail.com>
2018-02-28 21:15   ` chris <chrisk@pgsqlrocket.com>
2018-02-27 20:33 ` Joe Conway <mail@joeconway.com>

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