{"record":{"id":"1c7111fe27c5f31d","repo":"jackwener/OpenCLI","slug":"input","errorCode":null,"errorMessage":"无效的时间格式: \"${input}\"","messagePattern":"无效的时间格式: \"(.+?)\"","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"clis/douyin/_shared/timing.js","lineNumber":20,"sourceCode":"const MAX_OFFSET = 14 * 86400; // 14 days\nexport function validateTiming(unixSeconds) {\n    if (!Number.isFinite(unixSeconds))\n        throw new Error(`无效的时间戳: ${unixSeconds}`);\n    const now = Math.floor(Date.now() / 1000);\n    if (unixSeconds < now + MIN_OFFSET)\n        throw new Error(`定时发布时间必须在至少 2 小时后`);\n    if (unixSeconds > now + MAX_OFFSET)\n        throw new Error(`定时发布时间不能超过 14 天`);\n}\nexport function toUnixSeconds(input) {\n    if (typeof input === 'number')\n        return input;\n    if (/^\\d+$/.test(input)) {\n        return Number(input);\n    }\n    const ms = new Date(input).getTime();\n    if (isNaN(ms))\n        throw new Error(`无效的时间格式: \"${input}\"`);\n    return Math.floor(ms / 1000);\n}\n","sourceCodeStart":2,"sourceCodeEnd":23,"githubUrl":"https://github.com/jackwener/OpenCLI/blob/49907e53dc3ade5c223ff0c4c2c2785687cec4e6/clis/douyin/_shared/timing.js#L2-L23","documentation":"toUnixSeconds converts a time input (Date object, numeric timestamp string, or date string) into Unix seconds. It throws this error when the input is neither a Date, an all-digit numeric string, nor a string parseable by `new Date()` — i.e. the parsed milliseconds are NaN.","triggerScenarios":"Calling toUnixSeconds (directly or via timing helpers in clis/douyin/_shared/timing.js) with a non-date string such as \"\", \"yesterday\", \"2024/13/45\", \"15:30\" (time-only in some engines returns Invalid Date), or a malformed config value.","commonSituations":"Reading a publish-time or schedule value from config/env that was mistyped; passing a localized date string the Node version can't parse; passing an empty string from an unset environment variable.","solutions":["Print and correct the input string to a format `new Date()` accepts (ISO 8601 like 2024-01-15T10:30:00Z)","Pass a numeric epoch string (pure digits) or a Date object instead of a freeform string","Ensure env/config values are non-empty; trim whitespace before calling","Pre-parse manually, e.g. dayjs(input).unix(), for locale-specific formats"],"exampleFix":"// before\nconst ts = toUnixSeconds(process.env.PUBLISH_AT); // \"\" -> throws\n// after\nconst raw = (process.env.PUBLISH_AT || '').trim();\nif (!raw) throw new Error('PUBLISH_AT is required');\nconst ts = toUnixSeconds(raw);","handlingStrategy":"validation","validationCode":"function isParsableDate(input) {\n  if (input instanceof Date) return !isNaN(input.getTime());\n  if (typeof input === 'number') return Number.isFinite(input);\n  if (typeof input === 'string' && /^\\d+$/.test(input.trim())) return true;\n  return typeof input === 'string' && input.trim() !== '' && !isNaN(new Date(input).getTime());\n}","typeGuard":"function isValidTimeInput(input) {\n  return input instanceof Date ||\n    (typeof input === 'string' && (/^\\d+$/.test(input) || !isNaN(new Date(input).getTime()))) ||\n    typeof input === 'number';\n}","tryCatchPattern":"let ts;\ntry { ts = toUnixSeconds(input); }\ncatch (e) { console.error(`bad time input: ${input}`); ts = Math.floor(Date.now() / 1000); }","preventionTips":["Prefer ISO 8601 strings or epoch numbers for time inputs","Trim and validate env/config values before parsing","Add a unit test covering empty and locale-format inputs","Centralize date parsing in one helper instead of ad-hoc new Date() calls"],"tags":["date-parsing","validation","input-format"],"backgroundTag":"invalid-date-string","analyzedSha":"49907e53dc3ade5c223ff0c4c2c2785687cec4e6","analyzedAt":"2026-08-29T08:14:47.543Z","schemaVersion":2},"datasetVersion":"2026-08-29T12:17:43.993Z"}