8.5.1. Date/Time Input

8.5.1. Date/Time Input
8.5.1. Enter the date and time
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.
The date and time when the input can use almost any reasonable format, including ISO 8601, SQL-compatible, traditional POSTGRES and so on. For certain date format, input, month and year of the order may not be clear, but you can specify the expected order of the fields. The DateStyle parameter to specify the format MDY Month - Day - Year, DMY for the day - month - year, or YMD for the year - month - day.
 
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 is more flexible in handling date and time than the SQL standard requires. The exact date and time parsing rules for input and recognized text fields (including month, day of week and time zone), refer to Appendix 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
You need to use single quotation marks when remember the date or time entry, as did a string. For more information, see Section 4.1.2.7 . SQL syntax:
 
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 is an optional range of accuracy for the specified number of decimal places in the second region. Accuracy can be specified in time, timestamp, and interval in the range of 0-6. If the precision is not specified in the constant description, the default value for the accuracy of the text (but not more than 6).
 
8.5.1.1. Dates
8.5.1.1. Date
Table 8.10 shows some possible inputs for the date type.
Table 8.10 shows the input date data type several formats.
 

8.5.1.2. Times
8.5.1.2. Time
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 of day types are time [(p)] without time zone and time [(p)] with time zone. time equivalent to 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.
The above type of valid input format, the time plus the time zone (optional). (See Table 8.11 and Table 8.12.) If the type of time without time zone specified time zone when you enter, it will ignore the time zone. You can also specify a date, but it will be ignored, unless you are using the time zone Name and address daylight saving time rules, for example America / New_York. In this case, you need to specify the date to determine whether standard time or daylight saving time. The appropriate time-zone offset value to the recording time with time zone.
 
 

Refer to Section 8.5.3 for more information on how to specify time zones.
About how to define time zones, see Section 8.5.3 .
 
8.5.1.3. Time Stamps
8.5.1.3. Timestamp
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:
The valid input time stamp the date and time, followed by an optional time zone, and with an optional AD or BC. (Of course, AD / BC may be placed in front of the time zone, but is not recommended.) Thus:
 
1999-01-08 04:05:06
 
and:
with:
 
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万+

Guess you like

Origin blog.csdn.net/ghostliming/article/details/104638994