|
|
|
@ -1,10 +1,21 @@ |
|
|
|
import { Injectable } from '@angular/core'; |
|
|
|
import { inject, Injectable } from '@angular/core'; |
|
|
|
import { DOCUMENT } from '@angular/common'; |
|
|
|
import { ConfigStateService } from './config-state.service'; |
|
|
|
|
|
|
|
@Injectable({ |
|
|
|
providedIn: 'root', |
|
|
|
}) |
|
|
|
export class TimezoneService { |
|
|
|
protected readonly configState = inject(ConfigStateService); |
|
|
|
protected readonly document = inject(DOCUMENT); |
|
|
|
private readonly cookieKey = '__timezone'; |
|
|
|
private timeZoneNameFromSettings: string | null | undefined; |
|
|
|
|
|
|
|
constructor() { |
|
|
|
this.configState.getOne$('timing').subscribe(timezoneSettings => { |
|
|
|
this.timeZoneNameFromSettings = timezoneSettings?.timeZone?.iana?.timeZoneName; |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
getBrowserTimezone(): string { |
|
|
|
return Intl.DateTimeFormat().resolvedOptions().timeZone; |
|
|
|
@ -12,11 +23,11 @@ export class TimezoneService { |
|
|
|
|
|
|
|
getTimezone(): string { |
|
|
|
const fromCookie = this.getCookie(this.cookieKey); |
|
|
|
return fromCookie || this.getBrowserTimezone(); |
|
|
|
return this.timeZoneNameFromSettings || fromCookie || this.getBrowserTimezone(); |
|
|
|
} |
|
|
|
|
|
|
|
setTimezone(timezone: string): void { |
|
|
|
document.cookie = `${this.cookieKey}=${timezone}; path=/`; |
|
|
|
this.document.cookie = `${this.cookieKey}=${timezone}; path=/`; |
|
|
|
} |
|
|
|
|
|
|
|
convertUtcToLocal(date: string | Date): Date { |
|
|
|
@ -28,7 +39,7 @@ export class TimezoneService { |
|
|
|
} |
|
|
|
|
|
|
|
private getCookie(name: string): string | null { |
|
|
|
const match = document.cookie.match(new RegExp('(^| )' + name + '=([^;]+)')); |
|
|
|
const match = this.document.cookie.match(new RegExp('(^| )' + name + '=([^;]+)')); |
|
|
|
return match ? decodeURIComponent(match[2]) : null; |
|
|
|
} |
|
|
|
} |
|
|
|
|