This commit is contained in:
Louis Lam 2022-10-10 20:48:11 +08:00
parent 539683f8e9
commit c1ccaa7a9f
8 changed files with 93 additions and 45 deletions

View file

@ -648,8 +648,14 @@ module.exports.send403 = (res, msg = "") => {
};
function timeObjectConvertTimezone(obj, timezone, timeObjectToUTC = true) {
// e.g. +08:00
let offsetString = dayjs().tz(timezone).format("Z");
let offsetString;
if (timezone) {
offsetString = dayjs().tz(timezone).format("Z");
} else {
offsetString = dayjs().format("Z");
}
let hours = parseInt(offsetString.substring(1, 3));
let minutes = parseInt(offsetString.substring(4, 6));
@ -680,10 +686,22 @@ function timeObjectConvertTimezone(obj, timezone, timeObjectToUTC = true) {
return obj;
}
module.exports.timeObjectToUTC = (obj, timezone) => {
/**
*
* @param {object} obj
* @param {string} timezone
* @returns {object}
*/
module.exports.timeObjectToUTC = (obj, timezone = undefined) => {
return timeObjectConvertTimezone(obj, timezone, true);
};
module.exports.timeObjectToLocal = (obj, timezone) => {
/**
*
* @param {object} obj
* @param {string} timezone
* @returns {object}
*/
module.exports.timeObjectToLocal = (obj, timezone = undefined) => {
return timeObjectConvertTimezone(obj, timezone, false);
};