diff --git a/dist/timehelper.d.ts b/dist/timehelper.d.ts index 4dca049..9cdc507 100644 --- a/dist/timehelper.d.ts +++ b/dist/timehelper.d.ts @@ -1,7 +1,8 @@ export declare class TimeSpan { private start; private end; - constructor(timeSpanStr: string); + private timeZone; + constructor(timeSpanStr: string, timeZone: string); private parseTime; contains(timestamp?: number): boolean; } diff --git a/dist/timehelper.d.ts.map b/dist/timehelper.d.ts.map index 00c1cdd..c924317 100644 --- a/dist/timehelper.d.ts.map +++ b/dist/timehelper.d.ts.map @@ -1 +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"} \ No newline at end of file +{"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;IAChD,OAAO,CAAC,QAAQ,CAAC;gBAEL,WAAW,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM;IAOjD,OAAO,CAAC,SAAS;IAKjB,QAAQ,CAAC,SAAS,GAAE,MAAmB,GAAG,OAAO;CAqBlD;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"} \ No newline at end of file diff --git a/dist/timehelper.js b/dist/timehelper.js index 0ef369b..149081c 100644 --- a/dist/timehelper.js +++ b/dist/timehelper.js @@ -1,10 +1,12 @@ export class TimeSpan { start; end; - constructor(timeSpanStr) { + timeZone; + constructor(timeSpanStr, timeZone) { const [startStr, endStr] = timeSpanStr.split("-"); this.start = this.parseTime(startStr); this.end = this.parseTime(endStr); + this.timeZone = timeZone; } parseTime(timeStr) { const [hours, minutes] = timeStr.split(":").map(Number); @@ -12,8 +14,13 @@ export class TimeSpan { } contains(timestamp = Date.now()) { const date = new Date(timestamp); - const hours = date.getHours(); - const minutes = date.getMinutes(); + const berlinTimeStr = date.toLocaleString("de-DE", { + timeZone: this.timeZone, // "Europe/Berlin" + hour12: false, + hour: "2-digit", + minute: "2-digit", + }); + const [hours, minutes] = berlinTimeStr.split(":").map(Number); const currentMinutes = hours * 60 + minutes; const startMinutes = this.start.hours * 60 + this.start.minutes; const endMinutes = this.end.hours * 60 + this.end.minutes; diff --git a/src/timehelper.ts b/src/timehelper.ts index e1729b8..a50d625 100644 --- a/src/timehelper.ts +++ b/src/timehelper.ts @@ -1,11 +1,13 @@ export class TimeSpan { private start: { hours: number; minutes: number }; private end: { hours: number; minutes: number }; + private timeZone; - constructor(timeSpanStr: string) { + constructor(timeSpanStr: string, timeZone: string) { const [startStr, endStr] = timeSpanStr.split("-"); this.start = this.parseTime(startStr); this.end = this.parseTime(endStr); + this.timeZone = timeZone; } private parseTime(timeStr: string) { @@ -16,10 +18,15 @@ export class TimeSpan { 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 berlinTimeStr = date.toLocaleString("de-DE", { + timeZone: this.timeZone, // "Europe/Berlin" + hour12: false, + hour: "2-digit", + minute: "2-digit", + }); + const [hours, minutes] = berlinTimeStr.split(":").map(Number); + const currentMinutes = hours * 60 + minutes; const startMinutes = this.start.hours * 60 + this.start.minutes; const endMinutes = this.end.hours * 60 + this.end.minutes;