8.5.4. Interval Input
8.5.4. Interval Input
8.5.4.Interval输入
interval values can be written using the following verbose syntax:
interval值输入时的详细语法:
[@] quantity unit [quantity unit...] [direction]
where quantity is a number (possibly signed); unit is microsecond, millisecond, second,minute, hour, day, week, month, year, decade, century, millennium, or abbreviations or plurals of these units; direction can be ago or empty. The at sign (@) is optional noise.The amounts of the different units are implicitly added with appropriate sign accounting. ago negates all the fields. This syntax is also used for interval output, if IntervalStyle is set to postgres_verbose.
quantity是一个数字(可以带符号);unit是microsecond, millisecond, second,minute, hour, day, week, month, year, decade, century, millennium,或者这些unit缩写或复数;direction可以是ago或者为空。符号@是可选的。不同的unit会隐式添加适当的符号。ago会给所有的unit加负号。如果IntervalStyle设为postgres_verbose,则此语法也可用于interval输出。
Quantities of days, hours, minutes, and seconds can be specified without explicit unit markings. For example, '1 12:59:10' is read the same as '1 day 12 hours 59 min 10 sec'. Also,a combination of years and months can be specified with a dash; for example '200-10' is read the same as '200 years 10 months'. (These shorter forms are in fact the only ones allowed by the SQL standard, and are used for output when IntervalStyle is set to sql_standard.)
天、时、分和秒数可以不用显式使用unit限定。例如,'1 12:59:10'与'1 day 12 hours 59 min 10 sec'相同。当然,年和月的混合可以使用“-”号指定,例如'200-10'与'200 years 10 months'相同。(实际上,以上的缩写格式为SQL标准中唯一允许的,当IntervalStyle设为sql_standard时使用。)
Interval values can also be written as ISO 8601 time intervals, using either the “format with designators” of the standard's section 4.4.3.2 or the “alternative format” of section 4.4.3.3. The format with designators looks like this:
也可以将interval值写为ISO 8601的时间间隔值,使用4.4.3.2节(未找到此章节)中的“带有指示符的格式”或者4.4.3.3节(未找到此章节)的“特带格式”。带有指示符的格式类似于:
P quantity unit [ quantity unit ...] [ T [ quantity unit ...]]
The string must start with a P, and may include a T that introduces the time-of-day units. The available unit abbreviations are given in Table 8.16. Units may be omitted, and may be specified in any order,but units smaller than a day must appear after T. In particular, the meaning of M depends on whether it is before or after T.
该字符串必须以P开头,并且可以包含引入时间单位的T。表8.16中给出了可用的单位缩写。单位可以省略,也可以以任何顺序指定,但是小于天的单位必须出现在T后面。特别地,M的含义取决于它是在T之前还是之后。
In the alternative format:
另一种格式:
P [ years-months-days ] [ T hours:minutes:seconds ]
the string must begin with P, and a T separates the date and time parts of the interval. The values are given as numbers similar to ISO 8601 dates.
字符串必须以P开否,以T区分interval的时间和日期部分。值以类似于ISO 8601的数字给出。
When writing an interval constant with a fields specification, or when assigning a string to an interval column that was defined with a fields specification, the interpretation of unmarked quantities depends on the fields. For example INTERVAL '1' YEAR is read as 1 year, whereas INTERVAL '1' means 1 second. Also, field values “to the right” of the least significant field allowed by the fields specification are silently discarded. For example, writing INTERVAL '1 day 2:03:04' HOUR TO MINUTE results in dropping the seconds field, but not the day field.
在使用字段说明编写间隔常量时,或将字符串分配给使用字段说明定义的间隔列时,对于未标记量的解释取决于字段。 例如,INTERVAL'1'YEAR读为1年,而INTERVAL'1'表示1秒。同样,字段规范允许的最低有效字段“右侧”的字段值将被忽略。 例如,INTERVAL'1 day 2:03:04'HOUR TO MINUTE将导致删除秒字段,而不是天字段。
According to the SQL standard all fields of an interval value must have the same sign, so a leading negative sign applies to all fields; for example the negative sign in the interval literal '-1 2:03:04' applies to both the days and hour/minute/second parts. PostgreSQL allows the fields to have different signs, and traditionally treats each field in the textual representation as independently signed, so that the hour/minute/second part is considered positive in this example. If IntervalStyle is set to sql_standard then a leading sign is considered to apply to all fields (but only if no additional signs appear). Otherwise the traditional PostgreSQL interpretation is used. To avoid ambiguity, it's recommended to attach an explicit sign to each field if any field is negative.
根据SQL标准,间隔值的所有字段必须具有相同的符号,因此前导负号适用于所有字段;例如,间隔文字“ -1 2:03:04”中的负号适用于日期和小时/分钟/秒部分。PostgreSQL允许字段具有不同的符号,并且传统上将文本表示形式中的每个字段视为独立的,因此在此示例中,小时/分钟/秒部分被视为正数。如果将IntervalStyle设置为sql_standard,则认为前导符号适用于所有字段(但仅当没有其他符号出现时)。否则,将使用传统的PostgreSQL解释。为避免歧义,如果任何字段为负,建议在每个字段上都显式附加一个符号。
In the verbose input format, and in some fields of the more compact input formats, field values can have fractional parts; for example '1.5 week' or '01:02:03.45'. Such input is converted to the appropriate number of months, days, and seconds for storage. When this would result in a fractional number of months or days, the fraction is added to the lower-order fields using the conversion factors 1 month = 30 days and 1 day = 24 hours. For example, '1.5 month' becomes 1 month and 15 days. Only seconds will ever be shown as fractional on output.
在详细的输入格式中,以及在某些更紧凑的输入格式的字段中,字段值可以包含小数部分; 例如“ 1.5周”或“ 01:02:03.45”。 此类输入将转换为适当的月数,天数和秒数以进行存储。如果这将导致小数个月或几天,那么该分数将使用1个月= 30天和1天= 24小时的转换因子添加到低阶字段中。 例如,“ 1.5个月”变为1个月15天。 在输出中,仅秒将显示为小数。
Table 8.17 shows some examples of valid interval input.
表8.17为interval输入的几个示例:
Internally interval values are stored as months, days, and seconds. This is done because the number of days in a month varies, and a day can have 23 or 25 hours if a daylight savings time adjustment is involved. The months and days fields are integers while the seconds field can store fractions. Because intervals are usually created from constant strings or timestamp subtraction, this storage method works well in most cases, but can cause unexpected results:
内部间隔值存储为月,天和秒。这样做是因为一个月中的天数不同,并且如果涉及夏令时调整,则一天可以有23或25个小时。 月和日字段是整数,而秒字段可以存储分数。 由于间隔通常是由常量字符串或时间戳减法创建的,因此该存储方法在大多数情况下都可以很好地工作,但是会导致意外的结果:
SELECT EXTRACT(hours from '80 minutes'::interval);
date_part
-----------
1
SELECT EXTRACT(days from '80 hours'::interval);
date_part
-----------
0
Functions justify_days and justify_hours are available for adjusting days and hours that overflow their normal ranges.
函数justify_days和justify_hours可用于调整超出正常范围的天数和小时数。