LINUX时区
LINUX 操作系统时区由/etc/localtime设置,其可以是一个指向/usr/share/zoneinfo下文件的软连接,
当然也可以拷贝,在/usr/share/zoneinfo目录下每个文件都包含了特定地区的时区信息,很多都分为
洲目录/地区目录
如:
UTC:GMT标准时间+0时区
CET:欧洲中部时间
EST:美国东部标准时间
Asia/Shanghai:中国上海+8时区 (亚洲目录/上海地区)
我们可以简单的建立一个连接
cd /etc
ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
[root@testmy etc]# date -R
Wed, 07 Dec 2016 06:30:53 +0800
[root@testmy etc]# unlink localtime
[root@testmy etc]# ln -s /usr/share/zoneinfo/UTC /etc/localtime
[root@testmy etc]# date -R
Tue, 06 Dec 2016 22:31:18 +0000
也可以拷贝:
[root@testmy etc]# cp /usr/share/zoneinfo/UTC localtime
cp: overwrite `localtime'? y
[root@testmy etc]# date
Wed Dec 7 00:13:22 UTC 2016
[root@testmy etc]# cp /usr/share/zoneinfo/Asia/Shanghai localtime
cp: overwrite `localtime'? y
[root@testmy etc]# date
Wed Dec 7 08:13:41 CST 2016
可以看到时区的变换,注意时区更改变数据库某些函数的返回
ORACLE数据库SYSDATE和SYSTIMESTAMP函数的返回将会随着系统时区的改变而改变,LOCALTIMESTAMP
CURRENT_TIMESTAMP和CURRENT_DATE ,它们跟随的是会话级别的时区也就是应用连接本地时区
下来是来自metalink的描述:
LOCALTIMESTAMP returns the current date and time in the session time zone in
a value of datatype TIMESTAMP, that is date time similar to CURRENT_DATE
but the datatype is TIMESTAMP.
CURRENT_TIMESTAMP returns the current date and time in the session time zone,
in a value of datatype TIMESTAMP WITH TIME ZONE, that is date time similar to
CURRENT_DATE but the datatype is TIMESTAMP WITH TIME ZONE.
SYSTIMESTAMP returns the system date, including fractional seconds and time zone,
of the system on which the database resides. The return type is TIMESTAMP WITH
TIME ZONE. Unlike SYSDATE, which you can set to a constant using FIXED_DATE,
SYSTIMESTAMP will give the system date even though FIXED_DATE is set.
"SYSDATE" and "SYSTIMESTAMP" are purely dependent on the operating system clock,
hence it IS depending on the timezone information of this operating system and/or
the operating system settings when the database and listener where started.
列子:
SQL> select to_CHAR(systimestamp,'YYYY-MM-DD HH24:MI:SS:TZR') from dual;
TO_CHAR(SYSTIMESTAMP,'YYYY-MM-
----------------------------------------------------
2016-12-06 23:00:16:+00:00
更改系统时区后,重新连接一个session
SQL> select to_CHAR(systimestamp,'YYYY-MM-DD HH24:MI:SS:TZR') from dual;
TO_CHAR(SYSTIMESTAMP,'YYYY-MM-
----------------------------------------------------
2016-12-07 07:00:58:+08:00
看到时区改变了
MYSQL数据库如果设置参数:
time_zone 为system,那么代表每一个连接线程使用服务器OS的系统时间
那么时区受
| system_time_zone | UTC |
的影响,可以看到我这里是UTC标准时区
那么这个时候某些函数如now(),current_time(),sysdate()都将受到影响,
但是和ORACLE不同如果MYSQL数据库不重启数据库将不会受到影响
列子:
mysql> show variables like '%time_zone%';
+------------------+--------+
| Variable_name | Value |
+------------------+--------+
| system_time_zone | UTC |
| time_zone | SYSTEM |
+------------------+--------+
2 rows in set (0.00 sec)
mysql> select now(),current_time(),sysdate();
+---------------------+----------------+---------------------+
| now() | current_time() | sysdate() |
+---------------------+----------------+---------------------+
| 2016-12-06 23:06:17 | 23:06:17 | 2016-12-06 23:06:17 |
+---------------------+----------------+---------------------+
更改OS时区后重启MYSQL数据库
mysql> show variables like '%time_zone%';
+------------------+--------+
| Variable_name | Value |
+------------------+--------+
| system_time_zone | CST |
| time_zone | SYSTEM |
+------------------+--------+
2 rows in set (0.00 sec)
mysql> select now(),current_time(),sysdate();
+---------------------+----------------+---------------------+
| now() | current_time() | sysdate() |
+---------------------+----------------+---------------------+
| 2016-12-07 07:07:11 | 07:07:11 | 2016-12-07 07:07:11 |
+---------------------+----------------+---------------------+
1 row in set (0.00 sec)
可以看到时区改变。
当然time_zone是连接级别可以设置的,我们可以设置它为正确的时区,而不依赖
OS的时区如:set global time_zone = '+8:00';
最后还是给出一段简单的代码用于查看时区的变化对系统时间的影响
gaopeng@bogon:~/linuxapinew$ TZ="Asia/Shanghai" ./a.out
ctime() is:Wed Dec 28 19:15:55 2016
asmtime is:Wed Dec 28 19:15:55 2016
gaopeng@bogon:~/linuxapinew$ TZ="UTC" ./a.out
ctime() is:Wed Dec 28 11:16:01 2016
asmtime is:Wed Dec 28 11:16:01 2016
代码如下:
点击(此处)折叠或打开
- /*************************************************************************
- > File Name: timezone.c
- > Author: gaopeng QQ:22389860 all right reserved
- > Mail: gaopp_200217@163.com
- > Created Time: Wed 28 Dec 2016 06:57:50 PM CST
- ************************************************************************/
-
- #include<stdio.h>
- #include <time.h>
- #include <errno.h>
-
-
- int main(void)
- {
- time_t t;
- struct tm *loc;
- char* ctc;
- char* asc;
-
- t = time(NULL);
- ctc = ctime(&t);
- if ((loc = localtime(&t)) ==NULL)
- {
- perror("localtime:");
- }
-
- asc = asctime(loc);
-
- printf("ctime() is:%sasmtime is:%s",ctc,asc);
- return 0;
-
- }