Browse Source

timezone service added

pull/22619/head
erdemcaygor 2 years ago
parent
commit
af9cffe21e
  1. 1
      npm/ng-packs/packages/core/src/lib/services/index.ts
  2. 34
      npm/ng-packs/packages/core/src/lib/services/timezone.service.ts

1
npm/ng-packs/packages/core/src/lib/services/index.ts

@ -23,3 +23,4 @@ export * from './window.service';
export * from './internet-connection-service';
export * from './local-storage-listener.service';
export * from './title-strategy.service';
export * from './timezone.service';

34
npm/ng-packs/packages/core/src/lib/services/timezone.service.ts

@ -0,0 +1,34 @@
import { Injectable } from '@angular/core';
@Injectable({
providedIn: 'root',
})
export class TimezoneService {
private readonly cookieKey = '__timezone';
getBrowserTimezone(): string {
return Intl.DateTimeFormat().resolvedOptions().timeZone;
}
getTimezone(): string {
const fromCookie = this.getCookie(this.cookieKey);
return fromCookie || this.getBrowserTimezone();
}
setTimezone(timezone: string): void {
document.cookie = `${this.cookieKey}=${timezone}; path=/`;
}
convertUtcToLocal(date: string | Date): Date {
return new Date(date + 'Z');
}
convertLocalToUtc(date: Date): string {
return date.toISOString();
}
private getCookie(name: string): string | null {
const match = document.cookie.match(new RegExp('(^| )' + name + '=([^;]+)'));
return match ? decodeURIComponent(match[2]) : null;
}
}
Loading…
Cancel
Save