🚀 Socket Launch Week Day 5:Introducing Repository Access Permissions and Custom Roles.Learn more
Sign In

wft-utils

Package Overview
Dependencies
Maintainers
1
Versions
85
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

wft-utils - npm Package Compare versions

Comparing version
1.50.0
to
1.51.0
+1
-1
package.json
{
"name": "wft-utils",
"version": "1.50.0",
"version": "1.51.0",
"description": "The commonly used tool functions in daily development",

@@ -5,0 +5,0 @@ "main": "index.js",

@@ -266,2 +266,39 @@ /**

return result
}
/**
* 计算从00点开始算起,到某个时间点的秒数
* @param timeStr
* @returns
*/
export function getSecondsSinceMidnight(timeStr) {
// 验证输入格式(可选但推荐)
if (!/^\d{1,2}:\d{2}:\d{2}$/.test(timeStr)) {
throw new Error('Invalid time format. Expected "HH:mm:ss"');
}
const [hours, minutes, seconds] = timeStr.split(':').map(Number);
// 可选:校验数值范围
if (hours < 0 || hours > 23 || minutes < 0 || minutes > 59 || seconds < 0 || seconds > 59) {
throw new Error('Invalid time values');
}
return hours * 3600 + minutes * 60 + seconds;
}
/**
* 将秒数转换为HH:mm:ss格式(0秒 -> 00:00:00)
* 搭配上面的getSecondsSinceMidnight方法 反向解析
* @param seconds
* @returns
*/
export function secondsToTime(seconds) {
// 可选:限制在一天范围内(0 ~ 86399)
if (seconds < 0 || seconds > 86399) {
throw new Error('Seconds must be between 0 and 86399 (inclusive)');
}
const hours = Math.floor(seconds / 3600);
const minutes = Math.floor((seconds % 3600) / 60);
const secs = seconds % 60;
// 补零到两位
const pad = (num) => String(num).padStart(2, '0');
return `${pad(hours)}:${pad(minutes)}:${pad(secs)}`;
}