åè¨
å¨å®é ä¸å¡å¼åä¸ï¼ä¼ç¢°å°å¤ä»¤æ¶ï¼é°ç§ï¼æ¶åºè½¬æ¢çé®é¢ï¼è¿äºé®é¢é½éè¦ä»ä¸å¡è§åº¦å»èèï¼ä¿è¯ç¨æ·å¨ä»»ä½å°åºçå°çæ°æ®é½ä¸è´çï¼è¿å°±éè¦MySQLæ°æ®åºãå端æå¡ä»¥åå端æå¡åç¸åºçå¤çæè½å®æã
æè¿æä¹å好å¨å¼åçæ¶åéå°äºï¼æ幸就åä¸è¿ä¸ªæ¯è¾å·é¨çæç« ï¼è·å¤§å®¶èèå¤ä»¤æ¶ï¼é°ç§ï¼æ¶åºè½¬æ¢å¨å®é å¼åè¿ç¨ä¸ç解å³æ¹æ¡ã
å¤ä»¤æ¶
å¤ä»¤æ¶ä»ç»
å¤ä»¤æ¶ï¼Daylight Saving Timeï¼DSTï¼ï¼å称"æ¥å è约æ¶å¶"ï¼æ¯ä¸ç§ä¸ºè约è½æºè人为è§å®å°æ¹æ¶é´çå¶åº¦ï¼å¨è¿ä¸å¶åº¦å®è¡æé´æéç¨çç»ä¸æ¶é´ç§°ä¸ºâå¤ä»¤æ¶é´âã
ä¸è¬å¨å¤©äº®æ©çå¤å£äººä¸ºå°æ¶é´è°å¿«ä¸å°æ¶ï¼å¯ä»¥ä½¿äººæ©èµ·æ©ç¡ï¼åå°ç §æéï¼ä»¥å åå©ç¨å ç §èµæºï¼ä»èèçº¦ç §æç¨çµï¼é¤äºå¤ä»¤æ¶å¤è¿æå¬ä»¤æ¶ï¼éç¨çæ¯æ¬å°çæ åæ¶é´ã
å¯ä»¥çå°æ大å©æ¯æå¤ä»¤æ¶å¶ï¼å¤ä»¤æ¶çæ¶é´ä»3æ28æ¥å°10æ31æ¥ï¼å¬ä»¤æ¶ï¼æ¬å°æ åæ¶é´ï¼æ¯ä»11æ1æ¥å°3æ27æ¥ï¼å¨å¤ä»¤æ¶æ¶æ®µå ï¼æ¶é´æ¯æ åæ¶é´å¿«ä¸ä¸ªå°æ¶ï¼ä¾å¦ç½é©¬å¸çæ¶åºGMT + 1:00ï¼æ åæ¶é´ä¸º10:00:00ï¼å¨å¤ä»¤æ¶çæ¶é´å°±æ¯11:00:00ï¼å¬ä»¤æ¶çæ¶é´å°±æ¯10:00:00ã
CET (ä¸æ¬§æ åæ¶é´) æ¯UTC + 01:00æ¶åºçå称ä¹ä¸ï¼æ¯UTCï¼ä¸çæ åæ¶é´ï¼æå1个å°æ¶ï¼ä¸UTCçæ¶é´åå·®å¯å为+01:00ï¼å¨å¬å¤©ä½¿ç¨ï¼å¨å¤å£æ¶ä½¿ç¨CEST - ä¸æ¬§å¤ä»¤æ¶é´ (UTC + 02:00ï¼æåä¸ä¸ªå°æ¶)ã
LInuxæ¶åº
Linuxæå¡å¨çç³»ç»æ¶é´çæ ¡åæ¯éè¿NTPï¼Network Time Protocolï¼æå¡æ¥å®ç°ï¼æ¯éä¸æ®µæ¶é´ä¼è·æ¶éæºè¿è¡æ ¡å¯¹ï¼ç¡®ä¿Linuxç³»ç»æ¶é´çåç¡®æ§ï¼åæ¶Linuxæä½ç³»ç»æ¯æä¸åå½å®¶åå°åºçæ¶åºè®¾ç½®ï¼æææ¶åºä¿¡æ¯ä½äº/usr/share/zoneinfoç®å½ä¸ï¼å¦æéè¦è®¾ç½®æ¶åºï¼åªéè¦å°/etc/localtime软é¾æ¥å°ä¸ä¸ªå ·ä½çå°åºå³å¯ï¼å¦æè¿ä¸ªå°åºæDSTæºå¶ï¼é£ä¹Linuxä¼èªå¨å¨DSTåæ åæ¶é´ä¹é´åæ¢ï¼ä¸éè¦é¢å¤ç代ç æ¥å¤çã
## Linuxæ¯æçåºåä¿¡æ¯
$ ls -ltr /usr/share/zoneinfo/
total 320
lrwxrwxrwx 1 root root 3 10æ 23 05:18 Zulu -> UCT
-rw-r--r-- 1 root root 1544 10æ 23 05:18 W-SU
-rw-r--r-- 1 root root 1873 10æ 23 05:18 WET
lrwxrwxrwx 1 root root 3 10æ 23 05:18 UTC -> UCT
lrwxrwxrwx 1 root root 3 10æ 23 05:18 Universal -> UCT
-rw-r--r-- 1 root root 127 10æ 23 05:18 UCT
-rw-r--r-- 1 root root 1970 10æ 23 05:18 CET
## å端æå¡æå¨Linuxæå¡å¨çæ¶åº
$ ls -ltr /etc/localtime
lrwxrwxrwx 1 root root 33 11æ 1 06:20 /etc/localtime -> /usr/share/zoneinfo/Asia/Shanghai
å¤å¶
éè¿zdumpå½ä»¤æ¥çä¸æ大å©ç½é©¬çæ¶åºå±æ§ã
$ zdump -v /usr/share/zoneinfo/CET
/usr/share/zoneinfo/CET Sun Mar 28 00:59:59 2021 UT = Sun Mar 28 01:59:59 2021 CET isdst=0 gmtoff=3600
/usr/share/zoneinfo/CET Sun Mar 28 01:00:00 2021 UT = Sun Mar 28 03:00:00 2021 CEST isdst=1 gmtoff=7200 #2021å¹´å¤ä»¤æ¶å¼å§
/usr/share/zoneinfo/CET Sun Oct 31 00:59:59 2021 UT = Sun Oct 31 02:59:59 2021 CEST isdst=1 gmtoff=7200 #2021å¹´å¤ä»¤æ¶ç»æ
å¤å¶
Sun Mar 28 01:00:00 2021 UT = Sun Mar 28 03:00:00 2021 CEST isdst=1 gmtoff=7200
ä»ä¸é¢çä¿¡æ¯å¯ä»¥çå°ï¼2021å¹´å¤ä»¤æ¶çå¼å§æ¶é´æ¯Sun Mar 28 01:00:00ï¼ç»ææ¶é´ä¸ºSun Oct 31 00:59:59ï¼isdst = 1说æå½åå¤äºDSTæ¶æ®µï¼gmtoff=7200表示ä¸æ ¼ææ²»æ¶é´çoffsetï¼åä½ç§ï¼å³UTC + 02:00ï¼ä¹ç§°ä¸ºCESTæ¶é´ï¼è¿è¯´æLinuxæä½ç³»ç»å·²ç»èªå¨å®ç°äºä¸å¤ä»¤æ¶DSTçèªå¨åæ¢ã
å¤çå¤ä»¤æ¶
举个ä¾åï¼æ大å©ç½é©¬ç客æ·éè¦å¼åä¸ä¸ªç¨å¡ç³»ç»ï¼ç¨äºå½å åå°å¸çç¨æ¶è®°è´¦ï¼ç±äºæ大å©æ¯æå¤ä»¤æ¶å¶ï¼å°±éè¦èèå¤ä»¤æ¶DSTçå¤çï¼å¨å¼åçè¿ç¨ä¸ï¼æ¶åæ¶é´é®é¢çå æ¬MySQLæ°æ®åºï¼mysql-serverï¼ï¼å端æå¡ï¼backend-serviceï¼ä»¥åå端æå¡ï¼frontend-serviceï¼ä¸ä¸ªæ¹é¢ï¼ä¸é¢å°±ä»ä¸ä¸ªå±é¢åæå¦ä½å»å¤çDSTã
- å端å¤ç
ä¸å¡å¯¹å端çè¦æ±æ¯ï¼ä¸ç®¡ä½¿ç¨çæ¯ç§»å¨ç«¯è¿æ¯PC端ï¼é½åºè¯¥æ£ç¡®çæ¾ç¤ºå½æ¶æ¶é´ï¼å æ¬æå¤ä»¤æ¶å¶çæ¶é´ã
å¦æå¨ä¸å½çè¯ï¼å°±æ¯è¾å¥½å¤çï¼æ²¡æDSTæºå¶ï¼ç»ä¸ä½¿ç¨ä¸å «åºå³GMT/UTC + 08:00å³å¯ï¼å端æå¡çæ¶é´ç´æ¥åLinuxæå¡æå¡å¨çç³»ç»æ¶é´ï¼Linuxçæ¶åºåªéè¦è®¾ç½®ä¸ºAsia/Shanghaiå³å¯ï¼å端ä¸éè¦åä»»ä½æ¶é´çè½¬å ¥è½¬åºã
**è½¬å ¥ï¼**æPOST请æ±åå ¥æ°æ®ï¼user â> frontend-service â> backend-service â> mysql-serverï¼ä¾å¦ç¼´ç¨æ¥å£ã
**转åºï¼**æGET请æ±æ¥è¯¢æ°æ®ï¼mysql-server â> backend-service â> frontend-service â> userï¼ä¾å¦æ¥è¯¢æ¥å£ã
ä¸è¿å¼å¿çæ¯ï¼Linuxæä½ç³»ç»å·²ç»èªå¨å®ç°äºDST转æ¢ï¼å¨å端ä¸éè¦åä»»ä½å¤çï¼è®¾ç½®Linuxæ¶åºä¸ºCETã
# ä¿®æ¹LInuxæ¶åºä¸ºCETï¼ä¹å¯ä»¥éè¿timedatectlå½ä»¤ä¿®æ¹ã
$ ln -sf /usr/share/zoneinfo/CET /etc/localtime
å¤å¶
è¿æ ·å¨æ大å©å½å çç¨æ·çéè¿ç»ç«¯ï¼ç§»å¨ç«¯æPC端ï¼ï¼ç»å½ç³»ç»ç¼´ç¨ææ¥è¯¢æ¶ï¼ç¨æ·æ¶é´åå端æå¡çæ¶é´å®å ¨ä¸è´ï¼å³å®æå¦ä¸è¿ä¸æ¥çå¤çã
- å端å¤ç
æ们äºè§£äºå端Linuxæå¡å¨çæ¶åºè®¾ç½®ä¸ºCETï¼å°±è½èªå¨å¤çæ大å©DSTå¤ä»¤æ¶è½¬æ¢äºï¼å端Javaç¨åºé¨ç½²å¨Linuxæå¡å¨ä¸ï¼å°å ¶æ¶åºè®¾ç½®è·å端ä¸æ ·ï¼ä¹æ¯CETæ¶åºï¼å端åªéè¦æ¥æ¶åç«¯ä¼ è¿æ¥çå¼è¿è¡MySQLçCRUDæä½å³å¯ï¼ç¨å¡è¡¨çç»æå¦ä¸ï¼
CREATE TABLE `tax_form` (
`id` bigint NOT NULL AUTO_INCREMENT COMMENT '主é®id',
`tax_id` varchar(20) NOT NULL DEFAULT '' COMMENT 'ç¨å¡ç¼å·',
`amount` decimal(12,4) NOT NULL DEFAULT '0.0000' COMMENT '纳ç¨éé¢',
`tax_payer_id` varchar(20) NOT NULL DEFAULT '' COMMENT '纳ç¨äººç¼å·',
`status` tinyint NOT NULL DEFAULT '0' COMMENT 'ç¼´ç¨ç¶æ',
`audit_time` datetime NOT NULL DEFAULT '0000-00-00 00:00:00' COMMENT 'å®¡æ ¸æ¶é´',
`create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'å建æ¶é´',
`update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT 'ä¿®æ¹æ¶é´',
PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='ç¨å¡è®°å½è¡¨';
å¤å¶
主è¦çä¸ä¸ç¼´ç¨åå®¡æ ¸æ¥å£ï¼åå«ç对åºçSQLè¯å¥å¦ä¸ï¼
ç¼´ç¨
-- ç¼´ç¨æ¥å£ç对åºçSQL
insert into tax_form(tax_id,amount,tax_payer_id,status) values('T001', 1234.56, 'U001', 0)
å¤å¶
å®¡æ ¸
-- ä¿®æ¹å®¡æ ¸ç¶æ
update tax_form set status = 1, audit_time = '2021-01-07 12:02:30' where tax_payer_id = 'U001';
å¤å¶
æ¶åæ¶é´çå段æ两类
**å ¬å ±å段ï¼**create_timeï¼update_timeè¿äºæ¯æ¯ä¸ªè¡¨å¿ é¡»æçæ¶é´å段ï¼èä¸é»è®¤é½æ¯MySQLçCURRENT_TIMESTAMPï¼åçMySQL serverçå½åç³»ç»æ¶é´ï¼èè¿ä¸ªæ¶é´æ¯è·MySQLçæ¶åºtime_zone设置ä¸åèååçï¼åæ¶MySQLä¹æ¯æ¯æå¤ä»¤æ¶DSTèªå¨è½¬æ¢çã
ä¸å¡å段ï¼audit_timeå®¡æ ¸æ¶é´å±æ§æ¯ç±å端页é¢ä¼ å°å端è¿è¡å¤çï¼å端æ éåä»»ä½è½¬æ¢ã
- MySQLå¤ç
MySQLä¹æ¯æ¯æå¤ä»¤æ¶DSTæºå¶ï¼ä¸è¿è®¾ç½®æ¶åºtime_zoneåªè½è®¾ç½®ä¸ºå°åºï¼ç±»ä¼¼Linux设置æ¶åºä¸æ ·ï¼ï¼ä¸è½ç³æ°¸çº¢MySQL设置çæ¶åºçç¸å ³åé
mysql> show variables like '%zone%';
+------------------+--------+
| Variable_name | Value |
+------------------+--------+
| system_time_zone | CST | -- æ°æ®åºæå¡å¨çå½åæ¶åºï¼ä¸å¯ä¿®æ¹ï¼CSTè¿éæçæ¯ä¸å½æ åæ¶é´(China Standard Time UTC+08:00ï¼å³ä¸å
«åº)
| time_zone | SYSTEM | -- æ°æ®åºæ¶åºï¼é»è®¤è·æå¡å¨ä¿æä¸è´ï¼å¯ä¿®æ¹ã
å¤å¶
ç®åæ¯ä¸å «åºï¼ä¿®æ¹ä¸ºæ大å©æ¶åºï¼å³ä¸ä¸åºã
mysql> select now();
+---------------------+
| now() |
+---------------------+
| 2021-01-07 13:43:31
-- ä¿®æ¹æ°æ®åºæ¶åºä¸ºé¶æ¶åºï¼å³ã
mysql> set time_zone = 'CET';
ERROR 1298 (HY000): Unknown or incorrect time zone: 'CET'
-- å°è¯éè¿+0:00æ¹å¼ä¿®æ¹ï¼å¯ä»¥æåä¿®æ¹ã
mysql> set time_zone = '+1:00';
Query OK, 0 rows affected (0.00 sec)
å¤å¶
MySQLåå¨æ¶åºä¿¡æ¯çæ°æ®åå ¸
mysql> show tables from mysql like '%time_zone%';
+-------------------------------+
| Tables_in_mysql (%time_zone%) |
+-------------------------------+
| time_zone | -- æ¶åºä¿¡æ¯
| time_zone_leap_second | -- æ¶åºé°ç§ä¿¡æ¯
| time_zone_name | -- æ¶åºå
| time_zone_transition | -- æ¶åºè½¬æ¢
| time_zone_transition_type | -- æ¶åºè½¬æ¢ç±»å
å¤å¶
é»è®¤æ åµä¸ï¼è¿äºè¡¨é½æ¯ç©ºçï¼éè¦éè¿MySQLä¸é¨æä¾çå½ä»¤mysql_tzinfo_to_sqlå¯¼å ¥ï¼æ°æ®ä¼è¢«æå ¥å°è¡¨time_zoneç¸å ³ç表ä¸ã
# Linuxä¸çæ¶åºä¿¡æ¯/usr/share/zoneinfoéè¿å½ä»¤mysql_tzinfo_to_sqlå è½½å°ç¸å
³çtime_zone表ä¸ã
$ mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -u root mysql
å¤å¶
æ§è¡å®æä¹åï¼çä¸ä¸è¡¨ä¸çæ°æ®ï¼åå°è¯è®¾ç½®æ¶åºä¸ºCETã
mysql> select * from mysql.time_zone_name where name like '%CET%';
+------+--------------+
| Name | Time_zone_id |
+------+--------------+
| CET | 373 |
-- 设置æ¶åºä¸ºCET
mysql> set time_zone = 'CET';
Query OK, 0 rows affected (0.02 sec)
mysql> select now();
+---------------------+
| now() |
+---------------------+
| 2021-01-07 10:00:36 |
å¤å¶
èä¸ä¹æ¯ææ¶åºè½¬æ¢ï¼ä¾å¦å°å京æ¶é´è½¬æ¢æç½é©¬æ¶é´ã
-- å京æ¶é´17:00:00转æ¢æCETçç½é©¬æ¶é´å°±æ¯10:00:00
mysql> select convert_tz('2021-01-07 17:00:00', 'Asia/Shanghai', 'CET') as time;
+---------------------+
| time |
+---------------------+
| 2021-01-07 10:00:00 |
å¤å¶
æ们è¦è§£å³çé®é¢æ¯ï¼MySQL设置time_zone='CET'åæ¯å¦è½èªå¨å®ç°DST转æ¢ï¼å¦æå¯ä»¥çè¯ï¼é£ä¹ç¨æ·ç«¯ãå端æå¡ãå端æå¡ä»¥åMySQLæå¡å¨æ¶åºå°±ç»ä¸ä¸ºCETï¼åæ¶é½è½èªå¨å¤çDSTï¼ä»ä¸é¢çzdump -v /usr/share/zoneinfo/CETå½ä»¤è¾åºå¯ä»¥çå°ï¼2021å¹´æ大å©çå¤ä»¤æ¶ä»3æ28å·01:59:59å·å¼å§ï¼ä¹å°±æ¯æ¶é´è°å¿«ä¸å°æ¶ã
-- 01:59:59æ¶é´ç¹ï¼æ²¡æåçDSTåæ¢ã
mysql> select convert_tz('2021-03-28 01:59:59', '+1:00', 'CET') as time;
+---------------------+
| time |
+---------------------+
| 2021-03-28 01:59:59 |
-- 02:00:00æ¶é´ç¹ï¼ç¡®å®åçDSTåæ¢ï¼ä»02:00:00è°å¿«äºä¸å°æ¶åæäº03:00:00
mysql> select convert_tz('2021-03-28 02:00:00', '+1:00', 'CET') as time;
+---------------------+
| time |
+---------------------+
| 2021-03-28 03:00:00 |
-- å°+1:00æ¶é´æ¢æCETï¼ç»æä¹æ¯ä¸æ ·çï¼åçäºDSTåæ¢ã
mysql> select convert_tz('2021-03-28 02:00:00', 'CET', 'CET') as time;
+---------------------+
| time |
+---------------------+
| 2021-03-28 03:00:00 |
å¤å¶
ä»ä¸é¢çç»æå¯ä»¥çå°ï¼å½time_zone设置æå°åº/åå¸ï¼ç³»ç»ä¼èªå¨è§£å³å¤ä»¤æ¶DSTQåæ¢é®é¢ï¼å¦æ设置time_zone='+1:00âè¿ç§æ¹å¼å°±å¤±å»äºå¤ä»¤æ¶æºå¶ï¼ç®åå¨MySQLæ°æ®åºä¸ï¼å¨åå§åtime_zoneç¸å ³è¡¨å æ°æ®ä»¥åï¼MySQLå°±å¯ä»¥èªå·±å®æå¤ä»¤æ¶çä¿®æ£ï¼ä¸éè¦é¢å¤çæå¡å¤çã
对äºAWS RDSçæ¥è¯´ï¼time_zoneæ¯å¯ä»¥éæ©å°åº/åå¸çï¼ä¹å°±æ¯æ¯æå¤ä»¤æ¶çèªå¨åæ¢ã
- å¤çå¤ä»¤æ¶æ»ç»
éè¿ä¸é¢çåæå¯ä»¥ç¥éï¼Linuxæå¡å¨åMySQLæå¡å¨é½å¯ä»¥èªå¨å¤çDSTåæ¢ï¼åææ¯éè¦è®¾ç½®Linuxçæ¶åºåMySQLæ¶åºä¸ºå°åºï¼ä¾å¦é½è®¾ç½®ä¸ºCETã
é°ç§
æ为ä¿æåè°ä¸çæ¶æ¥è¿äºä¸çæ¶æ¶å»ï¼ç±å½é 计éå±ç»ä¸è§å®å¨å¹´åºæå¹´ä¸ï¼ä¹å¯è½å¨å£æ«ï¼å¯¹åè°ä¸çæ¶å¢å æåå°1ç§çè°æ´ãæè¿ä¸æ¬¡é°ç§å¨å京æ¶é´2017å¹´1æ1æ¥7æ¶59å59ç§ï¼æ¶éæ¾ç¤º07:59:60ï¼åºç°ã
å¨å®é çä¸å¡ç³»ç»ï¼åé°ç§å½±åçæLinuxæå¡å¨ï¼Java代ç 以åMySQLæ°æ®åºï¼æ们æ¥ççå®ä»¬åå«æ¯æä¹è§£å³çLeapSecondé®é¢çã
Linuxæå¡å¨
对äºå¤§å¤æ°æ°çlinuxå æ ¸ï¼2.6.xå æ ¸ä»¥åæ¯æ¯æLeapSecondï¼å¨è¿ä¹åå¯è½ä¼å¯¼è´Linux Kernel Crashï¼ï¼å¨è®¾è®¡æ¶é½æ¯æ¯æé°ç§çï¼Linuxæä½ç³»ç»æ¶é´æ¯éè¿NTPæå¡æ¥åæ¶éæºæ¥è¿è¡åæ¥ï¼NTPä¼ä¸çº§ä¸çº§å°ä¸åé°ç§äºä»¶éç¥ç´å°æè¾¹ç¼çNTPæå¡å¨ï¼ç¶åNTPå°±ä¼æé°ç§éç¥ç»å®¢æ·ç«¯çæä½ç³»ç»ï¼ç±æä½ç³»ç»æ¥å¤çé°ç§éç¥ã
对äºé°ç§2017-01-01 07:59:60ï¼Linuxå æ ¸éè¦å¤çè¿ä¸ªæ¶é´ï¼å°±éè¦åä¸äºç¹å®çå¤çï¼ä¸è¬ä¼æ以ä¸ä¸ç§æ¹æ¡ã
- åéä¸ç§
- åæ¢ä¸ç§
- çæ£çå¢å ä¸ç§
第ä¸ç§æ¹å¼ä¼å¯¼è´ä¸äºåºäºtimestampçæ¶æ¯éç¥ä¹±åºäºï¼è第äºç§ä¼å¯¼è´åºç°ä¸¤ä¸ªä¸æ¨¡ä¸æ ·çtimestampï¼èæåä¸ç§ä¸ä¼åºç°timestampçé®é¢ï¼ä¹æ¯åé¢Linuxå æ ¸éæ©çå¤çæ¹æ¡ã
mysql> select UNIX_TIMESTAMP('2017-01-01 07:59:59') as nts;
+------------+
| nts |
+------------+
| 1483257599 |
$ date -d '@1483257599' --utc
Sun Jan 1 07:59:59 UTC 2017
$ date -d '@1483257600' --utc
Sun Jan 1 08:00:00 UTC 2017
å¤å¶
ä»è¿éå¯ä»¥çå°ï¼Linuxéç¨çæ¯ç¬¬ä¸ç§æ¹æ¡ï¼çæ£çå¢å ä¸ç§ï¼è¿ä¹ç¬¦åä¸å¡ç³»ç»çéæ±ã
Java代ç
Java代ç çSystem.currentTimeMillis()ä¼äº§çé°ç§60ï¼æ¯åå³äºLinuxæä½ç³»ç»çï¼å¨Linux Kernel 2.6.xä¹åå·²ç»fixäºLeapSecondé®é¢ã
MySQLæ°æ®åº
ä¸é¢çå°äºå¨MySQLä¸å·²ç»æäºmysql.time_zone_leap_secondæ°æ®åå ¸ï¼è¯´æå·²ç»æ¯æäºLeapSecondï¼å¤çæ¹æ¡è·Linux类似ã
-- å建ä¸å¼ æµè¯è¡¨åå¨timestampæ¶é´æ³
CREATE TABLE ls(
id bigint NOT NULL COMMENT 'id',
ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (id));
-- 设置æ°æ®åºæ¶åºä¸ºUTC
mysql> set time_zone = 'UTC';
mysql> set timestamp = 1483257599; --对åºæ¶é´ï¼2017-01-01 07:59:59
mysql> insert into ls(id) values(1);
mysql> set timestamp = 1483257600; --对åºæ¶é´ï¼2017-01-01 07:59:60
mysql> insert into ls(id) values(2);
-- å¯ä»¥çå°MySQL对é°ç§è¿è¡äºå¤çï¼å°07:59:60转æ¢æäº08:00:00ã
mysql> select id, ts, unix_timestamp(ts) from ls;
+----+---------------------+--------------------+
| id | ts | unix_timestamp(ts) |
+----+---------------------+--------------------+
| 1 | 2017-01-01 07:59:59 | 1483257599 |
| 2 | 2017-01-01 08:00:00 | 1483257600 |
-- éè¿é°ç§æ¶é´æ¥è¯¢ä¼æ¥é
mysql> select * from ls where ts = '2017-01-01 07:59:60';
ERROR 1525 (HY000): Incorrect TIMESTAMP value: '2017-01-01 07:59:60'
mysql> select * from ls where ts = '2017-01-01 08:00:00';
+----+---------------------+
| id | ts |
+----+---------------------+
| 2 | 2017-01-01 08:00:00 |
å¤å¶
è·¨å¢ç³»ç»çæ¶é´å¤ç
ä¸é¢ä»ç»çæ大å©ç½é©¬çç¨å¡ç³»ç»ï¼å ¶å®å±äºæ¿ä¼ä¸å¡ï¼åªæå¡äºå½å çç¨æ·çéæ±ï¼ä¸æ¶åæµ·å¤ç¨æ·ç请æ±ï¼ç¸å¯¹æ¥è¯´å°åºå人åé½æ¯è¾åºå®ï¼ä½æ¯åè¿ç§è·¨å¢çµå巨头ebayï¼å®æå¡çç¨æ·éå¸å ¨çåå°ï¼èä¸æ¯ä¸ªå°åºçæ¶åºä¸åï¼åæ¶æ¯ä¸ªæ¶åºçå¤ä»¤æ¶DSTçèµ·å§æ¶é´ä¹ä¸ä¸æ ·ï¼æ们è¦è§£å³çæ¯è¦æ ¹æ®å®¢æ·æå¨å°åºæ¾ç¤ºæ£ç¡®çæ¶é´ï¼å æ¬DSTï¼ï¼è·åé¢çDSTå¤çä¸æ ·ä¹æ¶åå°ä¸ç«¯å¤çï¼å端æå¡ï¼frontend-serviceï¼ï¼å端æå¡ï¼backend-serviceï¼ä»¥åMySQLæ°æ®åºï¼mysql-serverï¼ã
ä»è¿ä¸ªå¾ä¸å¯ä»¥çå°ï¼å端æå¡çUIå±è·ç¨æ·æå¨çå°åºæ¶é´è¦å®å ¨ä¸è´ï¼è³äºå端æå¡åMySQLå¦ä½å¤çæ¶é´ï¼å¯¹äºç¨æ·æ¥è¯´æ ¹æ¬ä¸å ³å¿çï¼è¿å°±è¦æ±åç«¯å¿ é¡»è¦æ ¹æ®ä¸åå°åºï¼ä¸åæ¶åºï¼ä¸åå¤ä»¤æ¶DST产çä¸åçæ¶é´çç¨æ·è¿è¡è½¬æ¢å¤çï¼ä¸åå°åºçæ¶é´è½¬æ¢ç®åå端ï¼Vue/Reactï¼å·²ç»æç°æçæ件å¯ç´æ¥ä½¿ç¨ã
åæ¶å¸æåªå¨å端å¤çç¨æ·æ¶é´çè½¬å ¥å转åºï¼å端åMySQLæ°æ®åºä¸åä»»ä½ä¿®æ¹å°±è½å®æä¸å¡å¤çåæ°æ®åå¨ã
å京ç¨æ·å¨UTC + 8ä¹å°±æ¯ä¸å «åºï¼èç½é©¬ç¨æ·å¨UTC + 1ä¸ä¸åºï¼é½æ¯å¨UTCçåºç¡ä¸åå¤çï¼é£æ们就å¯ä»¥å°æ¶åºé½è®¾ç½®ä¸ºUTCï¼ç¶åæ ¹æ®ç¨æ·æå¨å°åºè¿è¡ç¸åºçå¤çã
MySQLå¤ç
设置MySQLæ°æ®åºçæ¶åºä¸ºUTCï¼ä¸ç®¡ç¨æ·æ¥èªåªä¸ªå°åºåå¨å¨æ°æ®åºçæ¶é´é½æ¯UTCï¼å æ¬å ¬å ±æ¶é´å段ï¼å建æ¶é´ï¼ä¿®æ¹æ¶é´ï¼ä»¥åä¸å¡æ¶é´å段ï¼äº¤æå¼å§æ¶é´ï¼äº¤æç»ææ¶é´ï¼ã
-- 设置æ°æ®åºæ¶åºä¸ºUTCï¼å³é¶æ¶åº
set global time_zone = 'UTC'
å¤å¶
å端å¤ç
MySQLæ¶åºæ¯UTCï¼é£ä¹å端æå¡çæå¨çLinux Serveræ¶åºç»ä¸è®¾ç½®ä¸ºUTCï¼è·MySQLä¿æä¸è´ï¼è¿æ ·å端就ä¸éè¦åä»»ä½è½¬æ¢ã
å端å¤ç
å端æ¿å°æ åæ¶åºUTCçæ°æ®ï¼ç»ä¸æ ¹æ®ç¨æ·æå¨æ¶åºè¿è¡è½¬æ¢ï¼è¿æ ·ä¿è¯ä¸å端æ°æ®æ¶åºçä¸è´æ§ï¼åç«¯æ ¹æ®å®é æ åµè¿è¡æ¸²æãä¸è¬æ¥è®²ï¼å端å°æ¶é´æ°æ®ä¼ éå°å端ï¼å端å°è£ ætimestampååå¨å¨MySQLä¸å¯¹åºtimestampç±»åï¼MySQLä¸çtimestampæ¯ä¸åºåæ¶åºçï¼ä¾å¦æ°æ®åºæ¯UTC 02:00:00ï¼å京ç¨æ·ä½¿ç¨ebayå¨CST 10:00:00ä¸åï¼æ°æ®åºä¸ç订å表çcreate_timeå°±åºè¯¥åå¨2020-12-03 10:00:00ï¼ï¼åæ¶å端æ¥è¯¢æ°æ®çä¹è¦åç¸åºç转æ¢å¤çã
å®æ¶ä»»å¡
å端æå¡ä¸è¬é½ä¼ä¸äºå®æ¶ä»»å¡ï¼è¿ä¸ªæ¶é´ä¸è¬åèªLinux OSçæ¶é´ï¼è·åç«¯æ²¡å ³ç³»ï¼åºäºLinuxçUTCæ¶åºåç¸åºçè°æ´å³å¯ã
æ»ç»
ä¸é¢ä»ç»äºå¤ä»¤æ¶ï¼é°ç§ä»¥åè·¨å¢ç³»ç»çæ¶é´å¤çé®é¢ï¼ä¸»è¦æ¶åå°MySQLæ°æ®åºï¼å端æå¡ä»¥åå端æå¡ä¸ä¸ªå±é¢ï¼å¯¹äºå¤ä»¤æ¶ï¼é°ç§ç转æ¢å¤çï¼LinuxåMySQLé½å¯ä»¥èªå¨å®æå¤çï¼ä¸éè¦é¢å¤è½¬æ¢ï¼å¯¹äºè·¨å¢ç³»ç»çæ¶é´å¤çï¼éè¿è®¾ç½®LinuxåMySQLæ¶åºä¸ºUTCï¼åªéè¦å端æå¡å¤çä¸åå°åºç¨æ·æ¶é´é®é¢ï¼éä½äºç³»ç»æ¹é çé£é©ï¼ä»å¤©å°±èè¿ä¹å¤ï¼å¸æ对大家ææ帮å©ã
ææ¯æä¸ï¼ä½ ç¥éçè¶å¤ï¼ä½ ä¸ç¥éçè¶å¤ï¼æè°¢åä½äººæçï¼ç¹èµãæ¶èåè¯è®ºï¼æ们ä¸æè§ï¼