diff --git a/index.html b/index.html index 32ec86a..70df880 100644 --- a/index.html +++ b/index.html @@ -1,19 +1,9 @@ diff --git a/package.json b/package.json index f0295a5..c5122c9 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@ztimson/utils", - "version": "0.25.10", + "version": "0.25.12", "description": "Utility library", "author": "Zak Timson", "license": "MIT", diff --git a/src/path-events.ts b/src/path-events.ts index dbdb0c2..dd7cd51 100644 --- a/src/path-events.ts +++ b/src/path-events.ts @@ -48,7 +48,7 @@ export function PES(str: TemplateStringsArray, ...args: any[]) { if(str[i]) combined.push(str[i]); if(args[i]) combined.push(args[i]); } - const [paths, methods] = combined.join('').split(':'); + const [paths, methods] = combined.join('/').split(':'); return PathEvent.toString(paths, methods?.split('')); } @@ -254,8 +254,8 @@ export class PathEventEmitter implements IPathEventEmitter{ constructor(public readonly prefix: string = '') { } emit(event: Event, ...args: any[]) { - const parsed = new PathEvent(`${this.prefix}/${new PathEvent(event).toString()}`); - this.listeners.filter(l => PathEvent.has(l[0], `${this.prefix}/${event}`)) + const parsed = PE`${this.prefix}/${event}`; + this.listeners.filter(l => PathEvent.has(l[0], parsed)) .forEach(async l => l[1](parsed, ...args)); }; diff --git a/src/time.ts b/src/time.ts index 35d5265..f65aa9a 100644 --- a/src/time.ts +++ b/src/time.ts @@ -190,3 +190,35 @@ export async function sleepWhile(fn : () => boolean | Promise, checkInt export function timeUntil(date: Date | number): number { return (date instanceof Date ? date.getTime() : date) - (new Date()).getTime(); } + + +/** + * Convert a timezone string (e.g., "America/Toronto") to its current UTC offset in minutes. + * @param {string} tz - Timezone string, e.g. "America/Toronto" + * @param {Date} [date=new Date()] - The date for which you want the offset (default is now) + * @returns {number} - Offset in minutes (e.g., -240) + */ +export function timezoneOffset(tz: string, date: Date = new Date()): number { + const dtf = new Intl.DateTimeFormat('en-US', { + timeZone: tz, + hour12: false, + year: 'numeric', + month: '2-digit', + day: '2-digit', + hour: '2-digit', + minute: '2-digit', + second: '2-digit', + }); + const parts = dtf.formatToParts(date); + const get = (type: string) => Number(parts.find(v => v.type === type)?.value); + const y = get('year'); + const mo = get('month'); + const d = get('day'); + const h = get('hour'); + const m = get('minute'); + const s = get('second'); + + const asUTC = Date.UTC(y, mo - 1, d, h, m, s); + const asLocal = date.getTime(); + return Math.round((asLocal - asUTC) / 60000); +}