Introduction to oracle date and timestamp

 

1. Basic Concepts

Time zone: time zone According to the 1884 International Longitude Conference, the world is divided into 24 time zones according to longitude, and each zone occupies 15° of longitude.

The time zone with the prime meridian as the central meridian is the zero time zone, which is divided into 12 zones from the zero time zone to the east and the west. The 12 zones in the east and west are all half time zones, and the time at the 180° longitude is used together.

CST: China Standard Time UTC+8:00 China Standard Time (Beijing Time), in East Eighth District

UTC: Universal Time Coordinated, Universal Coordinated Time, also known as Universal Standard Time, Universal Universal Time. UTC provides a time zone-independent (or time zone-neutral) time.

All time zones in the world can be represented as UTC plus or minus an offset.

Therefore, UTC is the time in time zone 0. For example, Beijing is eight in the morning (East Eighth District), UTC time is zero, and the time is eight hours later than Beijing time.

GMT: Greenwich Mean Time Greenwich Mean Time, which refers to the standard time at the Royal Greenwich Observatory in the suburbs of London, England, as the prime meridian is defined as the longitude passing there.

Unix timestamp: Unix timestamp, or Unix time, POSIX time, is a way of representing time.

Defined as the total number of seconds since 01/01/1970 00:00:00 Greenwich Mean Time (midnight in UTC/GMT).

You can say that:

UTC and GMT are almost the same concept. The difference between the two is that GMT is an astronomical concept, and UTC is based on atomic clocks.

GMT=UTC

GMT + 8 = UTC + 8 = CST

UTC + time difference = local time (the time difference is positive in the east, negative in the west, and recorded as +0800 in the East Eighth District)

 

2. Common date data types
2.1. DATE
This is the most commonly used date type in ORACLE. It can save date and time, and this type can be used for common date processing. The date range represented by DATE can be from January 1, 4712 BC to December 31, 9999 AD
. The storage of the date type in the database is fixed to 7 bytes, and the format is:
 1st byte: century + 100
 2nd Byte: year
 3rd byte: month
 4th byte: day
 5th byte: hour+1
 6th byte: minute+1
 7th byte: second+1

2.2, TIMESTAMP (p)
This is also a commonly used date type in ORACLE. The difference between it and date is that it can not only save the date and time, but also save fractional seconds. The number of decimal places can be specified as 0-9, and the default is 6, so the highest The precision can be up to ns (nanoseconds). The database is stored in 7 or 11 bytes internally. If the precision is 0, it is stored in 7 bytes. It has the same function as the date type. If the precision is greater than 0, it is stored in 11 bytes.
The format is:
 1st byte: century+100
 2nd byte: year
 3rd byte: month
 4th byte: day
 5th byte: hour+1
 6th byte: minute+1
 7th byte: Second+1
 8th-11th bytes: nanoseconds, stored in 4 bytes, the internal operation type is integer

Note: If the TIMESTAMP date type is added or subtracted with the value, it will be automatically converted to the DATE type, that is to say, the fractional seconds will be automatically removed.

 

As for the storage methods of the two data types, you can use the dump function to verify. It can be seen from the above that timestamp is nothing but an extension of date.

 

3. A project recently done by the company is to store the time in the form of a timestamp (the number of milliseconds from GMT time on January 1, 1970) in the form of long, so that the local time can be obtained in different time zones.

Oracle's timestamp data type storage form is as above, it can be seen that it will be affected by time zone. For example, a timestamp time of 1970-1-1 0:0:0 is stored, and the java code is taken out and stored in java.util.Date. The value obtained by getTime() is

-28800000. Because CST is 8 hours earlier than GMT, 1970-1-1 0:0:0 of ​​CST is 1969-12-31 16:0:0 of ​​GMT

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326020357&siteId=291194637