Обсуждение: selecting timestamp

Поиск
Список
Период
Сортировка

selecting timestamp

От
chris
Дата:

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

Re: selecting timestamp

От
Adrian Klaver
Дата:
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


Re: selecting timestamp

От
"David G. Johnston"
Дата:
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.


Re: selecting timestamp

От
Joe Conway
Дата:
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


Вложения

Re: selecting timestamp

От
chris
Дата:

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> 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.