agri-app/utils/TimeUtil.js

105 lines
4.5 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

/**
* 时间差判断工具类
* 核心功能:判断目标时间与当前时间的差值是否小于指定秒数
* 支持的目标时间格式毫秒级时间戳、秒级时间戳、Date对象、合法时间字符串如 "2026-02-12 10:00:00"
*/
class TimeUtil {
/**
* 判断目标时间距离当前时间是否小于指定秒数
* @param {String|Number|Date} targetTime 目标时间(支持多格式)
* @param {Number} seconds 指定秒数(需为非负数字)
* @returns {Boolean} true=时间差小于指定秒数false=时间差大于/等于指定秒数或参数异常
* @throws {Error} 当秒数参数不合法时抛出明确异常
*/
static isLessThanSpecifiedSeconds(targetTime, seconds) {
// 1. 严格校验秒数参数
if (typeof seconds !== 'number' || isNaN(seconds) || seconds < 0) {
throw new Error(`指定秒数不合法:${seconds},请传入非负数字(如 90、60`);
}
// 2. 转换目标时间为毫秒级时间戳(内部私有方法处理多格式兼容)
const targetTimestamp = this._parseToMillisecondTimestamp(targetTime);
if (targetTimestamp === null) return false; // 时间格式不合法时返回false
// 3. 计算时间差(绝对值:兼容目标时间在当前时间之前/之后的场景)
const nowTimestamp = Date.now();
const timeDiffMs = Math.abs(nowTimestamp - targetTimestamp);
// 4. 转换指定秒数为毫秒,判断是否小于(核心逻辑)
const specifiedMs = seconds * 1000;
return timeDiffMs < specifiedMs;
}
/**
* 私有方法:统一转换任意时间格式为毫秒级时间戳
* @param {String|Number|Date} time 待转换的时间
* @returns {Number|null} 合法返回毫秒级时间戳不合法返回null
*/
static _parseToMillisecondTimestamp(time) {
let timestamp;
// 情况1传入的是数字秒级/毫秒级时间戳)
if (typeof time === 'number') {
// 区分秒级戳10位和毫秒级戳13位
timestamp = time.toString().length <= 10 ? time * 1000 : time;
// 校验时间戳有效性
if (isNaN(timestamp) || timestamp < 0) {
console.error(`时间戳不合法:${time},需为非负数字`);
return null;
}
}
// 情况2传入的是Date对象
else if (time instanceof Date) {
timestamp = time.getTime();
if (isNaN(timestamp)) {
console.error(`Date对象不合法${time}`);
return null;
}
}
// 情况3传入的是时间字符串如 "2026-02-12 10:00:00"、"2026/02/12"
else if (typeof time === 'string') {
const date = new Date(time);
timestamp = date.getTime();
if (isNaN(timestamp)) {
console.error(`时间字符串格式不合法:${time},请使用如 "2026-02-12 10:00:00" 的格式`);
return null;
}
}
// 情况4不支持的类型
else {
console.warn(`不支持的时间类型:${typeof time}仅支持数字、Date对象、字符串`);
return null;
}
return timestamp;
}
}
export default TimeUtil;
// -------------------------- 测试用例 --------------------------
// // 测试1目标时间是90秒前判断是否小于100秒 → true
// const test1 = TimeUtil.isLessThanSpecifiedSeconds(Date.now() - 90 * 1000, 100);
// console.log('测试1', test1); // true
//
// // 测试2目标时间是100秒前判断是否小于90秒 → false
// const test2 = TimeUtil.isLessThanSpecifiedSeconds(Date.now() - 100 * 1000, 90);
// console.log('测试2', test2); // false
//
// // 测试3传入时间字符串判断是否小于60秒 → false
// const test3Time = "2026-02-12 09:00:00";
// const test3 = TimeUtil.isLessThanSpecifiedSeconds(test3Time, 60);
// console.log('测试3', test3); // false
//
// // 测试4传入秒级时间戳1740000000 → 转换为毫秒级判断是否小于30秒
// const test4 = TimeUtil.isLessThanSpecifiedSeconds(1740000000, 30);
// console.log('测试4', test4); // 取决于当前时间大概率false
//
// // 测试5非法秒数会抛出异常
// try {
// TimeUtil.isLessThanSpecifiedSeconds(Date.now(), -10);
// } catch (e) {
// console.log('测试5异常', e.message); // 指定秒数不合法:-10请传入非负数字
// }