/**
* 获取指定时区的时间
* @param offset 时区
* @returns {Date} 指定时区的 时间信息
*/
module.exports.getZoneTime = (offset)=> {
let localtime = new Date();
let localmesc = localtime.getTime();
let localOffset = localtime.getTimezoneOffset() * 60000;
let utc = localOffset + localmesc;
let calctime = utc + (3600000 * offset);
return new Date(calctime);
};
带验证 方法 参考Java实现
module.exports.checkTz = (tz) => {
if (!tz) {
tz = "+0000";
}
if ('-' != tz.charAt(0) && '+' != tz.charAt(0)) {
tz = "+" + tz;
}
let hours, minutes, seconds;
switch (tz.length) {
case 2:
tz = tz.charAt(0) + "0" + tz.charAt(1);
case 3:
hours = parseNumber(tz, 1, false);
minutes = 0;
seconds = 0;
break;
case 5:
hours = parseNumber(tz, 1, false);
minutes = parseNumber(tz, 3, false);
seconds = 0;
break;
case 6:
hours = parseNumber(tz, 1, false);
minutes = parseNumber(tz, 4, true);
seconds = 0;
break;
case 7:
hours = parseNumber(tz, 1, false);
minutes = parseNumber(tz, 3, false);
seconds = parseNumber(tz, 5, false);
break;
case 9:
hours = parseNumber(tz, 1, false);
minutes = parseNumber(tz, 4, true);
seconds = parseNumber(tz, 7, true);
break;
default:
throw new Error("Invalid ID for ZoneOffset, invalid format: " + tz);
}
let first = tz.charAt(0);
if (first != '+' && first != '-') {
throw new Error("Invalid ID for ZoneOffset, plus/minus not found when expected: " + tz);
}
if (first == '-') {
return ofHoursMinutesSeconds(-hours, -minutes, -seconds);
} else {
return ofHoursMinutesSeconds(hours, minutes, seconds);
}
};
function parseNumber(tz, pos, precededByColon) {
if (precededByColon && tz.charAt(pos - 1) != ":") {
throw new Error("Invalid ID for ZoneOffset, colon not found when expected: " + tz);
}
let ch1 = tz.charCodeAt(pos);
let ch2 = tz.charCodeAt(pos + 1);
if (String.fromCharCode(ch1) < "0" || String.fromCharCode(ch1) > "9" ||
String.fromCharCode(ch2) < "0" || String.fromCharCode(ch2) > "9") {
throw new Error("Invalid ID for ZoneOffset, non numeric characters found: " + tz);
}
return (ch1 - 48) * 10 + (ch2 - 48);
}
function ofHoursMinutesSeconds(hours, minutes, seconds) {
validate(hours, minutes, seconds);
let totalS = totalSeconds(hours, minutes, seconds);
return ofTotalSeconds(totalS);
}
function ofTotalSeconds(totalS) {
let localtime = new Date();
let localmesc = localtime.getTime();
let localOffset = localtime.getTimezoneOffset() * 60000;
let utc = localOffset + localmesc;
let calctime = utc + (totalS*1000);
return new Date(calctime);
}
function totalSeconds(hours, minutes, seconds) {
return hours * SECONDS_PER_HOUR + minutes * SECONDS_PER_MINUTE + seconds;
}
function validate(hours, minutes, seconds) {
if (hours < -18 || hours > 18) {
throw new Error("Zone offset hours not in valid range: value " + hours +
" is not in the range -18 to 18");
}
if (hours > 0) {
if (minutes < 0 || seconds < 0) {
throw new Error("Zone offset minutes and seconds must be positive because hours is positive");
}
} else if (hours < 0) {
if (minutes > 0 || seconds > 0) {
throw new Error("Zone offset minutes and seconds must be negative because hours is negative");
}
} else if ((minutes > 0 && seconds < 0) || (minutes < 0 && seconds > 0)) {
throw new Error("Zone offset minutes and seconds must have the same sign");
}
if (Math.abs(minutes) > 59) {
throw new Error("Zone offset minutes not in valid range: abs(value) " +
Math.abs(minutes) + " is not in the range 0 to 59");
}
if (Math.abs(seconds) > 59) {
throw new Error("Zone offset seconds not in valid range: abs(value) " +
Math.abs(seconds) + " is not in the range 0 to 59");
}
if (Math.abs(hours) == 18 && (Math.abs(minutes) > 0 || Math.abs(seconds) > 0)) {
throw new Error("Zone offset not in valid range: -18:00 to +18:00");
}
}