BetterDiscordApp-rauenzi/renderer/src/builtins/appearance/24hour.js

29 lines
1.1 KiB
JavaScript
Raw Normal View History

2020-07-16 07:42:56 +02:00
import Builtin from "../../structs/builtin";
2019-06-20 04:19:34 +02:00
import {DiscordModules} from "modules";
2019-05-30 23:18:52 +02:00
export default new class TwentyFourHour extends Builtin {
get name() {return "24Hour";}
get category() {return "appearance";}
2019-06-06 06:28:43 +02:00
get id() {return "twentyFourHour";}
2019-05-30 23:18:52 +02:00
enabled() {
this.inject24Hour();
}
disabled() {
2019-06-20 04:19:34 +02:00
this.unpatchAll();
2019-05-30 23:18:52 +02:00
}
inject24Hour() {
const twelveHour = new RegExp(`([0-9]{1,2}):([0-9]{1,2})\\s(AM|PM)`);
2019-06-20 04:19:34 +02:00
const convert = (thisObject, args, returnValue) => {
const matched = returnValue.match(twelveHour);
2019-05-30 23:18:52 +02:00
if (!matched || matched.length !== 4) return;
2019-06-20 04:19:34 +02:00
if (matched[3] === "AM") return returnValue = returnValue.replace(matched[0], `${matched[1] === "12" ? "00" : matched[1].padStart(2, "0")}:${matched[2]}`);
return returnValue = returnValue.replace(matched[0], `${matched[1] === "12" ? "12" : parseInt(matched[1]) + 12}:${matched[2]}`);
2019-05-30 23:18:52 +02:00
};
2019-06-20 04:19:34 +02:00
this.after(DiscordModules.TimeFormatter, "calendarFormat", convert); // Called in Cozy mode
this.after(DiscordModules.TimeFormatter, "dateFormat", convert); // Called in Compact mode
2019-05-30 23:18:52 +02:00
}
};