Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
actions-user committed Feb 4, 2025
2 parents e8eb161 + 0602ce6 commit 94fa882
Showing 1 changed file with 30 additions and 8 deletions.
38 changes: 30 additions & 8 deletions lib/db/getSiteData.js
Original file line number Diff line number Diff line change
Expand Up @@ -704,13 +704,17 @@ function convertToUTC(dateStr, timeZone = 'Asia/Shanghai') {

// 亚洲地区
'Asia/Shanghai': 8, // 中国
'Asia/Taipei': 8, // 台湾
'Asia/Tokyo': 9, // 日本
'Asia/Seoul': 9, // 韩国
'Asia/Kolkata': 5.5, // 印度
'Asia/Jakarta': 7, // 印尼
'Asia/Singapore': 8, // 新加坡
'Asia/Hong_Kong': 8, // 香港
'Asia/Bangkok': 7, // 泰国
'Asia/Dubai': 4, // 阿联酋
'Asia/Tehran': 3.5, // 伊朗
'Asia/Riyadh': 3, // 沙特阿拉伯

// 欧洲地区
'Europe/London': 0, // 英国(GMT)
Expand Down Expand Up @@ -738,20 +742,38 @@ function convertToUTC(dateStr, timeZone = 'Asia/Shanghai') {
'Pacific/Auckland': 13, // 新西兰
'Pacific/Fiji': 12, // 斐济

// 中东地区
'Asia/Dubai': 4, // 阿联酋
'Asia/Tehran': 3.5, // 伊朗
'Asia/Riyadh': 3, // 沙特阿拉伯

// 北极与南极
'Antarctica/Palmer': -3, // 南极洲帕尔默
'Antarctica/McMurdo': 13 // 南极洲麦克默多
}

// 预设每个大洲的默认时区
const continentDefaults = {
Asia: 'Asia/Shanghai',
Europe: 'Europe/London',
America: 'America/New_York',
Africa: 'Africa/Cairo',
Australia: 'Australia/Sydney',
Pacific: 'Pacific/Auckland',
Antarctica: 'Antarctica/Palmer',
UTC: 'UTC'
}

// 获取目标时区的偏移量(以小时为单位)
const offsetHours = timeZoneOffsets[timeZone]
let offsetHours = timeZoneOffsets[timeZone]

// 未被支持的时区采用兼容
if (offsetHours === undefined) {
throw new Error(`Unsupported time zone: ${timeZone}`)
// 获取时区所属大洲("Continent/City" -> "Continent")
const continent = timeZone.split('/')[0]

// 选择该大洲的默认时区
const fallbackZone = continentDefaults[continent] || 'UTC'
offsetHours = timeZoneOffsets[fallbackZone]

console.warn(
`Warning: Unsupported time zone "${timeZone}". Using default "${fallbackZone}" for continent "${continent}".`
)
}

// 将日期字符串转换为本地时间的 Date 对象
Expand All @@ -766,7 +788,7 @@ function convertToUTC(dateStr, timeZone = 'Asia/Shanghai') {
}

// 辅助函数:生成指定日期时间的时间戳(基于目标时区)
function getTimestamp(date, time ='00:00', time_zone) {
function getTimestamp(date, time = '00:00', time_zone) {
if (!date) return null
return convertToUTC(`${date} ${time}:00`, time_zone).getTime()
}
Expand Down

0 comments on commit 94fa882

Please sign in to comment.