从 moment -> nodejs -> sequelize -> postgres,你都得设置好时区

背景 最近在做报表统计,因为 sequelize 的时区配置没加导致了统计数字对不上的问题。 问:大家都知道时区,但是你清楚 UTC 和 GMT 的区别吗? 答:UTC 是我们现在用的时间标准,GMT 是老的时间计量标准。 (1)GMT 是根据地球的自转和公转来计算时间,也就是太阳每天经过位于英国伦敦郊区的皇家格林威治天文台的时间就是中午12点 缺点:地球的自转正在缓速变慢。所以 GMT 会越来越有误差。 国外科学家认为,地球自转变慢的原因有很多,其中包括来自月球的潮汐力以及地核降温等等。预计 100 年以后每天时间会增加 1.8ms,2 亿年之后,地球上一天的是将是 25 小时。 (2)UTC 是根据原子钟来计算时间,50 亿年误差一秒,可以说非常精确。 不同环节中时区的设置 moment -> nodejs -> sequelize -> postgres 1、moment 使用Moment Timezone库: const moment = require('moment-timezone'); moment.tz.setDefault('Asia/Shanghai'); 2、nodejs (1)类 UNIX OS process.env.TZ = 'Asia/Shanghai'; (2)mac / windows 修改系统设置,如 mac 下: moment().format("YYYY-MM-DD HH:mm:ss Z") //2018-09-13 01:44:09 +08:00 // 修改时区 moment().format("YYYY-MM-DD HH:mm:ss Z") //2018-09-12 18:44:25 +01:00 3、postgres (1)配置文件 postgresql.conf timezone = 'PRC' (2)postgres CLI set timezone = 'xxx' 注:postgres 底层存的是 UTC 时间,set timezone 只是改变了数据库展示时间的方式(带时区) show timezone // postgres 默认为 PRC ( = Asia/Shanghai) select "activatedAt" from "Members" where id = '373' //2018-07-26 07:13:38.291+08 //修改时区 set timezone='UTC' show timezone // UTC select "activatedAt" from "Members" where id = '373' //2018-07-25 23:13:38.291+00 4、sequelize timezone: '+08:00' let sequelize = new Sequelize(config.database, config.username, config.password, { "username": "username", "password": "password", "database": "database", "host": "127.0.0.1", "port": 5432, "dialect": "postgres", timezone: '+08:00', // 等同于 postgres 的 `set timezone = 'xxx'` }); 参考文档:https://sequelize.readthedocs.io/en/latest/api/sequelize/ 原文 翻译 分类: node.jshttps://www.cnblogs.com/xjnotxj/p/9638367.html
50000+
5万行代码练就真实本领
17年
创办于2008年老牌培训机构
1000+
合作企业
98%
就业率

联系我们

电话咨询

0532-85025005

扫码添加微信