0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

golangとsecretmanager

Last updated at Posted at 2025-07-23
// 判断 JST 时间字符串 (yyyymmddhhmmssSSS) 是否在当前 UTC 时间的过去 3 分钟内
function isWithinLast3Minutes(jstTimeStr) {
    try {
        // 验证输入格式 (必须是17位数字)
        if (!/^\d{17}$/.test(jstTimeStr)) {
            console.error("格式错误: 需要17位数字格式 yyyymmddhhmmssSSS");
            return false;
        }

        // 解析 JST 时间字符串
        const year = parseInt(jstTimeStr.substring(0, 4));
        const month = parseInt(jstTimeStr.substring(4, 6)) - 1; // 月份从0开始
        const day = parseInt(jstTimeStr.substring(6, 8));
        const hour = parseInt(jstTimeStr.substring(8, 10));
        const minute = parseInt(jstTimeStr.substring(10, 12));
        const second = parseInt(jstTimeStr.substring(12, 14));
        const millisecond = parseInt(jstTimeStr.substring(14, 17));

        // 创建 JST 时间对象 (视为 UTC+9)
        const jstDate = new Date(Date.UTC(year, month, day, hour, minute, second, millisecond));
        
        // 验证日期有效性
        if (isNaN(jstDate.getTime())) {
            console.error("日期无效");
            return false;
        }

        // 转换为 UTC 时间 (减去9小时)
        const utcDate = new Date(jstDate.getTime() - (9 * 60 * 60 * 1000));
        
        // 获取当前 UTC 时间
        const currentUtc = new Date();
        
        // 计算时间差 (毫秒)
        const timeDiff = currentUtc.getTime() - utcDate.getTime();
        
        // 检查是否在过去3分钟内 (0 ≤ 差值 ≤ 180,000毫秒)
        return timeDiff >= 0 && timeDiff <= 180000;
        
    } catch (e) {
        console.error("处理失败:", e);
        return false;
    }
}

/****************** 使用示例 ******************/
// 示例1: 当前时间测试 (假设当前UTC时间为2025-08-14T16:25:30.500Z)
const sampleTime = "20250815012530500"; // JST 01:25:30.500 = UTC 16:25:30.500

// 在Postman Tests中使用:
pm.test("时间有效性检查", () => {
    const isValid = isWithinLast3Minutes(sampleTime);
    pm.expect(isValid).to.be.true;
    
    // 存储结果到环境变量
    pm.environment.set("is_valid_time", isValid);
});

// 控制台输出结果
console.log("时间是否有效:", isWithinLast3Minutes(sampleTime));
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?