plugins-evert/timezone/plugin.ts

65 lines
4.0 KiB
TypeScript

import {
Plugin,
Configurable,
EventListener,
DependencyLoad
} from '@squeebot/core/lib/plugin';
import { IMessage, MessageResolver } from '@squeebot/core/lib/types';
const tz: {[key: string]: number} = {'UTC-12': -12, 'UTC-11': -11, 'UTC-10': -10, 'UTC-9': -9, 'UTC-9:30': -9.5, 'UTC-8': -8, 'UTC-7': -7, 'UTC-6': -6, 'UTC-5': -5, 'UTC-4': -4, 'UTC-3': -3, 'UTC-3:30': -3.5, 'UTC-2': -2, 'UTC-1': -1, UTC: 0, 'UTC+1': 1, 'UTC+2': 2, 'UTC+3': 3, 'UTC+4': 4, 'UTC+4:30': 4.5, 'UTC+5': 5, 'UTC+5:30': 5.5, 'UTC+5:45': 5.75, 'UTC+6': 6, 'UTC+6:30': 6.5, 'UTC+7': 7, 'UTC+8': 8, 'UTC+8:30': 8.5, 'UTC+8:45': 8.75, 'UTC+9': 9, 'UTC+9:30': 9.5, 'UTC+10': 10, 'UTC+10:30': 10.5, 'UTC+11': 11, 'UTC+12': 12, 'UTC+12:45': 12.75, 'UTC+13': 13, 'UTC+13:45': 13.75, 'UTC+14': 14, BST: 1, CET: 1, DFT: 1, IST: 1, MET: 1, WAT: 1, WEST: 1, CAT: 2, CEST: 2, EET: 2, HAEC: 2, MEST: 2, SAST: 2, USZ1: 2, WAST: 2, AST: 3, EAT: 3, EEST: 3, FET: 3, IDT: 3, IOT: 3, MSK: 3, SYOT: 3, TRT: 3, IRST: 3.5, AMT: 4, AZT: 4, GET: 4, GST: 4, MUT: 4, RET: 4, SAMT: 4, SCT: 4, VOLT: 4, AFT: 4.5, IRDT: 4.5, MAWT: 5, MVT: 5, ORAT: 5, PKT: 5, TFT: 5, TJT: 5, TMT: 5, UZT: 5, YEKT: 5, IndianST: 5.5, SLST: 5.5, NPT: 5.75, BIOT: 6, BTT: 6, KGT: 6, OMST: 6, VOST: 6, CCT: 6.5, MMT: 6.5, ACT: 6.5, CXT: 7, DAVT: 7, HOVT: 7, ICT: 7, KRAT: 7, THA: 7, WIT: 7, AWST: 8, BDT: 8, CHOT: 8, CIT: 8, CT: 8, HKT: 8, HOVST: 8, IRKT: 8, MST: 8, MYT: 8, PHT: 8, SGT: 8, SST: 8, ULAT: 8, WST: 8, CWST: 8.5, CHOST: 9, EIT: 9, JST: 9, KST: 9, TLT: 9, ULAST: 9, YAKT: 9, ACST: 9.5, AEST: 10, CHST: 10, CHUT: 10, DDUT: 10, PGT: 10, VLAT: 10, ACDT: 10.5, LHST: 10.5, AEDT: 11, KOST: 11, MIST: 11, NCT: 11, NFT: 11, PONT: 11, SAKT: 11, SBT: 11, SRET: 11, VUT: 11, FJT: 12, GILT: 12, MAGT: 12, MHT: 12, NZST: 12, PETT: 12, TVT: 12, WAKT: 12, CHAST: 12.75, NZDT: 13, PHOT: 13, TKT: 13, TOT: 13, CHADT: 13.75, LINT: 14, AZOT: -1, CVT: -1, EGT: -1, BRST: -2, FNT: -2, PMDT: -2, UYST: -2, NDT: -2.5, ADT: -3, AMST: -3, ART: -3, BRT: -3, CLST: -3, FKST: -3, GFT: -3, PMST: -3, PYST: -3, ROTT: -3, UYT: -3, NST: -3.5, NT: -3.5, AtST: -4, BOT: -4, CLT: -4, COST: -4, ECT: -4, EDT: -4, FKT: -4, GYT: -4, PYT: -4, VET: -4, CDT: -5, COT: -5, EASST: -5, EST: -5, PET: -5, CST: -6, EAST: -6, GALT: -6, MDT: -6, PDT: -7, AKDT: -8, CIST: -8, PST: -8, AKST: -9, GAMT: -9, GIT: -9, HADT: -9, MART: -9.5, MIT: -9.5, CKT: -10, HAST: -10, TAHT: -10, NUT: -11, BIT: -12, GMT: 0, WET: 0};
// Offset timezone from UTC
function offsetTZ(offset: number): number {
const utc = new Date(new Date().toUTCString()).getTime();
return utc + 3600000 * offset;
}
// Offset timezone from UTC (readable string)
function offsetTZStr(offset: number, tzn: string | null): string {
const real = offset >= 0 ? '+' + offset : offset;
return new Date(offsetTZ(offset)).toUTCString().replace('GMT', 'UTC') + real + (tzn ? ' (' + tzn + ')' : '');
}
function timeNow(short: string): string | null {
short = short.toUpperCase();
const offset = tz[short];
if (offset == null) { return null; }
return offsetTZStr(offset, short.indexOf('UTC') !== 0 ? short : null);
}
@Configurable({
timezone: 'UTC'
})
class TZPlugin extends Plugin {
@EventListener('pluginUnload')
public unloadEventHandler(plugin: string | Plugin): void {
if (plugin === this.name || plugin === this) {
this.emit('pluginUnloaded', this);
}
}
@DependencyLoad('simplecommands')
addCommands(cmd: any): void {
cmd.registerCommand({
name: 'timezone',
plugin: this.name,
execute: async (msg: IMessage, msr: MessageResolver, spec: any, prefix: string, ...simplified: any[]): Promise<boolean> => {
const cfg = this.config.get('timezone', 'UTC');
const tn = timeNow(simplified[0] || cfg);
if (!tn) {
msg.resolve('Unknown or unlisted timezone abbreviation');
return true;
}
msg.resolve(tn);
return true;
},
aliases: ['tz'],
description: 'Date and Time in a certain time zone',
usage: '[abbrv]|UTC<+|-><offset>'
});
}
}
module.exports = TZPlugin;