Are timestamp values stored differently in PostgreSQL when the data type is WITH TIME ZONE
versus WITHOUT TIME ZONE
? Can the differences be illustrated with simple test cases?
相关问题
- Django distinct is not working
- PostgreSQL: left outer join syntax
- Connecting Python to a Heroku PostgreSQL DB?
- Display time in local timezone when querying Influ
- Extracting from a Union Type when some have identi
相关文章
- postgresql 关于使用between and 中是字符串的问题
- postgresql 月份差计算问题
- Using boolean expression in order by clause
- Table valued Parameter Equivalent in Postgresql
- in redshift postgresql can I skip columns with the
- How do I get from a type to the TryParse method?
- Oracle equivalent of PostgreSQL INSERT…RETURNING *
- Java Generics: How to specify a Class type for a g
I try to explain it more understandably than the referred PostgreSQL documentation.
Neither
TIMESTAMP
variants store a time zone (or an offset), despite what the names suggest. The difference is in the interpretation of the stored data (and in the intended application), not in the storage format itself:TIMESTAMP WITHOUT TIME ZONE
stores local date-time (aka. wall calendar date and wall clock time). Its time zone is unspecified as far as PostgreSQL can tell (though your application may knows what it is). Hence, PostgreSQL does no time zone related conversion on input or output. If the value was entered into the database as'2011-07-01 06:30:30'
, then no mater in what time zone you display it later, it will still say year 2011, month 07, day 01, 06 hours, 30 minutes, and 30 seconds (in some format). Also, any offset or time zone you specify in the input is ignored by PostgreSQL, so'2011-07-01 06:30:30+00'
and'2011-07-01 06:30:30+05'
are the same as just'2011-07-01 06:30:30'
. For Java developers: it's analogous tojava.time.LocalDateTime
.TIMESTAMP WITH TIME ZONE
stores a point on the UTC time line. How it looks (how many hours, minutes, etc.) depends on your time zone, but it always refers to the same "physical" instant (like the moment of an actual physical event). The input is internally converted to UTC, and that's how it's stored. For that, the offset of the input must be known, so when the input contains no explicit offset or time zone (like'2011-07-01 06:30:30'
) it's assumed to be in the current time zone of the PostgreSQL session, otherwise the explicitly specified offset or time zone is used (as in'2011-07-01 06:30:30+05'
). The output is displayed converted to the current time zone of the PostgreSQL session. For Java developers: It's analogous tojava.time.Instant
(with lower resolution though), but with JDBC and JPA 2.2 you are supposed to map it tojava.time.OffsetDateTime
(or tojava.util.Date
orjava.sql.Timestamp
of course).Some say that both
TIMESTAMP
variations store UTC date-time. Kind of, but it's confusing to put it that way in my opinion.TIMESTAMP WITHOUT TIME ZONE
is stored like aTIMESTAMP WITH TIME ZONE
, which rendered with UTC time zone happens to give the same year, month, day, hours, minutes, seconds, and microseconds as they are in the local date-time. But it's not meant to represent the point on the time line that the UTC interpretation says, it's just way the local date-time fields are encoded. (It's some cluster of dots on the time line, as the real time zone is not UTC; we don't know what it is.)The differences are covered at the PostgreSQL documentation for date/time types. Yes, the treatment of
TIME
orTIMESTAMP
differs between oneWITH TIME ZONE
orWITHOUT TIME ZONE
. It doesn't affect how the values are stored; it affects how they are interpreted.The effects of time zones on these data types is covered specifically in the docs. The difference arises from what the system can reasonably know about the value:
With a time zone as part of the value, the value can be rendered as a local time in the client.
Without a time zone as part of the value, the obvious default time zone is UTC, so it is rendered for that time zone.
The behaviour differs depending on at least three factors:
WITH TIME ZONE
orWITHOUT TIME ZONE
) of the value.Here are examples covering the combinations of those factors:
Here is an example that should help. If you have a timestamp with a timezone, you can convert that timestamp into any other timezone. If you haven't got a base timezone it won't be converted correctly.
Output: