dsh-session-guard 0.1.0 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/time.js CHANGED
@@ -1,13 +1,23 @@
1
1
  /**
2
2
  * dsh-session-guard — 时间判定(纯函数,零依赖,可单测)。
3
3
  *
4
- * 高峰/周末识别必须基于「配置时区」的墙钟,而不是裸 UTC(D1):
5
- * 周末判断用 `Intl.DateTimeFormat(timeZone)` 投影,避免 off-peak 那种
6
- * 用 `getUTCDay()` 导致北京周末边界错 8 小时的 bug。
4
+ * 峰谷判定必须基于 DeepSeek 官方计费基准——北京时间(UTC+8),
5
+ * 而不是用户配置的本地时区。用硬编码 BILLING_TIMEZONE = 'Asia/Shanghai'。
6
+ *
7
+ * 周末判定基于用户配置时区(settings.timezone),因为"周末"是用户本地概念。
8
+ *
9
+ * 两者都通过 `Intl.DateTimeFormat(timeZone)` 投影,避免裸 `getUTCDay()`
10
+ * 导致北京周末边界错 8 小时的 bug。
7
11
  *
8
12
  * 高峰时段为左闭右开 [start, end);跨午夜窗口(start > end)安全。
9
13
  */
10
14
 
15
+ /**
16
+ * DeepSeek 峰谷计费基准时区(固定北京时间 UTC+8)。
17
+ * 峰谷窗口判定必须用此时区,不受用户 timezone 配置影响。
18
+ */
19
+ export const BILLING_TIMEZONE = 'Asia/Shanghai'
20
+
11
21
  /** 解析 "HH:mm" → 当日分钟数,非法返回 null。 */
12
22
  export function parseHHMM(s) {
13
23
  const m = /^(\d{1,2}):(\d{2})$/.exec(String(s).trim())
@@ -69,6 +79,10 @@ export function isInPeak(wc, windows) {
69
79
 
70
80
  /**
71
81
  * 主判定:此刻是否应触发高峰暂停。
82
+ *
83
+ * - 峰谷判定:固定使用 BILLING_TIMEZONE(北京时间),与 DeepSeek 官方计费一致;
84
+ * - 周末判定:使用 settings.timezone(用户本地时区),因为周末是用户本地概念。
85
+ *
72
86
  * @param {object} settings { enabled, weekendMode, timezone, peakWindows }
73
87
  * @param {Date} date
74
88
  * @returns {{pause:boolean, reason:string}}
@@ -76,12 +90,14 @@ export function isInPeak(wc, windows) {
76
90
  */
77
91
  export function shouldPause(settings, date) {
78
92
  if (!settings || settings.enabled !== true) return { pause: false, reason: 'disabled' }
79
- const wc = wallClock(settings.timezone, date)
80
- // 周末模式:识别周末 无视峰谷,畅快跑(D6)。
81
- if (settings.weekendMode === true && isWeekend(wc.weekday)) {
93
+ // 周末判定:用用户配置时区(用户本地的周末)
94
+ const wcUser = wallClock(settings.timezone, date)
95
+ if (settings.weekendMode === true && isWeekend(wcUser.weekday)) {
82
96
  return { pause: false, reason: 'weekend' }
83
97
  }
84
- if (isInPeak(wc, settings.peakWindows || [])) {
98
+ // 峰谷判定:固定北京时间(DeepSeek 官方计费基准)
99
+ const wcBilling = wallClock(BILLING_TIMEZONE, date)
100
+ if (isInPeak(wcBilling, settings.peakWindows || [])) {
85
101
  return { pause: true, reason: 'peak' }
86
102
  }
87
103
  return { pause: false, reason: 'off-peak' }