How is a timestamp with timezone read in PostgreSQL?












1















Lets say I have a timestamp with timezone stored in my PostgreSQL database. Something like this:



2003-04-12 04:05:06.814191 America/New_York


When reading this data, would the database return the adjusted time in GMT to standardize all timestamps read, or would it simply return it as is?



The reason I ask this question is I created a table with records, where each record had a timestamp column. I went on to provide different timezones for different records. When I queried this table, and tried ordering by the timestamp column, the timezone did not seem to have any effect on the ordering. Is this by design?



A sample of my results:



1954-06-27 08:44:01.963454 Asia/Baghdad
1954-06-27 08:44:01.963454 GMT
1954-06-27 08:44:01.963454 Europe/Paris
1954-06-27 08:44:01.963454 Asia/Baghdad
1954-06-27 08:44:01.963454 America/New_York
1954-06-27 08:44:01.963454 America/New_York
1954-06-27 08:44:01.963454 America/New_York
1954-06-27 08:44:01.963454 GMT
1954-06-27 08:44:01.963454 America/New_York
1954-06-27 08:44:01.963454 America/New_York
1954-06-27 08:44:01.963454 Europe/Paris
1954-06-27 08:44:01.963454 America/New_York
1954-06-27 08:44:01.963454 Asia/Dhaka
1954-06-27 08:44:01.963454 GMT
1954-06-27 08:44:01.963454 GMT
1954-06-27 08:44:01.963454 Asia/Damascus
1954-06-27 08:44:01.963454 GMT
1954-06-27 08:44:01.963454 Asia/Damascus
1954-06-27 08:44:01.963454 America/New_York
1954-06-27 08:44:01.963454 Asia/Dhaka
1954-06-27 08:44:01.963454 Asia/Baghdad
1954-06-27 08:44:01.963454 Europe/Paris
1954-06-27 08:44:01.963454 Europe/Paris
1954-06-27 08:44:01.963454 Asia/Baghdad
1954-06-27 08:44:01.963454 Asia/Baghdad


The date and time are the same for all these records, and the varying timezones had no effect on the ordering.



To make it more clear, I am actually storing these timestamps as strings in a JSONb column called data with a key of datetime, ie:



data = {
"datetime" : "2003-04-12 04:05:06.814191 America/New_York",
.....
}


During ordering and filtering, I'm casting it as a timestamp. Something like this:



"(data->>'datetime')::timestamp"









share|improve this question




















  • 1





    No timezone data is associated with the TIMEZONETZ type—its' just an instant in time. Can you show how you're inserting/selecting your example data?

    – teppic
    Jan 1 at 23:22











  • @teppic : Sorry I forgot to mention an important detail earlier. Updated the question.

    – darkhorse
    Jan 1 at 23:25











  • If you're casting to timestamp, that means timestamp without time zone, not timestamp with time zone.

    – Wyzard
    Jan 2 at 0:02
















1















Lets say I have a timestamp with timezone stored in my PostgreSQL database. Something like this:



2003-04-12 04:05:06.814191 America/New_York


When reading this data, would the database return the adjusted time in GMT to standardize all timestamps read, or would it simply return it as is?



The reason I ask this question is I created a table with records, where each record had a timestamp column. I went on to provide different timezones for different records. When I queried this table, and tried ordering by the timestamp column, the timezone did not seem to have any effect on the ordering. Is this by design?



A sample of my results:



1954-06-27 08:44:01.963454 Asia/Baghdad
1954-06-27 08:44:01.963454 GMT
1954-06-27 08:44:01.963454 Europe/Paris
1954-06-27 08:44:01.963454 Asia/Baghdad
1954-06-27 08:44:01.963454 America/New_York
1954-06-27 08:44:01.963454 America/New_York
1954-06-27 08:44:01.963454 America/New_York
1954-06-27 08:44:01.963454 GMT
1954-06-27 08:44:01.963454 America/New_York
1954-06-27 08:44:01.963454 America/New_York
1954-06-27 08:44:01.963454 Europe/Paris
1954-06-27 08:44:01.963454 America/New_York
1954-06-27 08:44:01.963454 Asia/Dhaka
1954-06-27 08:44:01.963454 GMT
1954-06-27 08:44:01.963454 GMT
1954-06-27 08:44:01.963454 Asia/Damascus
1954-06-27 08:44:01.963454 GMT
1954-06-27 08:44:01.963454 Asia/Damascus
1954-06-27 08:44:01.963454 America/New_York
1954-06-27 08:44:01.963454 Asia/Dhaka
1954-06-27 08:44:01.963454 Asia/Baghdad
1954-06-27 08:44:01.963454 Europe/Paris
1954-06-27 08:44:01.963454 Europe/Paris
1954-06-27 08:44:01.963454 Asia/Baghdad
1954-06-27 08:44:01.963454 Asia/Baghdad


The date and time are the same for all these records, and the varying timezones had no effect on the ordering.



To make it more clear, I am actually storing these timestamps as strings in a JSONb column called data with a key of datetime, ie:



data = {
"datetime" : "2003-04-12 04:05:06.814191 America/New_York",
.....
}


During ordering and filtering, I'm casting it as a timestamp. Something like this:



"(data->>'datetime')::timestamp"









share|improve this question




















  • 1





    No timezone data is associated with the TIMEZONETZ type—its' just an instant in time. Can you show how you're inserting/selecting your example data?

    – teppic
    Jan 1 at 23:22











  • @teppic : Sorry I forgot to mention an important detail earlier. Updated the question.

    – darkhorse
    Jan 1 at 23:25











  • If you're casting to timestamp, that means timestamp without time zone, not timestamp with time zone.

    – Wyzard
    Jan 2 at 0:02














1












1








1








Lets say I have a timestamp with timezone stored in my PostgreSQL database. Something like this:



2003-04-12 04:05:06.814191 America/New_York


When reading this data, would the database return the adjusted time in GMT to standardize all timestamps read, or would it simply return it as is?



The reason I ask this question is I created a table with records, where each record had a timestamp column. I went on to provide different timezones for different records. When I queried this table, and tried ordering by the timestamp column, the timezone did not seem to have any effect on the ordering. Is this by design?



A sample of my results:



1954-06-27 08:44:01.963454 Asia/Baghdad
1954-06-27 08:44:01.963454 GMT
1954-06-27 08:44:01.963454 Europe/Paris
1954-06-27 08:44:01.963454 Asia/Baghdad
1954-06-27 08:44:01.963454 America/New_York
1954-06-27 08:44:01.963454 America/New_York
1954-06-27 08:44:01.963454 America/New_York
1954-06-27 08:44:01.963454 GMT
1954-06-27 08:44:01.963454 America/New_York
1954-06-27 08:44:01.963454 America/New_York
1954-06-27 08:44:01.963454 Europe/Paris
1954-06-27 08:44:01.963454 America/New_York
1954-06-27 08:44:01.963454 Asia/Dhaka
1954-06-27 08:44:01.963454 GMT
1954-06-27 08:44:01.963454 GMT
1954-06-27 08:44:01.963454 Asia/Damascus
1954-06-27 08:44:01.963454 GMT
1954-06-27 08:44:01.963454 Asia/Damascus
1954-06-27 08:44:01.963454 America/New_York
1954-06-27 08:44:01.963454 Asia/Dhaka
1954-06-27 08:44:01.963454 Asia/Baghdad
1954-06-27 08:44:01.963454 Europe/Paris
1954-06-27 08:44:01.963454 Europe/Paris
1954-06-27 08:44:01.963454 Asia/Baghdad
1954-06-27 08:44:01.963454 Asia/Baghdad


The date and time are the same for all these records, and the varying timezones had no effect on the ordering.



To make it more clear, I am actually storing these timestamps as strings in a JSONb column called data with a key of datetime, ie:



data = {
"datetime" : "2003-04-12 04:05:06.814191 America/New_York",
.....
}


During ordering and filtering, I'm casting it as a timestamp. Something like this:



"(data->>'datetime')::timestamp"









share|improve this question
















Lets say I have a timestamp with timezone stored in my PostgreSQL database. Something like this:



2003-04-12 04:05:06.814191 America/New_York


When reading this data, would the database return the adjusted time in GMT to standardize all timestamps read, or would it simply return it as is?



The reason I ask this question is I created a table with records, where each record had a timestamp column. I went on to provide different timezones for different records. When I queried this table, and tried ordering by the timestamp column, the timezone did not seem to have any effect on the ordering. Is this by design?



A sample of my results:



1954-06-27 08:44:01.963454 Asia/Baghdad
1954-06-27 08:44:01.963454 GMT
1954-06-27 08:44:01.963454 Europe/Paris
1954-06-27 08:44:01.963454 Asia/Baghdad
1954-06-27 08:44:01.963454 America/New_York
1954-06-27 08:44:01.963454 America/New_York
1954-06-27 08:44:01.963454 America/New_York
1954-06-27 08:44:01.963454 GMT
1954-06-27 08:44:01.963454 America/New_York
1954-06-27 08:44:01.963454 America/New_York
1954-06-27 08:44:01.963454 Europe/Paris
1954-06-27 08:44:01.963454 America/New_York
1954-06-27 08:44:01.963454 Asia/Dhaka
1954-06-27 08:44:01.963454 GMT
1954-06-27 08:44:01.963454 GMT
1954-06-27 08:44:01.963454 Asia/Damascus
1954-06-27 08:44:01.963454 GMT
1954-06-27 08:44:01.963454 Asia/Damascus
1954-06-27 08:44:01.963454 America/New_York
1954-06-27 08:44:01.963454 Asia/Dhaka
1954-06-27 08:44:01.963454 Asia/Baghdad
1954-06-27 08:44:01.963454 Europe/Paris
1954-06-27 08:44:01.963454 Europe/Paris
1954-06-27 08:44:01.963454 Asia/Baghdad
1954-06-27 08:44:01.963454 Asia/Baghdad


The date and time are the same for all these records, and the varying timezones had no effect on the ordering.



To make it more clear, I am actually storing these timestamps as strings in a JSONb column called data with a key of datetime, ie:



data = {
"datetime" : "2003-04-12 04:05:06.814191 America/New_York",
.....
}


During ordering and filtering, I'm casting it as a timestamp. Something like this:



"(data->>'datetime')::timestamp"






postgresql datetime






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 1 at 23:24







darkhorse

















asked Jan 1 at 23:01









darkhorsedarkhorse

1,49751846




1,49751846








  • 1





    No timezone data is associated with the TIMEZONETZ type—its' just an instant in time. Can you show how you're inserting/selecting your example data?

    – teppic
    Jan 1 at 23:22











  • @teppic : Sorry I forgot to mention an important detail earlier. Updated the question.

    – darkhorse
    Jan 1 at 23:25











  • If you're casting to timestamp, that means timestamp without time zone, not timestamp with time zone.

    – Wyzard
    Jan 2 at 0:02














  • 1





    No timezone data is associated with the TIMEZONETZ type—its' just an instant in time. Can you show how you're inserting/selecting your example data?

    – teppic
    Jan 1 at 23:22











  • @teppic : Sorry I forgot to mention an important detail earlier. Updated the question.

    – darkhorse
    Jan 1 at 23:25











  • If you're casting to timestamp, that means timestamp without time zone, not timestamp with time zone.

    – Wyzard
    Jan 2 at 0:02








1




1





No timezone data is associated with the TIMEZONETZ type—its' just an instant in time. Can you show how you're inserting/selecting your example data?

– teppic
Jan 1 at 23:22





No timezone data is associated with the TIMEZONETZ type—its' just an instant in time. Can you show how you're inserting/selecting your example data?

– teppic
Jan 1 at 23:22













@teppic : Sorry I forgot to mention an important detail earlier. Updated the question.

– darkhorse
Jan 1 at 23:25





@teppic : Sorry I forgot to mention an important detail earlier. Updated the question.

– darkhorse
Jan 1 at 23:25













If you're casting to timestamp, that means timestamp without time zone, not timestamp with time zone.

– Wyzard
Jan 2 at 0:02





If you're casting to timestamp, that means timestamp without time zone, not timestamp with time zone.

– Wyzard
Jan 2 at 0:02












1 Answer
1






active

oldest

votes


















2














PostgreSQL's "timestamp with time zone" type does not actually store a time zone offset in each record. It just normalizes everything to UTC internally.



The point of "timestamp with time zone" is that because the stored value is represented in a known time zone, it unambiguously represents a specific point in time. The alternative, "timestamp without time zone", is ambiguous because it has a time but doesn't say what zone it's in — the record might say noon today, but is that noon in Greenwich? Noon in New York? Noon in Tokyo? The "with time zone" type avoids that problem, because the zone is always Greenwich.



(For comparison: I believe Oracle has a "timestamp with time zone" that actually stores an offset in each record, and a "timestamp with local time zone" that normalizes everything to the server's time zone to avoid having to store per-record offsets. PostgreSQL's "timestamp with time zone" is named like the former, but behaves like the latter.)





However, it sounds like you're not actually storing a "timestamp with time zone" in your table; you're storing essentially a string, and casting to timestamp as part of a select. The timestamp keyword by itself refers to the "without time zone" variant (as required by the SQL standard), and the PostgreSQL documentation says:




In a literal that has been determined to be timestamp without time zone, PostgreSQL will silently ignore any time zone indication. That is, the resulting value is derived from the date/time fields in the input value, and is not adjusted for time zone.




I'd expect that the same applies with casting, though if that's the case it's not quite clear why your output (of timestamp values) includes the TZ names. But you probably want to cast to timestamptz instead, or maybe create a separate timestamptz column (so the values can be indexed).





The PostgreSQL documentation on date/time types is a good reference.






share|improve this answer


























  • Casting it as timestamptz fixed the issue. Thank you.

    – darkhorse
    Jan 2 at 0:32











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53999605%2fhow-is-a-timestamp-with-timezone-read-in-postgresql%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














PostgreSQL's "timestamp with time zone" type does not actually store a time zone offset in each record. It just normalizes everything to UTC internally.



The point of "timestamp with time zone" is that because the stored value is represented in a known time zone, it unambiguously represents a specific point in time. The alternative, "timestamp without time zone", is ambiguous because it has a time but doesn't say what zone it's in — the record might say noon today, but is that noon in Greenwich? Noon in New York? Noon in Tokyo? The "with time zone" type avoids that problem, because the zone is always Greenwich.



(For comparison: I believe Oracle has a "timestamp with time zone" that actually stores an offset in each record, and a "timestamp with local time zone" that normalizes everything to the server's time zone to avoid having to store per-record offsets. PostgreSQL's "timestamp with time zone" is named like the former, but behaves like the latter.)





However, it sounds like you're not actually storing a "timestamp with time zone" in your table; you're storing essentially a string, and casting to timestamp as part of a select. The timestamp keyword by itself refers to the "without time zone" variant (as required by the SQL standard), and the PostgreSQL documentation says:




In a literal that has been determined to be timestamp without time zone, PostgreSQL will silently ignore any time zone indication. That is, the resulting value is derived from the date/time fields in the input value, and is not adjusted for time zone.




I'd expect that the same applies with casting, though if that's the case it's not quite clear why your output (of timestamp values) includes the TZ names. But you probably want to cast to timestamptz instead, or maybe create a separate timestamptz column (so the values can be indexed).





The PostgreSQL documentation on date/time types is a good reference.






share|improve this answer


























  • Casting it as timestamptz fixed the issue. Thank you.

    – darkhorse
    Jan 2 at 0:32
















2














PostgreSQL's "timestamp with time zone" type does not actually store a time zone offset in each record. It just normalizes everything to UTC internally.



The point of "timestamp with time zone" is that because the stored value is represented in a known time zone, it unambiguously represents a specific point in time. The alternative, "timestamp without time zone", is ambiguous because it has a time but doesn't say what zone it's in — the record might say noon today, but is that noon in Greenwich? Noon in New York? Noon in Tokyo? The "with time zone" type avoids that problem, because the zone is always Greenwich.



(For comparison: I believe Oracle has a "timestamp with time zone" that actually stores an offset in each record, and a "timestamp with local time zone" that normalizes everything to the server's time zone to avoid having to store per-record offsets. PostgreSQL's "timestamp with time zone" is named like the former, but behaves like the latter.)





However, it sounds like you're not actually storing a "timestamp with time zone" in your table; you're storing essentially a string, and casting to timestamp as part of a select. The timestamp keyword by itself refers to the "without time zone" variant (as required by the SQL standard), and the PostgreSQL documentation says:




In a literal that has been determined to be timestamp without time zone, PostgreSQL will silently ignore any time zone indication. That is, the resulting value is derived from the date/time fields in the input value, and is not adjusted for time zone.




I'd expect that the same applies with casting, though if that's the case it's not quite clear why your output (of timestamp values) includes the TZ names. But you probably want to cast to timestamptz instead, or maybe create a separate timestamptz column (so the values can be indexed).





The PostgreSQL documentation on date/time types is a good reference.






share|improve this answer


























  • Casting it as timestamptz fixed the issue. Thank you.

    – darkhorse
    Jan 2 at 0:32














2












2








2







PostgreSQL's "timestamp with time zone" type does not actually store a time zone offset in each record. It just normalizes everything to UTC internally.



The point of "timestamp with time zone" is that because the stored value is represented in a known time zone, it unambiguously represents a specific point in time. The alternative, "timestamp without time zone", is ambiguous because it has a time but doesn't say what zone it's in — the record might say noon today, but is that noon in Greenwich? Noon in New York? Noon in Tokyo? The "with time zone" type avoids that problem, because the zone is always Greenwich.



(For comparison: I believe Oracle has a "timestamp with time zone" that actually stores an offset in each record, and a "timestamp with local time zone" that normalizes everything to the server's time zone to avoid having to store per-record offsets. PostgreSQL's "timestamp with time zone" is named like the former, but behaves like the latter.)





However, it sounds like you're not actually storing a "timestamp with time zone" in your table; you're storing essentially a string, and casting to timestamp as part of a select. The timestamp keyword by itself refers to the "without time zone" variant (as required by the SQL standard), and the PostgreSQL documentation says:




In a literal that has been determined to be timestamp without time zone, PostgreSQL will silently ignore any time zone indication. That is, the resulting value is derived from the date/time fields in the input value, and is not adjusted for time zone.




I'd expect that the same applies with casting, though if that's the case it's not quite clear why your output (of timestamp values) includes the TZ names. But you probably want to cast to timestamptz instead, or maybe create a separate timestamptz column (so the values can be indexed).





The PostgreSQL documentation on date/time types is a good reference.






share|improve this answer















PostgreSQL's "timestamp with time zone" type does not actually store a time zone offset in each record. It just normalizes everything to UTC internally.



The point of "timestamp with time zone" is that because the stored value is represented in a known time zone, it unambiguously represents a specific point in time. The alternative, "timestamp without time zone", is ambiguous because it has a time but doesn't say what zone it's in — the record might say noon today, but is that noon in Greenwich? Noon in New York? Noon in Tokyo? The "with time zone" type avoids that problem, because the zone is always Greenwich.



(For comparison: I believe Oracle has a "timestamp with time zone" that actually stores an offset in each record, and a "timestamp with local time zone" that normalizes everything to the server's time zone to avoid having to store per-record offsets. PostgreSQL's "timestamp with time zone" is named like the former, but behaves like the latter.)





However, it sounds like you're not actually storing a "timestamp with time zone" in your table; you're storing essentially a string, and casting to timestamp as part of a select. The timestamp keyword by itself refers to the "without time zone" variant (as required by the SQL standard), and the PostgreSQL documentation says:




In a literal that has been determined to be timestamp without time zone, PostgreSQL will silently ignore any time zone indication. That is, the resulting value is derived from the date/time fields in the input value, and is not adjusted for time zone.




I'd expect that the same applies with casting, though if that's the case it's not quite clear why your output (of timestamp values) includes the TZ names. But you probably want to cast to timestamptz instead, or maybe create a separate timestamptz column (so the values can be indexed).





The PostgreSQL documentation on date/time types is a good reference.







share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 2 at 0:11

























answered Jan 2 at 0:01









WyzardWyzard

27.9k34968




27.9k34968













  • Casting it as timestamptz fixed the issue. Thank you.

    – darkhorse
    Jan 2 at 0:32



















  • Casting it as timestamptz fixed the issue. Thank you.

    – darkhorse
    Jan 2 at 0:32

















Casting it as timestamptz fixed the issue. Thank you.

– darkhorse
Jan 2 at 0:32





Casting it as timestamptz fixed the issue. Thank you.

– darkhorse
Jan 2 at 0:32




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53999605%2fhow-is-a-timestamp-with-timezone-read-in-postgresql%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Mossoró

Error while reading .h5 file using the rhdf5 package in R

Pushsharp Apns notification error: 'InvalidToken'