You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

70 lines
2.2 KiB
JavaScript

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.

// timePointUtils.js
class TimePointUtils {
constructor() {
this.timePoints = this.generateTimePoints();
}
// 生成时间点数组,格式如下:[{"HHmm":"0000","package":0},{"HHmm":"0005","package":1},{"HHmm":"0010","package":2},...]
generateTimePoints() {
const points = [];
for (let hour = 0; hour < 24; hour++) {
for (let minute = 0; minute < 60; minute += 5) {
const hh = hour.toString().padStart(2, '0');
const mm = minute.toString().padStart(2, '0');
const packageNum = hour * 12 + minute / 5;
points.push({
HHmm: hh + mm,
package: packageNum
});
}
}
return points;
}
// 通过HHmm获取package编号
getPackageByHHmm(hhmm) {
if (!/^([0-1][0-9]|2[0-3])([0-5][0-9])$/.test(hhmm)) {
console.error('Invalid HHmm format, should be like "0000", "2355"');
return null;
}
const point = this.timePoints.find(item => item.HHmm === hhmm);
return point ? point.package : null;
}
// 通过package编号获取HHmm
getHHmmByPackage(packageNum) {
if (packageNum < 0 || packageNum > 287 || !Number.isInteger(packageNum)) {
console.error('Invalid package number, should be integer between 0 and 287');
return null;
}
const point = this.timePoints.find(item => item.package === packageNum);
return point ? point.HHmm : null;
}
/**
* 从yyyy-MM-dd-HH-mm格式的时间字符串中提取HHmm部分
* @param {string} datetime - 格式为"yyyy-MM-dd-HH-mm"的时间字符串,例如"2025-06-27-10-40"
* @returns {string} 返回HHmm格式的时间字符串例如"1040"
*/
getHHmmFromDateTime(datetime) {
// 验证输入格式
if (!/^\d{4}-\d{2}-\d{2}-\d{2}-\d{2}$/.test(datetime)) {
console.error('Invalid datetime format, should be "yyyy-MM-dd-HH-mm"');
return null;
}
// 分割字符串获取小时和分钟部分
const parts = datetime.split('-');
const hours = parts[3]; // 小时部分
const minutes = parts[4]; // 分钟部分
// 组合成HHmm格式
return hours + minutes;
}
}
// 导出单例
const timePointUtils = new TimePointUtils();
export default timePointUtils;