目录
- 前言
- 一、时间戳是什么?
- 二、时区数据处理
- 三、时区数据测试工具
- 总结
前言
提示:这里可以添加本文要记录的大概内容:
某时区用户A,系统运行发生异常,产生告警记录,通知相关负责人某时区B,并告知触发时间T_a,但是由于A所在时区和B所在时区不同,A告知的触发时间为A时区的时间,如果B要查询这条记录的话,就得先把A时区的时间T_a,转成的B时区对应的时间,才能查询到当前的记录。不方便快速的定位问题,故此,在系统,添加时区选项,可以切换任意时区,进行数据查询。
这里我们的处理方式是,有前端添加一个时区的选项,用户选择时区,并且选择具体时间之后,前端将时间数据处理成时间戳,传递给后端。
提示:以下是本篇文章正文内容,下面案例可供参考
一、时间戳是什么?
时间戳就是Unix时间戳(Unix timestamp),定义为从格林威治时间1970年01月01日00时00分00秒起至现在的总秒数,不考虑闰秒。
时间戳是不会跟着时区的改变而改变,所以不管你在哪个时区,时间戳都是一致的。
二、时区数据处理
时区选择框的默认选项为当前时区
new Date().getTimezoneOffset()/60 //获取当前时区
<div class="layout-start light-select item">
<label style="width: 56px">时区选择</label>
<el-select
v-model="filter.time_zone"
class="m-2"
placeholder="默认选中北京时区"
style="width: 200px">
<el-option
v-for="item in options_time_zone"
:key="item.value"
:label="item.label"
:value="item.value" />
</el-select>
</div>
const options_time_zone = [{
label: "零时区(UTC)--7.5°W~7.5°E",
value: 0,
},{
label: "东一区(UTC+1)--7.5°E~22.5°E",
value: +1,
},{
label: "东二区(UTC+2)--22.5°E~37.5°E",
value: +2,
},{
label: "东三区(UTC+3)--37.5°E~52.5°E",
value: +3,
},{
label: "东四区(UTC+4)--52.5°E~67.5°E",
value: +4,
},{
label: "东四五(UTC+5)--67.5°E~82.5°E",
value: +5,
},{
label: "东六区(UTC+6)--82.5°E~97.5°E",
value: +6,
},{
label: "东七区(UTC+7)--97.5°E~112.5°E",
value: +7,
},{
label: "东八区(UTC+8)--112.5°E~127.5°E",
value: +8,
},{
label: "东九区(UTC+9)--127.5°E~142.5°E",
value: +9,
},{
label: "东十区(UTC+10)--142.5°E~157.5°E",
value: +10,
},{
label: "东十一区(UTC+11)--157.5°E~172.5°E",
value: +11,
},{
label: "东十二区(UTC+12)--172.5°E~172.5°W",
value: 12,
},{
label: "西十二区(UTC-12)--172.5°E~172.5°W",
value: -12,
},{
label: "西十一区(UTC-11)--172.5°W~157.5°W",
value: -11,
},{
label: "西十区(UTC-10)--157.5°W~142.5°W",
value: -10,
},{
label: "西九区(UTC-9)--142.5°W~127.5°W",
value: -9,
},{
label: "西八区(UTC-8)--127.5°W~112.5°W",
value: -8,
},{
label: "西七区(UTC-7)--112.5°W~97.5°W",
value: -7,
},{
label: "西六区(UTC-6)--97.5°W~82.5°W",
value: -6,
},{
label: "西五区(UTC-5)--82.5°W~67.5°W",
value: -5,
},{
label: "西四区(UTC-4)--67.5°W~52.5°W",
value: -4,
},{
label: "西三区(UTC-3)--52.5°W~37.5°W",
value: -3,
},{
label: "西二区(UTC-2)--37.5°W~22.5°W",
value: -2,
},{
label: "西一区(UTC-1)--22.5°W~7.5°W",
value: -1,
}];
import moment from 'moment';
filter.start_time = moment(time[0]).unix();
filter.end_time = moment(time[1]).unix();
var begin_timestamp = filter.start_time
var end_timestamp = filter.end_time;
let diff = 0;
let local_zone = -new Date().getTimezoneOffset()/60;
//都是东区
if(local_zone > 0 && filter.time_zone > 0 && local_zone > filter.time_zone) {
diff = (local_zone - filter.time_zone);
begin_timestamp = begin_timestamp + diff * 60 * 60;
end_timestamp = end_timestamp + diff * 60 * 60;
} else if(local_zone > 0 && filter.time_zone > 0 && local_zone < filter.time_zone) {
diff = (filter.time_zone - local_zone);
begin_timestamp = begin_timestamp - diff * 60 * 60;
end_timestamp = end_timestamp - diff * 60 * 60;
//都是西区
} else if(local_zone < 0 && filter.time_zone < 0 && local_zone > filter.time_zone) {
diff = (local_zone - filter.time_zone);
begin_timestamp = begin_timestamp + diff * 60 * 60;
end_timestamp = end_timestamp + diff * 60 * 60;
} else if(local_zone < 0 && filter.time_zone < 0 && local_zone < filter.time_zone){
diff = (filter.time_zone - local_zone);
begin_timestamp = begin_timestamp - diff * 60 * 60;
end_timestamp = end_timestamp - diff * 60 * 60;
// 西区 ==> 东区
} else if(local_zone < 0 && filter.time_zone > 0) {
diff = filter.time_zone - local_zone;
begin_timestamp = begin_timestamp - diff * 60 * 60;
end_timestamp = end_timestamp - diff * 60 * 60;
// 东区 ==> 西区
} else if(local_zone > 0 && filter.time_zone < 0) {
diff = local_zone - filter.time_zone;
begin_timestamp = begin_timestamp + diff * 60 * 60;
end_timestamp = end_timestamp + diff * 60 * 60;
} else if(local_zone == 0 && filter.time_zone > 0){
diff = filter.time_zone;
begin_timestamp = begin_timestamp - diff * 60 * 60;
end_timestamp = end_timestamp - diff * 60 * 60;
} else if(local_zone == 0 && filter.time_zone < 0) {
diff = - filter.time_zone;
begin_timestamp = begin_timestamp + diff * 60 * 60;
end_timestamp = end_timestamp + diff * 60 * 60;
}
三、时区数据测试工具
时间戳测试工具,可以任意切换时区,十分方便
点击此处进入
总结
提示:这里对文章进行总结:
以上只是粗略的记录了一下海外时区不同,服务器地址部署时区不同,导致的数据无法正确查询的问题。
这个时间戳转换工具,极力推荐,可以大大的减轻了测试过程中的繁琐转换。