mirror of
https://github.com/d0zingcat/NotionNext.git
synced 2026-06-07 23:16:52 +00:00
定时发布,时区
This commit is contained in:
@@ -368,7 +368,7 @@ function handleDataBeforeReturn(db) {
|
|||||||
p.date.time_zone
|
p.date.time_zone
|
||||||
)
|
)
|
||||||
console.log(
|
console.log(
|
||||||
'[定时任务] 隐藏--> 文章:',
|
'[定时发布] 隐藏--> 文章:',
|
||||||
p.title,
|
p.title,
|
||||||
'当前时间戳:',
|
'当前时间戳:',
|
||||||
currentTimestamp,
|
currentTimestamp,
|
||||||
@@ -688,13 +688,87 @@ function isInRange(title, date = {}) {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将指定时区的日期字符串转换为 UTC 时间
|
||||||
|
* @param {string} dateStr - 日期字符串,格式为 YYYY-MM-DD HH:mm:ss
|
||||||
|
* @param {string} timeZone - 时区名称(如 "Asia/Shanghai")
|
||||||
|
* @returns {Date} - 转换后的 Date 对象(UTC 时间)
|
||||||
|
*/
|
||||||
|
function convertToUTC(dateStr, timeZone) {
|
||||||
|
// 维护一个时区偏移映射(以小时为单位)
|
||||||
|
const timeZoneOffsets = {
|
||||||
|
// UTC 基础
|
||||||
|
UTC: 0,
|
||||||
|
'Etc/GMT': 0,
|
||||||
|
'Etc/GMT+0': 0,
|
||||||
|
|
||||||
|
// 亚洲地区
|
||||||
|
'Asia/Shanghai': 8, // 中国
|
||||||
|
'Asia/Tokyo': 9, // 日本
|
||||||
|
'Asia/Seoul': 9, // 韩国
|
||||||
|
'Asia/Kolkata': 5.5, // 印度
|
||||||
|
'Asia/Jakarta': 7, // 印尼
|
||||||
|
'Asia/Singapore': 8, // 新加坡
|
||||||
|
'Asia/Hong_Kong': 8, // 香港
|
||||||
|
'Asia/Bangkok': 7, // 泰国
|
||||||
|
|
||||||
|
// 欧洲地区
|
||||||
|
'Europe/London': 0, // 英国(GMT)
|
||||||
|
'Europe/Paris': 1, // 法国(CET)
|
||||||
|
'Europe/Berlin': 1, // 德国
|
||||||
|
'Europe/Moscow': 3, // 俄罗斯
|
||||||
|
'Europe/Amsterdam': 1, // 荷兰
|
||||||
|
|
||||||
|
// 美洲地区
|
||||||
|
'America/New_York': -5, // 美国东部(EST)
|
||||||
|
'America/Chicago': -6, // 美国中部(CST)
|
||||||
|
'America/Denver': -7, // 美国山区时间(MST)
|
||||||
|
'America/Los_Angeles': -8, // 美国西部(PST)
|
||||||
|
'America/Sao_Paulo': -3, // 巴西
|
||||||
|
'America/Argentina/Buenos_Aires': -3, // 阿根廷
|
||||||
|
|
||||||
|
// 非洲地区
|
||||||
|
'Africa/Johannesburg': 2, // 南非
|
||||||
|
'Africa/Cairo': 2, // 埃及
|
||||||
|
'Africa/Nairobi': 3, // 肯尼亚
|
||||||
|
|
||||||
|
// 大洋洲地区
|
||||||
|
'Australia/Sydney': 10, // 澳大利亚东部
|
||||||
|
'Australia/Perth': 8, // 澳大利亚西部
|
||||||
|
'Pacific/Auckland': 13, // 新西兰
|
||||||
|
'Pacific/Fiji': 12, // 斐济
|
||||||
|
|
||||||
|
// 中东地区
|
||||||
|
'Asia/Dubai': 4, // 阿联酋
|
||||||
|
'Asia/Tehran': 3.5, // 伊朗
|
||||||
|
'Asia/Riyadh': 3, // 沙特阿拉伯
|
||||||
|
|
||||||
|
// 北极与南极
|
||||||
|
'Antarctica/Palmer': -3, // 南极洲帕尔默
|
||||||
|
'Antarctica/McMurdo': 13 // 南极洲麦克默多
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取目标时区的偏移量(以小时为单位)
|
||||||
|
const offsetHours = timeZoneOffsets[timeZone]
|
||||||
|
if (offsetHours === undefined) {
|
||||||
|
throw new Error(`Unsupported time zone: ${timeZone}`)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 将日期字符串转换为本地时间的 Date 对象
|
||||||
|
const localDate = new Date(`${dateStr.replace(' ', 'T')}Z`)
|
||||||
|
if (isNaN(localDate.getTime())) {
|
||||||
|
throw new Error(`Invalid date string: ${dateStr}`)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 计算 UTC 时间的时间戳
|
||||||
|
const utcTimestamp = localDate.getTime() - offsetHours * 60 * 60 * 1000
|
||||||
|
return new Date(utcTimestamp)
|
||||||
|
}
|
||||||
|
|
||||||
// 辅助函数:生成指定日期时间的时间戳(基于目标时区)
|
// 辅助函数:生成指定日期时间的时间戳(基于目标时区)
|
||||||
function getTimestamp(date, time, time_zone) {
|
function getTimestamp(date, time, time_zone) {
|
||||||
if (!date) return null
|
if (!date) return null
|
||||||
const dateTimeString = `${date}T${time}:00` // 拼接日期时间
|
return convertToUTC(`${date} ${time}:00`, time_zone).getTime()
|
||||||
return new Date(
|
|
||||||
new Date(dateTimeString).toLocaleString('en-US', { timeZone: time_zone })
|
|
||||||
).getTime()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user