Compare commits
18 Commits
04322894b5
...
v1.0.11
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1aade62ddb | ||
|
|
31a02625e8 | ||
|
|
16c262f2c4 | ||
|
|
dfa93723ef | ||
|
|
59900bf294 | ||
|
|
1ab38c4c87 | ||
|
|
b47db3cf05 | ||
|
|
32c21f82eb | ||
|
|
5afb0e243d | ||
|
|
a80e4ec00d | ||
|
|
7beb867bd5 | ||
|
|
d11d6bf5b1 | ||
|
|
cfb45a03a2 | ||
|
|
49bb137388 | ||
|
|
10501bc0a9 | ||
|
|
f016d7a01c | ||
|
|
fee4ca327a | ||
|
|
60390fcbdf |
7
dist/homeassistant.d.ts
vendored
7
dist/homeassistant.d.ts
vendored
@@ -1,3 +1,4 @@
|
|||||||
|
import { TimeBetween } from "./timehelper";
|
||||||
export type HomeAssistantEntity = {
|
export type HomeAssistantEntity = {
|
||||||
entity_id: string;
|
entity_id: string;
|
||||||
state: string;
|
state: string;
|
||||||
@@ -21,5 +22,11 @@ export interface HomeAssistantDeskPositionResult {
|
|||||||
raw: HomeAssistantEntity;
|
raw: HomeAssistantEntity;
|
||||||
asBoolean: boolean;
|
asBoolean: boolean;
|
||||||
asText: () => string;
|
asText: () => string;
|
||||||
|
standingTime: TimeBetween;
|
||||||
}
|
}
|
||||||
|
export type API_HA_DeskPosition = {
|
||||||
|
position: string;
|
||||||
|
isStanding: boolean;
|
||||||
|
standingTime: string;
|
||||||
|
};
|
||||||
//# sourceMappingURL=homeassistant.d.ts.map
|
//# sourceMappingURL=homeassistant.d.ts.map
|
||||||
2
dist/homeassistant.d.ts.map
vendored
2
dist/homeassistant.d.ts.map
vendored
@@ -1 +1 @@
|
|||||||
{"version":3,"file":"homeassistant.d.ts","sourceRoot":"","sources":["../src/homeassistant.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,mBAAmB,GAAG;IAChC,SAAS,EAAE,MAAM,CAAC;IAClB,KAAK,EAAE,MAAM,CAAC;IACd,UAAU,EAAE;QACV,WAAW,CAAC,EAAE,MAAM,CAAC;QACrB,mBAAmB,CAAC,EAAE,MAAM,CAAC;QAC7B,IAAI,CAAC,EAAE,MAAM,CAAC;QACd,aAAa,CAAC,EAAE,MAAM,CAAC;QACvB,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC;KACxB,CAAC;IACF,YAAY,EAAE,MAAM,CAAC;IACrB,aAAa,EAAE,MAAM,CAAC;IACtB,YAAY,EAAE,MAAM,CAAC;IACrB,OAAO,EAAE;QACP,EAAE,EAAE,MAAM,CAAC;QACX,SAAS,EAAE,MAAM,GAAG,IAAI,CAAC;QACzB,OAAO,EAAE,MAAM,GAAG,IAAI,CAAC;KACxB,CAAC;CACH,CAAC;AAEF,MAAM,WAAW,+BAA+B;IAC9C,GAAG,EAAE,mBAAmB,CAAC;IACzB,SAAS,EAAE,OAAO,CAAC;IACnB,MAAM,EAAE,MAAM,MAAM,CAAC;CACtB"}
|
{"version":3,"file":"homeassistant.d.ts","sourceRoot":"","sources":["../src/homeassistant.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAE3C,MAAM,MAAM,mBAAmB,GAAG;IAChC,SAAS,EAAE,MAAM,CAAC;IAClB,KAAK,EAAE,MAAM,CAAC;IACd,UAAU,EAAE;QACV,WAAW,CAAC,EAAE,MAAM,CAAC;QACrB,mBAAmB,CAAC,EAAE,MAAM,CAAC;QAC7B,IAAI,CAAC,EAAE,MAAM,CAAC;QACd,aAAa,CAAC,EAAE,MAAM,CAAC;QACvB,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC;KACxB,CAAC;IACF,YAAY,EAAE,MAAM,CAAC;IACrB,aAAa,EAAE,MAAM,CAAC;IACtB,YAAY,EAAE,MAAM,CAAC;IACrB,OAAO,EAAE;QACP,EAAE,EAAE,MAAM,CAAC;QACX,SAAS,EAAE,MAAM,GAAG,IAAI,CAAC;QACzB,OAAO,EAAE,MAAM,GAAG,IAAI,CAAC;KACxB,CAAC;CACH,CAAC;AAEF,MAAM,WAAW,+BAA+B;IAC9C,GAAG,EAAE,mBAAmB,CAAC;IACzB,SAAS,EAAE,OAAO,CAAC;IACnB,MAAM,EAAE,MAAM,MAAM,CAAC;IACrB,YAAY,EAAE,WAAW,CAAC;CAC3B;AAED,MAAM,MAAM,mBAAmB,GAAG;IAChC,QAAQ,EAAE,MAAM,CAAC;IACjB,UAAU,EAAE,OAAO,CAAC;IACpB,YAAY,EAAE,MAAM,CAAC;CACtB,CAAC"}
|
||||||
2
dist/index.d.ts
vendored
2
dist/index.d.ts
vendored
@@ -1,5 +1,5 @@
|
|||||||
export * from "./homeassistant";
|
export * from "./homeassistant";
|
||||||
export * as Logger from "./logger";
|
export * as Logger from "./logger";
|
||||||
export * from "./tidal";
|
export * from "./tidal";
|
||||||
export * as Utility from "./utility";
|
export * as TimeHelper from "./timehelper";
|
||||||
//# sourceMappingURL=index.d.ts.map
|
//# sourceMappingURL=index.d.ts.map
|
||||||
2
dist/index.d.ts.map
vendored
2
dist/index.d.ts.map
vendored
@@ -1 +1 @@
|
|||||||
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,iBAAiB,CAAC;AAChC,OAAO,KAAK,MAAM,MAAM,UAAU,CAAC;AACnC,cAAc,SAAS,CAAC;AACxB,OAAO,KAAK,OAAO,MAAM,WAAW,CAAC"}
|
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,iBAAiB,CAAC;AAChC,OAAO,KAAK,MAAM,MAAM,UAAU,CAAC;AACnC,cAAc,SAAS,CAAC;AACxB,OAAO,KAAK,UAAU,MAAM,cAAc,CAAC"}
|
||||||
4
dist/index.js
vendored
4
dist/index.js
vendored
@@ -36,8 +36,8 @@ var __importStar = (this && this.__importStar) || (function () {
|
|||||||
};
|
};
|
||||||
})();
|
})();
|
||||||
Object.defineProperty(exports, "__esModule", { value: true });
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||||||
exports.Utility = exports.Logger = void 0;
|
exports.TimeHelper = exports.Logger = void 0;
|
||||||
__exportStar(require("./homeassistant"), exports);
|
__exportStar(require("./homeassistant"), exports);
|
||||||
exports.Logger = __importStar(require("./logger"));
|
exports.Logger = __importStar(require("./logger"));
|
||||||
__exportStar(require("./tidal"), exports);
|
__exportStar(require("./tidal"), exports);
|
||||||
exports.Utility = __importStar(require("./utility"));
|
exports.TimeHelper = __importStar(require("./timehelper"));
|
||||||
|
|||||||
1
dist/logger.d.ts
vendored
1
dist/logger.d.ts
vendored
@@ -2,4 +2,5 @@ export declare function logError(...args: unknown[]): void;
|
|||||||
export declare function logWarning(...args: unknown[]): void;
|
export declare function logWarning(...args: unknown[]): void;
|
||||||
export declare function logSuccess(...args: unknown[]): void;
|
export declare function logSuccess(...args: unknown[]): void;
|
||||||
export declare function logInfo(...args: unknown[]): void;
|
export declare function logInfo(...args: unknown[]): void;
|
||||||
|
export declare function printNetworkError(error: unknown): void;
|
||||||
//# sourceMappingURL=logger.d.ts.map
|
//# sourceMappingURL=logger.d.ts.map
|
||||||
2
dist/logger.d.ts.map
vendored
2
dist/logger.d.ts.map
vendored
@@ -1 +1 @@
|
|||||||
{"version":3,"file":"logger.d.ts","sourceRoot":"","sources":["../src/logger.ts"],"names":[],"mappings":"AAEA,wBAAgB,QAAQ,CAAC,GAAG,IAAI,EAAE,OAAO,EAAE,QAE1C;AAED,wBAAgB,UAAU,CAAC,GAAG,IAAI,EAAE,OAAO,EAAE,QAE5C;AAED,wBAAgB,UAAU,CAAC,GAAG,IAAI,EAAE,OAAO,EAAE,QAE5C;AAED,wBAAgB,OAAO,CAAC,GAAG,IAAI,EAAE,OAAO,EAAE,QAEzC"}
|
{"version":3,"file":"logger.d.ts","sourceRoot":"","sources":["../src/logger.ts"],"names":[],"mappings":"AAGA,wBAAgB,QAAQ,CAAC,GAAG,IAAI,EAAE,OAAO,EAAE,QAE1C;AAED,wBAAgB,UAAU,CAAC,GAAG,IAAI,EAAE,OAAO,EAAE,QAE5C;AAED,wBAAgB,UAAU,CAAC,GAAG,IAAI,EAAE,OAAO,EAAE,QAE5C;AAED,wBAAgB,OAAO,CAAC,GAAG,IAAI,EAAE,OAAO,EAAE,QAEzC;AAED,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,OAAO,QAY/C"}
|
||||||
16
dist/logger.js
vendored
16
dist/logger.js
vendored
@@ -7,6 +7,8 @@ exports.logError = logError;
|
|||||||
exports.logWarning = logWarning;
|
exports.logWarning = logWarning;
|
||||||
exports.logSuccess = logSuccess;
|
exports.logSuccess = logSuccess;
|
||||||
exports.logInfo = logInfo;
|
exports.logInfo = logInfo;
|
||||||
|
exports.printNetworkError = printNetworkError;
|
||||||
|
const axios_1 = __importDefault(require("axios"));
|
||||||
const chalk_1 = __importDefault(require("chalk"));
|
const chalk_1 = __importDefault(require("chalk"));
|
||||||
function logError(...args) {
|
function logError(...args) {
|
||||||
console.error(chalk_1.default.red("ERROR:"), ...args);
|
console.error(chalk_1.default.red("ERROR:"), ...args);
|
||||||
@@ -20,3 +22,17 @@ function logSuccess(...args) {
|
|||||||
function logInfo(...args) {
|
function logInfo(...args) {
|
||||||
console.info(chalk_1.default.cyan("INFO:"), ...args);
|
console.info(chalk_1.default.cyan("INFO:"), ...args);
|
||||||
}
|
}
|
||||||
|
function printNetworkError(error) {
|
||||||
|
if (axios_1.default.isAxiosError(error)) {
|
||||||
|
logError("Axios error details:", {
|
||||||
|
message: error.message,
|
||||||
|
status: error.response?.status,
|
||||||
|
statusText: error.response?.statusText,
|
||||||
|
data: error.response?.data,
|
||||||
|
url: error.config?.url,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
logError("Unexpected error:", error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
14
dist/timehelper.d.ts
vendored
Normal file
14
dist/timehelper.d.ts
vendored
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
export declare class TimeSpan {
|
||||||
|
private start;
|
||||||
|
private end;
|
||||||
|
constructor(timeSpanStr: string);
|
||||||
|
private parseTime;
|
||||||
|
contains(timestamp?: number): boolean;
|
||||||
|
}
|
||||||
|
export interface TimeBetween {
|
||||||
|
seconds: number;
|
||||||
|
toReadable: (roundToMinutes?: boolean) => string;
|
||||||
|
}
|
||||||
|
export declare function calculateSecondsBetween(start: number, end: number): TimeBetween;
|
||||||
|
export declare function secondsToReadable(secs: number, roundToMinutes?: boolean): string;
|
||||||
|
//# sourceMappingURL=timehelper.d.ts.map
|
||||||
1
dist/timehelper.d.ts.map
vendored
Normal file
1
dist/timehelper.d.ts.map
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
{"version":3,"file":"timehelper.d.ts","sourceRoot":"","sources":["../src/timehelper.ts"],"names":[],"mappings":"AAAA,qBAAa,QAAQ;IACnB,OAAO,CAAC,KAAK,CAAqC;IAClD,OAAO,CAAC,GAAG,CAAqC;gBAEpC,WAAW,EAAE,MAAM;IAM/B,OAAO,CAAC,SAAS;IAKjB,QAAQ,CAAC,SAAS,GAAE,MAAmB,GAAG,OAAO;CAgBlD;AAED,MAAM,WAAW,WAAW;IAC1B,OAAO,EAAE,MAAM,CAAC;IAChB,UAAU,EAAE,CAAC,cAAc,CAAC,EAAE,OAAO,KAAK,MAAM,CAAC;CAClD;AAED,wBAAgB,uBAAuB,CACrC,KAAK,EAAE,MAAM,EACb,GAAG,EAAE,MAAM,GACV,WAAW,CAOb;AAED,wBAAgB,iBAAiB,CAC/B,IAAI,EAAE,MAAM,EACZ,cAAc,GAAE,OAAe,GAC9B,MAAM,CAmBR"}
|
||||||
50
dist/timehelper.js
vendored
Normal file
50
dist/timehelper.js
vendored
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
"use strict";
|
||||||
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||||||
|
exports.TimeSpan = void 0;
|
||||||
|
exports.calculateSecondsBetween = calculateSecondsBetween;
|
||||||
|
exports.secondsToReadable = secondsToReadable;
|
||||||
|
class TimeSpan {
|
||||||
|
constructor(timeSpanStr) {
|
||||||
|
const [startStr, endStr] = timeSpanStr.split("-");
|
||||||
|
this.start = this.parseTime(startStr);
|
||||||
|
this.end = this.parseTime(endStr);
|
||||||
|
}
|
||||||
|
parseTime(timeStr) {
|
||||||
|
const [hours, minutes] = timeStr.split(":").map(Number);
|
||||||
|
return { hours, minutes };
|
||||||
|
}
|
||||||
|
contains(timestamp = Date.now()) {
|
||||||
|
const date = new Date(timestamp);
|
||||||
|
const hours = date.getHours();
|
||||||
|
const minutes = date.getMinutes();
|
||||||
|
const currentMinutes = hours * 60 + minutes;
|
||||||
|
const startMinutes = this.start.hours * 60 + this.start.minutes;
|
||||||
|
const endMinutes = this.end.hours * 60 + this.end.minutes;
|
||||||
|
if (startMinutes > endMinutes) {
|
||||||
|
return currentMinutes >= startMinutes || currentMinutes < endMinutes;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return currentMinutes >= startMinutes && currentMinutes < endMinutes;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
exports.TimeSpan = TimeSpan;
|
||||||
|
function calculateSecondsBetween(start, end) {
|
||||||
|
const seconds = Math.max(60, (end - start) / 1000);
|
||||||
|
return {
|
||||||
|
seconds,
|
||||||
|
toReadable: (roundToMinutes) => secondsToReadable(seconds, roundToMinutes),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
function secondsToReadable(secs, roundToMinutes = false) {
|
||||||
|
const totalSeconds = roundToMinutes ? Math.round(secs / 60) * 60 : secs;
|
||||||
|
var days = Math.floor(totalSeconds / (3600 * 24));
|
||||||
|
var hours = Math.floor((totalSeconds % (3600 * 24)) / 3600);
|
||||||
|
var minutes = Math.floor((totalSeconds % 3600) / 60);
|
||||||
|
var seconds = Math.floor(totalSeconds % 60);
|
||||||
|
var dayDisplay = days > 0 ? days + (days === 1 ? " day, " : " days, ") : "";
|
||||||
|
var hourDisplay = hours > 0 ? hours + (hours === 1 ? " hour, " : " hours, ") : "";
|
||||||
|
var minuteDisplay = minutes > 0 ? minutes + (minutes === 1 ? " minute, " : " minutes, ") : "";
|
||||||
|
var secondDisplay = seconds > 0 ? seconds + (seconds === 1 ? " second" : " seconds") : "";
|
||||||
|
return (dayDisplay + hourDisplay + minuteDisplay + secondDisplay).replace(/,\s*$/, "");
|
||||||
|
}
|
||||||
8
dist/timespan.d.ts
vendored
Normal file
8
dist/timespan.d.ts
vendored
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
export declare class TimeSpan {
|
||||||
|
private start;
|
||||||
|
private end;
|
||||||
|
constructor(timeSpanStr: string);
|
||||||
|
private parseTime;
|
||||||
|
contains(timestamp?: number): boolean;
|
||||||
|
}
|
||||||
|
//# sourceMappingURL=timespan.d.ts.map
|
||||||
1
dist/timespan.d.ts.map
vendored
Normal file
1
dist/timespan.d.ts.map
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
{"version":3,"file":"timespan.d.ts","sourceRoot":"","sources":["../src/timespan.ts"],"names":[],"mappings":"AAAA,qBAAa,QAAQ;IACnB,OAAO,CAAC,KAAK,CAAqC;IAClD,OAAO,CAAC,GAAG,CAAqC;gBAEpC,WAAW,EAAE,MAAM;IAM/B,OAAO,CAAC,SAAS;IAKjB,QAAQ,CAAC,SAAS,GAAE,MAAmB,GAAG,OAAO;CAgBlD"}
|
||||||
29
dist/timespan.js
vendored
Normal file
29
dist/timespan.js
vendored
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
"use strict";
|
||||||
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||||||
|
exports.TimeSpan = void 0;
|
||||||
|
class TimeSpan {
|
||||||
|
constructor(timeSpanStr) {
|
||||||
|
const [startStr, endStr] = timeSpanStr.split("-");
|
||||||
|
this.start = this.parseTime(startStr);
|
||||||
|
this.end = this.parseTime(endStr);
|
||||||
|
}
|
||||||
|
parseTime(timeStr) {
|
||||||
|
const [hours, minutes] = timeStr.split(":").map(Number);
|
||||||
|
return { hours, minutes };
|
||||||
|
}
|
||||||
|
contains(timestamp = Date.now()) {
|
||||||
|
const date = new Date(timestamp);
|
||||||
|
const hours = date.getHours();
|
||||||
|
const minutes = date.getMinutes();
|
||||||
|
const currentMinutes = hours * 60 + minutes;
|
||||||
|
const startMinutes = this.start.hours * 60 + this.start.minutes;
|
||||||
|
const endMinutes = this.end.hours * 60 + this.end.minutes;
|
||||||
|
if (startMinutes > endMinutes) {
|
||||||
|
return currentMinutes >= startMinutes || currentMinutes < endMinutes;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return currentMinutes >= startMinutes && currentMinutes < endMinutes;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
exports.TimeSpan = TimeSpan;
|
||||||
4
package-lock.json
generated
4
package-lock.json
generated
@@ -1,12 +1,12 @@
|
|||||||
{
|
{
|
||||||
"name": "@dpu/shared",
|
"name": "@dpu/shared",
|
||||||
"version": "1.0.1",
|
"version": "1.0.11",
|
||||||
"lockfileVersion": 3,
|
"lockfileVersion": 3,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"packages": {
|
"packages": {
|
||||||
"": {
|
"": {
|
||||||
"name": "@dpu/shared",
|
"name": "@dpu/shared",
|
||||||
"version": "1.0.1",
|
"version": "1.0.11",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"axios": "^1.7.9",
|
"axios": "^1.7.9",
|
||||||
"chalk": "^5.6.2"
|
"chalk": "^5.6.2"
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@dpu/shared",
|
"name": "@dpu/shared",
|
||||||
"version": "1.0.1",
|
"version": "1.0.11",
|
||||||
"description": "",
|
"description": "",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
"types": "dist/index.d.ts",
|
"types": "dist/index.d.ts",
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
import { TimeBetween } from "./timehelper";
|
||||||
|
|
||||||
export type HomeAssistantEntity = {
|
export type HomeAssistantEntity = {
|
||||||
entity_id: string;
|
entity_id: string;
|
||||||
state: string;
|
state: string;
|
||||||
@@ -22,4 +24,11 @@ export interface HomeAssistantDeskPositionResult {
|
|||||||
raw: HomeAssistantEntity;
|
raw: HomeAssistantEntity;
|
||||||
asBoolean: boolean;
|
asBoolean: boolean;
|
||||||
asText: () => string;
|
asText: () => string;
|
||||||
|
standingTime: TimeBetween;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export type API_HA_DeskPosition = {
|
||||||
|
position: string;
|
||||||
|
is_standing: boolean;
|
||||||
|
last_changed: string;
|
||||||
|
};
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
export * from "./homeassistant";
|
export * from "./homeassistant";
|
||||||
export * as Logger from "./logger";
|
export * as Logger from "./logger";
|
||||||
export * from "./tidal";
|
export * from "./tidal";
|
||||||
export * as Utility from "./utility";
|
export * as TimeHelper from "./timehelper";
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import axios from "axios";
|
||||||
import chalk from "chalk";
|
import chalk from "chalk";
|
||||||
|
|
||||||
export function logError(...args: unknown[]) {
|
export function logError(...args: unknown[]) {
|
||||||
@@ -15,3 +16,17 @@ export function logSuccess(...args: unknown[]) {
|
|||||||
export function logInfo(...args: unknown[]) {
|
export function logInfo(...args: unknown[]) {
|
||||||
console.info(chalk.cyan("INFO:"), ...args);
|
console.info(chalk.cyan("INFO:"), ...args);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function printNetworkError(error: unknown) {
|
||||||
|
if (axios.isAxiosError(error)) {
|
||||||
|
logError("Axios error details:", {
|
||||||
|
message: error.message,
|
||||||
|
status: error.response?.status,
|
||||||
|
statusText: error.response?.statusText,
|
||||||
|
data: error.response?.data,
|
||||||
|
url: error.config?.url,
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
logError("Unexpected error:", error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
73
src/timehelper.ts
Normal file
73
src/timehelper.ts
Normal file
@@ -0,0 +1,73 @@
|
|||||||
|
export class TimeSpan {
|
||||||
|
private start: { hours: number; minutes: number };
|
||||||
|
private end: { hours: number; minutes: number };
|
||||||
|
|
||||||
|
constructor(timeSpanStr: string) {
|
||||||
|
const [startStr, endStr] = timeSpanStr.split("-");
|
||||||
|
this.start = this.parseTime(startStr);
|
||||||
|
this.end = this.parseTime(endStr);
|
||||||
|
}
|
||||||
|
|
||||||
|
private parseTime(timeStr: string) {
|
||||||
|
const [hours, minutes] = timeStr.split(":").map(Number);
|
||||||
|
return { hours, minutes };
|
||||||
|
}
|
||||||
|
|
||||||
|
contains(timestamp: number = Date.now()): boolean {
|
||||||
|
const date = new Date(timestamp);
|
||||||
|
|
||||||
|
const hours = date.getHours();
|
||||||
|
const minutes = date.getMinutes();
|
||||||
|
const currentMinutes = hours * 60 + minutes;
|
||||||
|
|
||||||
|
const startMinutes = this.start.hours * 60 + this.start.minutes;
|
||||||
|
const endMinutes = this.end.hours * 60 + this.end.minutes;
|
||||||
|
|
||||||
|
if (startMinutes > endMinutes) {
|
||||||
|
return currentMinutes >= startMinutes || currentMinutes < endMinutes;
|
||||||
|
} else {
|
||||||
|
return currentMinutes >= startMinutes && currentMinutes < endMinutes;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface TimeBetween {
|
||||||
|
seconds: number;
|
||||||
|
toReadable: (roundToMinutes?: boolean) => string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function calculateSecondsBetween(
|
||||||
|
start: number,
|
||||||
|
end: number,
|
||||||
|
): TimeBetween {
|
||||||
|
const seconds = Math.max(60, (end - start) / 1000);
|
||||||
|
return {
|
||||||
|
seconds,
|
||||||
|
toReadable: (roundToMinutes?: boolean) =>
|
||||||
|
secondsToReadable(seconds, roundToMinutes),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export function secondsToReadable(
|
||||||
|
secs: number,
|
||||||
|
roundToMinutes: boolean = false,
|
||||||
|
): string {
|
||||||
|
const totalSeconds = roundToMinutes ? Math.round(secs / 60) * 60 : secs;
|
||||||
|
|
||||||
|
var days = Math.floor(totalSeconds / (3600 * 24));
|
||||||
|
var hours = Math.floor((totalSeconds % (3600 * 24)) / 3600);
|
||||||
|
var minutes = Math.floor((totalSeconds % 3600) / 60);
|
||||||
|
var seconds = Math.floor(totalSeconds % 60);
|
||||||
|
|
||||||
|
var dayDisplay = days > 0 ? days + (days === 1 ? " day, " : " days, ") : "";
|
||||||
|
var hourDisplay =
|
||||||
|
hours > 0 ? hours + (hours === 1 ? " hour, " : " hours, ") : "";
|
||||||
|
var minuteDisplay =
|
||||||
|
minutes > 0 ? minutes + (minutes === 1 ? " minute, " : " minutes, ") : "";
|
||||||
|
var secondDisplay =
|
||||||
|
seconds > 0 ? seconds + (seconds === 1 ? " second" : " seconds") : "";
|
||||||
|
return (dayDisplay + hourDisplay + minuteDisplay + secondDisplay).replace(
|
||||||
|
/,\s*$/,
|
||||||
|
"",
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -1,32 +0,0 @@
|
|||||||
export class TimeSpan {
|
|
||||||
private start: { hours: number; minutes: number };
|
|
||||||
private end: { hours: number; minutes: number };
|
|
||||||
|
|
||||||
constructor(timeSpanStr: string) {
|
|
||||||
const [startStr, endStr] = timeSpanStr.split("-");
|
|
||||||
this.start = this.parseTime(startStr);
|
|
||||||
this.end = this.parseTime(endStr);
|
|
||||||
}
|
|
||||||
|
|
||||||
private parseTime(timeStr: string) {
|
|
||||||
const [hours, minutes] = timeStr.split(":").map(Number);
|
|
||||||
return { hours, minutes };
|
|
||||||
}
|
|
||||||
|
|
||||||
contains(timestamp: number = Date.now()): boolean {
|
|
||||||
const date = new Date(timestamp);
|
|
||||||
|
|
||||||
const hours = date.getHours();
|
|
||||||
const minutes = date.getMinutes();
|
|
||||||
const currentMinutes = hours * 60 + minutes;
|
|
||||||
|
|
||||||
const startMinutes = this.start.hours * 60 + this.start.minutes;
|
|
||||||
const endMinutes = this.end.hours * 60 + this.end.minutes;
|
|
||||||
|
|
||||||
if (startMinutes > endMinutes) {
|
|
||||||
return currentMinutes >= startMinutes || currentMinutes < endMinutes;
|
|
||||||
} else {
|
|
||||||
return currentMinutes >= startMinutes && currentMinutes < endMinutes;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,40 +0,0 @@
|
|||||||
import axios from "axios";
|
|
||||||
import { logError } from "./logger";
|
|
||||||
|
|
||||||
export function secondsToReadable(
|
|
||||||
secs: number,
|
|
||||||
roundToMinutes: boolean = false,
|
|
||||||
): string {
|
|
||||||
const totalSeconds = roundToMinutes ? Math.round(secs / 60) * 60 : secs;
|
|
||||||
|
|
||||||
var days = Math.floor(totalSeconds / (3600 * 24));
|
|
||||||
var hours = Math.floor((totalSeconds % (3600 * 24)) / 3600);
|
|
||||||
var minutes = Math.floor((totalSeconds % 3600) / 60);
|
|
||||||
var seconds = Math.floor(totalSeconds % 60);
|
|
||||||
|
|
||||||
var dayDisplay = days > 0 ? days + (days === 1 ? " day, " : " days, ") : "";
|
|
||||||
var hourDisplay =
|
|
||||||
hours > 0 ? hours + (hours === 1 ? " hour, " : " hours, ") : "";
|
|
||||||
var minuteDisplay =
|
|
||||||
minutes > 0 ? minutes + (minutes === 1 ? " minute, " : " minutes, ") : "";
|
|
||||||
var secondDisplay =
|
|
||||||
seconds > 0 ? seconds + (seconds === 1 ? " second" : " seconds") : "";
|
|
||||||
return (dayDisplay + hourDisplay + minuteDisplay + secondDisplay).replace(
|
|
||||||
/,\s*$/,
|
|
||||||
"",
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
export function printNetworkError(error: unknown) {
|
|
||||||
if (axios.isAxiosError(error)) {
|
|
||||||
logError("Axios error details:", {
|
|
||||||
message: error.message,
|
|
||||||
status: error.response?.status,
|
|
||||||
statusText: error.response?.statusText,
|
|
||||||
data: error.response?.data,
|
|
||||||
url: error.config?.url,
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
logError("Unexpected error:", error);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user