/[sudobot]/branches/7.x/src/utils/datetime.ts
ViewVC logotype

Annotation of /branches/7.x/src/utils/datetime.ts

Parent Directory Parent Directory | Revision Log Revision Log


Revision 577 - (hide annotations)
Mon Jul 29 18:52:37 2024 UTC (8 months ago) by rakinar2
File MIME type: application/typescript
File size: 2590 byte(s)
chore: add old version archive branches (2.x to 9.x-dev)
1 rakinar2 577 /*
2     * This file is part of SudoBot.
3     *
4     * Copyright (C) 2021-2023 OSN Developers.
5     *
6     * SudoBot is free software; you can redistribute it and/or modify it
7     * under the terms of the GNU Affero General Public License as published by
8     * the Free Software Foundation, either version 3 of the License, or
9     * (at your option) any later version.
10     *
11     * SudoBot is distributed in the hope that it will be useful, but
12     * WITHOUT ANY WARRANTY; without even the implied warranty of
13     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14     * GNU Affero General Public License for more details.
15     *
16     * You should have received a copy of the GNU Affero General Public License
17     * along with SudoBot. If not, see <https://www.gnu.org/licenses/>.
18     */
19    
20     export function stringToTimeInterval(input: string, { milliseconds = false } = {}) {
21     let seconds = 0;
22     let number = "";
23    
24     for (let i = 0; i < input.length; i++) {
25     if (["1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "."].includes(input[i])) {
26     number += input[i];
27     } else {
28     const unit = input.substring(i);
29     const float = parseFloat(number.toString());
30    
31     if (Number.isNaN(float)) {
32     return { error: "Invalid numeric time value", result: NaN };
33     }
34    
35     if (["s", "sec", "secs", "second", "seconds"].includes(`${unit}`)) {
36     seconds += float;
37     } else if (["m", "min", "mins", "minute", "minutes"].includes(`${unit}`)) {
38     seconds += float * 60;
39     } else if (["h", "hr", "hrs", "hour", "hours"].includes(unit)) {
40     seconds += float * 60 * 60;
41     } else if (["d", "dy", "dys", "day", "days"].includes(unit)) {
42     seconds += float * 60 * 60 * 24;
43     } else if (["w", "wk", "wks", "week", "weeks"].includes(unit)) {
44     seconds += float * 60 * 60 * 24 * 7;
45     } else if (["M", "mo", "mos", "month", "months"].includes(unit)) {
46     seconds += float * 60 * 60 * 24 * 30;
47     } else if (["y", "yr", "yrs", "year", "years"].includes(unit)) {
48     seconds += float * 60 * 60 * 24 * 365;
49     } else {
50     return { error: "Invalid time unit", result: NaN };
51     }
52    
53     break;
54     }
55     }
56    
57     return { error: undefined, result: seconds * (milliseconds ? 1000 : 1) };
58     }
59    
60     export function displayDate(date: Date) {
61     return displayTimeSeconds(Math.round(date.getTime() / 1000));
62     }
63    
64     export function displayTimeSeconds(seconds: number) {
65     return `<t:${seconds}:f> (<t:${seconds}:R>)`;
66     }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26