系统开发中关于时区的设置

简介

中国的北京时间使用的是+0800(东八区),那就是指china standard time (中国标准时间CST)。全区360°时间是以0°经线为中心,按24小时划分为24个时区,度以东的12个时区称为东时区,而东八区就是以120°经线为中心,向东西各7.5范围的一个时区。我国的时间以北京所在的东八区为标准‍。

1、linux 修改时区

查看时区
#以下两种方法可以查看
$ date
Wed Nov 17 10:30:37 CST 2021
$ ls -l /etc/localtime
lrwxrwxrwx 1 root root 33 Dec 25  2019 /etc/localtime -> /usr/share/zoneinfo/Asia/Shanghai
修改时区的几种方法

方法1 执行 tzselect 命令修改时区,通过向导选择你所在大洲、国家和城市
在这里插入图片描述
**方法2 ** 更改每个用户的时区
Linux 用户一个多用户系统,每个用户都可以配置自己所需的时区,你可以为自己新增一个 TZ 环境变量:

$ export TZ='Asia/Shanghai'
执行完成之后需要重新登录系统或刷新 ~/.bashrc 生效。
$ source ~/.bashrc

方法3 更改Linux系统时区
要更改 Linux 系统整个系统范围的时区可以使用如下命令:

sudo rm -f /etc/localtime
sudo ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

注意:/usr/share/zoneinfo/Asia/Shanghai 中的具体时区请用自己获取到的 TZ 值进行替换。

或者使用Systemd更改Linux系统时区,如果你使用的 Linux 系统使用 Systemd,还可以使用 timedatectl 命令来更改 Linux 系统范围的时区。在 Systemd 下有一个名为 systemd-timedated 的系统服务负责调整系统时钟和时区,我们可以使用 timedatectl 命令对此系统服务进行配置。
sudo timedatectl set-timezone 'Asia/Shanghai'

2、mysql 修改时区

查看时区、查看当前时间

show variables like '%time_zone%';
select now();

在这里插入图片描述
由于mysql中修改

set global time_zone = '+8:00';
set time_zone = '+8:00';
flush privileges;

3、 MySQL 时间类型 DATE、DATETIME和TIMESTAMP

TypeRangeRemark
DATE‘1000-01-01’ to ‘9999-12-31’只有日期部分,没有时间部分
DATETIME‘1000-01-01 00:00:00’ to ‘9999-12-31 23:59:59’时间格式为 YYYY-MM-DD hh:mm:ss,默认精确到秒
TIMESTAMP‘1970-01-01 00:00:01’ UTC to '2038-01-19 03:14:07’UTC默认精确到秒

TIMESTAMP
对于TIMESTAMP,它把客户端插入的时间从当前时区转化为UTC(世界标准时间)进行存储。查询时,将其又转化为客户端当前时区进行返回。而对于DATETIME,不做任何改变,基本上是原样输入和输出。
MySQL converts TIMESTAMP values from the current time zone to UTC for storage, and back from UTC to the current time zone for retrieval. (This does not occur for other types such as DATETIME.)
By default, the current time zone for each connection is the server’s time. The time zone can be set on a per-connection basis.
As long as the time zone setting remains constant, you get back the same value you store. If you store a TIMESTAMP value, and then change the time zone and retrieve the value,
the retrieved value is different from the value you stored. This occurs because the same time zone was not used for conversion in both directions.
The current time zone is available as the value of the time_zone system variable.

参考官网

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值