Oracle Timezone

数据库参数-TIME_ZONE

Oracle中相关的时区大体可以分为两类:数据库时区和session时区。
select dbtimezone from dual;
ALTER DATABASE SET TIME_ZONE='+08:00'; --修改数据库时区
select sessiontimezone from dual;
ALTER SESSION SET TIME_ZONE='+08:00';  --修改当前会话时区


SQL数据类型

TIMESTAMP WITH TIME ZONE:
TIMESTAMP WITH LOCAL TIME ZONE:以数据库时区时间保存在数据库中,用户请求数据时,以客户端会话(session)时区时间返回。
上述两种数据类型都是timestamp的变种。Date和timestamp数据类型不包含时区信息。

时区相关函数:

DBTIMEZONE returns the value of the database time zone
select dbtimezone from dual;

DBTIME
------
+00:00

SESSIONTIMEZONE returns the value of the current session's time zone.
select sessiontimezone from dual;

SESSIONTIMEZONE
---------------------------------------------------------------------------
-05:00

CURRENT_DATE returns the current date in the session time zone, in a value in the Gregorian calendar of data type DATE.

CURRENT_TIMESTAMP returns the current date and time in the session time zone, in a value of data type TIMESTAMP WITH TIME ZONE
show parameter timestamp;

NAME                                 TYPE        VALUE
------------------------------------ ----------- ------------------------------
nls_timestamp_format                 string
nls_timestamp_tz_format              string


show parameter date_format;


NAME                                 TYPE        VALUE
------------------------------------ ----------- ------------------------------
nls_date_format                      string

select CURRENT_TIMESTAMP from dual;

CURRENT_TIMESTAMP
---------------------------------------------------------------------------
20-NOV-13 09.32.50.359313 AM -05:00

SYSDATE returns the date and time of the operating system on which the database resides, taking into account the time zone of the database server's operating system that was in effect when the database was started.

SYSTIMESTAMP returns the system date, including fractional seconds and time zone of the system on which the database resides.
select SYSTIMESTAMP from dual;

SYSTIMESTAMP
---------------------------------------------------------------------------
20-NOV-13 09.33.11.949260 AM -05:00

select cast(systimestamp as timestamp with local time zone) from dual;

CAST(SYSTIMESTAMPASTIMESTAMPWITHLOCALTIMEZONE)
---------------------------------------------------------------------------
20-NOV-13 10.22.06.301906 AM


The return type of systimestamp and current_timestamp is TIMESTAMP WITH TIME ZONE. Here, systimestamp does not use the dbtimezone, as dbtimezone only affects TIMESTAMP WITH LOCAL TIME ZONE. The systimestamp uses the OS timezone instead.


LOCALTIMESTAMP returns the current date and time in the session time zone in a value of data type TIMESTAMP.

SELECT SYSTIMESTAMP AT TIME ZONE 'UTC' FROM DUAL;

SYSTIMESTAMPATTIMEZONE'UTC'
---------------------------------------------------------------------------
20-NOV-13 02.51.40.952831 PM UTC

Note:
All of the datetime functions that return current system datetime information, such as SYSDATE, SYSTIMESTAMP, CURRENT_TIMESTAMP, and so forth, are evaluated once for each SQL statement, regardless how many times they are referenced in that statement.
### Oracle TIMESTAMP WITH TIME ZONE 数据类型 #### 功能描述 `TIMESTAMP WITH TIME ZONE` 是一种支持时区的时间戳数据类型,在存储时间的同时也保存了该时间所属的时区信息。这使得应用程序能够处理跨越多个不同时区的数据,并确保这些数据的一致性和准确性[^3]。 #### 创建表结构 当创建包含 `TIMESTAMP WITH TIME ZONE` 列的表格时,可以按照如下方式定义: ```sql CREATE TABLE events ( id NUMBER PRIMARY KEY, event_name VARCHAR2(100), start_time TIMESTAMP WITH TIME ZONE ); ``` 此命令会建立一张名为 `events` 的新表,其中有一个字段用于记录带有特定时区的时间戳。 #### 插入带有时区的信息 向上述表中插入一条记录时,可以通过指定具体时间和对应的时区来完成操作: ```sql INSERT INTO events (id, event_name, start_time) VALUES (1, 'Conference', TO_TIMESTAMP_TZ('2024-07-15 18:00:00 America/New_York', 'YYYY-MM-DD HH24:MI:SS TZR')); ``` 这里使用了 `TO_TIMESTAMP_TZ()` 函数将字符串转换成具有明确时区属性的时间戳对象。 #### 查询并显示结果 为了查看已存入数据库中的带有时区的时间戳,可以直接执行简单的 SELECT 语句: ```sql SELECT * FROM events; ``` 如果希望获取某个特定格式的结果,则可利用相应的格式化函数来进行调整。例如,要获得本地化的表示形式,可以用下面的方式实现: ```sql SELECT id, event_name, TO_CHAR(start_time AT TIME ZONE SESSIONTIMEZONE(), 'DD-MON-YYYY HH24:MI') AS local_start_time FROM events; ``` 这段 SQL 将把原始 UTC 或其他时区的时间转化为当前会话所在的时区下的表达形式。 #### 处理不同客户端之间的差异 值得注意的是,对于通过 JDBC 进行连接的不同地理位置上的应用服务器而言,即使它们访问同一个 Oracle 实例,由于各自所在的操作环境可能处于不同的时区内,因此所看到的具体数值可能会有所区别。然而,只要遵循标准的做法——即始终以统一的标准(通常是 UTC)作为内部存储的基础,再根据不同需求灵活地进行展示层面的变化——就可以有效避免潜在的问题[^4]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值