8.5.1. Date/Time Input

8.5.1. Date/Time Input
8.5.1.日期时间输入
Date and time input is accepted in almost any reasonable format, including ISO 8601, SQL-compatible, traditional POSTGRES, and others. For some formats, ordering of day, month, and year in date input is ambiguous and there is support for specifying the expected ordering of these fields. Set the DateStyle parameter to MDY to select month-day-year interpretation, DMY to select day-month-year interpretation, or YMD to select year-month-day interpretation.
日期和时间输入的时候可以使用几乎所有合理的格式,包括ISO 8601,SQL兼容,传统的POSTGRES等。对于某些格式,输入的日期,月份和年份的顺序可能不明确,不过可以指定这些字段的预期顺序。将DateStyle参数设置为MDY指定格式为月-日-年,DMY为日-月-年,或YMD为年-月-日。
 
PostgreSQL is more flexible in handling date/time input than the SQL standard requires. See Appendix B for the exact parsing rules of date/time input and for the recognized text fields including months,days of the week, and time zones.
PostgreSQL在处理时间日期比SQL标准要求的更加灵活。有关日期时间输入的确切解析规则以及公认的文本字段(包括月份,星期几和时区),请参阅附录B
 
Remember that any date or time literal input needs to be enclosed in single quotes, like text strings.Refer to Section 4.1.2.7 for more information. SQL requires the following syntax
谨记,日期或者时间输入时需要使用单引号引起来,就像字符串那样。更多信息,参见4.1.2.7节。SQL语法:
 
type [ (p) ] 'value'
 
where is an optional precision specification giving the number of fractional digits in the seconds field. Precision can be specified for timetimestamp, and interval types, and can range from 0 to 6. If no precision is specified in a constant specification, it defaults to the precision of the literal value (but not more than 6 digits).
p是可选的精度范围,用以指定在秒区域的小数位数。精度可以在time,timestamp和interval中指定,范围为0到6。如果在常量说明中未指定精度,则默认为文字值的精度(但不超过6位)。
 
8.5.1.1. Dates
8.5.1.1.日期
Table 8.10 shows some possible inputs for the date type.
表8.10展示了date数据类型的几种输入格式。
 

8.5.1.2. Times
8.5.1.2.时间
The time-of-day types are time [ (p) ] without time zone and time [ (p) ] with time zonetime alone is equivalent to time without time zone.
一天中的时间类型有time[(p)] without time zone和time [(p)] with time zone。time与time without time zone等效。
 
Valid input for these types consists of a time of day followed by an optional time zone. (See Table 8.11 and Table 8.12.) If a time zone is specified in the input for time without time zone, it is silently ignored. You can also specify a date but it will be ignored, except when you use a time zone name that involves a daylight-savings rule, such as America/New_York. In this case specifying the date is required in order to determine whether standard or daylight-savings time applies. The appropriate time zone offset is recorded in the time with time zone value.
以上类型的有效输入格式为,时间后加时区(可选)。(参见表8.11和表8.12。)如果在输入时为类型time without time zone指定了时区,那么会忽略该时区。还可以指定一个日期,但是它将被忽略,除非使用的是涉及夏时制规则的时区名称,例如America / New_York。在这种情况下,需要指定日期才能确定是标准时间还是夏令时。适当的时区偏移量以time with time zone值记录。
 
 

Refer to Section 8.5.3 for more information on how to specify time zones.
有关如何定义时区,参见8.5.3节
 
8.5.1.3. Time Stamps
8.5.1.3.时间戳
Valid input for the time stamp types consists of the concatenation of a date and a time, followed by an optional time zone, followed by an optional AD or BC. (Alternatively, AD/BC can appear before the time zone, but this is not the preferred ordering.) Thus:
时间戳的有效输入包含日期和时间,后跟可选的时区,然后跟可选的AD或BC。(当然,AD/BC也可以放到时区前面,但不建议这么做。)因此:
 
1999-01-08 04:05:06
 
and:
和:
 
1999-01-08 04:05:06 -8:00
 
are valid values, which follow the ISO 8601 standard. In addition, the common format:
为遵守ISO 8601的有效值。还有,这种格式:
 
January 8 04:05:06 1999 PST
 
is supported.
也是支持的。
 
The SQL standard differentiates timestamp without time zone and timestamp with time zone literals by the presence of a “+” or “-” symbol and time zone offset after the time. Hence,according to the standard,
SQL标准通过存在“ +”或“-”符号以及时间后的时区偏移来区分不带时区的时间戳和带时区的时间戳。 因此,根据该标准,
 
TIMESTAMP '2004-10-19 10:23:54'
 
is a timestamp without time zone, while
为不带时区的时间戳,而
 
TIMESTAMP '2004-10-19 10:23:54+02'
 
is a timestamp with time zone. PostgreSQL never examines the content of a literal string before determining its type, and therefore will treat both of the above as timestamp without time zone. To ensure that a literal is treated as timestamp with time zone, give it the correct explicit type:
是带时区的时间戳。PostgreSQL在确定字符串的类型之前从未检查过字符串的内容,因此会将以上两者均视为没有时区的时间戳。为确保文字被视为带时区的时间戳,请为其显式提供正确的类型:
 
TIMESTAMP WITH TIME ZONE '2004-10-19 10:23:54+02'
 
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.
对于timestamp without time zone的数据,PostgreSQL将忽略输入值中的时区。也就是说, 结果值是从输入值中的日期/时间字段派生的,并且未针对时区进行调整。
 
For timestamp with time zone, the internally stored value is always in UTC (Universal Coordinated Time, traditionally known as Greenwich Mean Time, GMT). An input value that has an explicit time zone specified is converted to UTC using the appropriate offset for that time zone. If no time zone is stated in the input string, then it is assumed to be in the time zone indicated by the system's TimeZone parameter, and is converted to UTC using the offset for the timezone zone.
对于带有时区的时间戳,内部存储的值始终以UTC(通用协调时间,传统上称为格林威治标准时间,GMT)表示。使用该时区的适当偏移量,将指定了明确时区的输入值转换为UTC。如果在输入字符串中未指定任何时区,则假定该时区位于系统的TimeZone参数指示的时区中,并使用timezone指定的时区的偏移量将其转换为UTC。
 
When a timestamp with time zone value is output, it is always converted from UTC to the current timezone zone, and displayed as local time in that zone. To see the time in another time zone, either change timezone or use the AT TIME ZONE construct (see Section 9.9.3).
当输出带有时区值的时间戳时,总是将其从UTC转换为当前时区,并在该时区中显示为本地时间。要查看其他时区的时间,请更改时区或使用AT TIME ZONE构造(请参见第9.9.3节)。
 
Conversions between timestamp without time zone and timestamp with time zone normally assume that the timestamp without time zone value should be taken or given as timezone local time. A different time zone can be specified for the conversion using AT TIME ZONE.
timestamp without time zone与timestamp with time zone之间转换的时候,一般默认为timestamp without time zone应该给定本地时区。可以使用AT TIME ZONE指定不同的时区。
 
8.5.1.4. Special Values
8.5.1.4.特殊值
PostgreSQL supports several special date/time input values for convenience, as shown in Table 8.13. The values infinity and -infinity are specially represented inside the system and will be displayed unchanged; but the others are simply notational shorthands that will be converted to ordinary date/time values when read. (In particular, now and related strings are converted to a specific time value as soon as they are read.) All of these values need to be enclosed in single quotes when used as constants in SQL commands.
为方便起见,PostgreSQL支持几个特殊的日期/时间输入值,如表8.13所示。 值infinity和-infinity专门用于系统内部,且将保持不变。但其他仅仅是符号速记,仅在读取时将转换为普通的日期/时间值。(特别是,now和相关的字符串在读取时会立即转换为特定的时间值。)当在SQL命令中,将所有这些值用作常量时,都必须用单引号引起来。
 
The following SQL-compatible functions can also be used to obtain the current time value for the corresponding data type: CURRENT_DATECURRENT_TIMECURRENT_TIMESTAMPLOCALTIME,LOCALTIMESTAMP. The latter four accept an optional subsecond precision specification. (See Section 9.9.4.) Note that these are SQL functions and are not recognized in data input strings.
以下SQL兼容函数也可以用于获取对应数据类型的当前时间值:CURRENT_DATE,CURRENT_TIME,CURRENT_TIMESTAMP,LOCALTIME,LOCALTIMESTAMP。 后四个接受可选的亚秒精度规格。 (请参阅第9.9.4节。)请注意,这些是SQL函数,不能在数据输入字符串中识别。
 
发布了341 篇原创文章 · 获赞 54 · 访问量 88万+

猜你喜欢

转载自blog.csdn.net/ghostliming/article/details/104638994