Postgresql 中文操作指南
B.2. Handling of Invalid or Ambiguous Timestamps #
通常,如果日期/时间字符串在语法上有效,但包含超出范围的字段值,则将会引发错误。例如,指定 2 月 31 日的输入将被拒绝。
Ordinarily, if a date/time string is syntactically valid but contains out-of-range field values, an error will be thrown. For example, input specifying the 31st of February will be rejected.
在夏令时转换期间,可能会有有效的timestamp字符串表示不存在或者不明确的时间戳。此类情况不会被拒绝;不确定的情形通过确定要应用哪个UTC偏移量来解决。例如,假设 TimeZone参数设置为_America/New_York_,考虑以下内容:
During a daylight-savings-time transition, it is possible for a seemingly valid timestamp string to represent a nonexistent or ambiguous timestamp. Such cases are not rejected; the ambiguity is resolved by determining which UTC offset to apply. For example, supposing that the TimeZone parameter is set to America/New_York, consider
=> SELECT '2018-03-11 02:30'::timestamptz;
timestamptz
------------------------
2018-03-11 03:30:00-04
(1 row)
因为那一天是该时区的春令时开始日期,所以没有 2:30 AM 民用时间;时钟从美国东部时间 2:00 直接跳到美国东部夏令时 3:00。PostgreSQL 将给定的时间解释为标准时间(UTC-5),然后显示为美国东部夏令时 3:30(UTC-4)。
Because that day was a spring-forward transition date in that time zone, there was no civil time instant 2:30AM; clocks jumped forward from 2AM EST to 3AM EDT. PostgreSQL interprets the given time as if it were standard time (UTC-5), which then renders as 3:30AM EDT (UTC-4).
相反,考虑一下回退期间的行为:
Conversely, consider the behavior during a fall-back transition:
=> SELECT '2018-11-04 01:30'::timestamptz;
timestamptz
------------------------
2018-11-04 01:30:00-05
(1 row)
在该日期,有两种可能的 1:30 AM 解释;第一个是美国东部夏令时 1:30 AM,然后时钟从美国东部夏令时 2:00 回退到美国东部时间 1:00,还有一个小时后是美国东部时间 1:30 AM。同样,PostgreSQL 将给定的时间解释为标准时间(UTC-5)。我们可以通过指定夏令时来强制另一种解释:
On that date, there were two possible interpretations of 1:30AM; there was 1:30AM EDT, and then an hour later after clocks jumped back from 2AM EDT to 1AM EST, there was 1:30AM EST. Again, PostgreSQL interprets the given time as if it were standard time (UTC-5). We can force the other interpretation by specifying daylight-savings time:
=> SELECT '2018-11-04 01:30 EDT'::timestamptz;
timestamptz
------------------------
2018-11-04 01:30:00-04
(1 row)
在此类情况下所应用的精确规则是,将分配在时钟提前夏令时转换期间发生的无效时间戳在转换前时区中占优势的协调世界时偏移量,而可能落在时钟回退转换两侧的模糊时间戳将分配在转换后占优势的协调世界时偏移量。对于大多数时区,这等效于“在有疑问时优先采用标准时间解释”。
The precise rule that is applied in such cases is that an invalid timestamp that appears to fall within a jump-forward daylight savings transition is assigned the UTC offset that prevailed in the time zone just before the transition, while an ambiguous timestamp that could fall on either side of a jump-back transition is assigned the UTC offset that prevailed just after the transition. In most time zones this is equivalent to saying that “the standard-time interpretation is preferred when in doubt”.
在所有情况下,都可以使用数字协调世界时偏移量或对应固定协调世界时偏移量的时区缩写来明确指定与时间戳关联的协调世界时偏移量。仅当需要推断偏移量会变化的时区的协调世界时偏移量时,才适用刚才给出的规则。
In all cases, the UTC offset associated with a timestamp can be specified explicitly, using either a numeric UTC offset or a time zone abbreviation that corresponds to a fixed UTC offset. The rule just given applies only when it is necessary to infer a UTC offset for a time zone in which the offset varies.