ngx-date-fns-tz
Version:
@date-fns/tz pipes for Angular
251 lines (237 loc) • 10.3 kB
JavaScript
import * as i0 from '@angular/core';
import { signal, Injectable, inject, ChangeDetectorRef, effect, Pipe, makeEnvironmentProviders } from '@angular/core';
import { enGB } from 'date-fns/locale';
import { isValid } from 'date-fns';
import { toZonedTime, fromZonedTime, formatInTimeZone, getTimezoneOffset } from 'date-fns-tz';
class DateFnsTzConfigurationService {
constructor() {
this._timezone = signal(this.resolveBrowserTimeZone());
this._locale = signal(enGB);
this._format = signal('dd-MM-yyyy HH:mm:ss zzz');
}
set timeZone(tz) {
this._timezone.set(tz);
}
get timeZone() {
return this._timezone();
}
set locale(locale) {
this._locale.set(locale);
}
get locale() {
return this._locale();
}
set format(fmt) {
this._format.set(fmt);
}
get format() {
return this._format();
}
resolveBrowserTimeZone() {
return Intl.DateTimeFormat().resolvedOptions().timeZone;
}
static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.14", ngImport: i0, type: DateFnsTzConfigurationService, deps: [], target: i0.ɵɵFactoryTarget.Injectable }); }
static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "19.2.14", ngImport: i0, type: DateFnsTzConfigurationService, providedIn: 'root' }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.14", ngImport: i0, type: DateFnsTzConfigurationService, decorators: [{
type: Injectable,
args: [{
providedIn: 'root'
}]
}] });
function isValidDate(date) {
return date != null && isValid(new Date(date));
}
class MockChangeDetectorRef {
markForCheck() { }
detach() { }
detectChanges() { }
checkNoChanges() { }
reattach() { }
}
class UtcToZonedTimePipe {
constructor() {
this.service = inject(DateFnsTzConfigurationService);
this.cdr = inject(ChangeDetectorRef);
this.cleanup = effect(() => {
this.service.timeZone;
this.cdr.markForCheck();
});
}
transform(date, tz) {
if (!isValidDate(date))
return new Date(0);
tz = tz ?? this.service.timeZone;
return toZonedTime(date, tz);
}
ngOnDestroy() {
this.cleanup.destroy();
}
static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.14", ngImport: i0, type: UtcToZonedTimePipe, deps: [], target: i0.ɵɵFactoryTarget.Pipe }); }
static { this.ɵpipe = i0.ɵɵngDeclarePipe({ minVersion: "14.0.0", version: "19.2.14", ngImport: i0, type: UtcToZonedTimePipe, isStandalone: true, name: "utcToZonedTime", pure: false }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.14", ngImport: i0, type: UtcToZonedTimePipe, decorators: [{
type: Pipe,
args: [{
name: 'utcToZonedTime',
standalone: true,
pure: false
}]
}] });
class ZonedTimeToUtcPipe {
constructor() {
this.service = inject(DateFnsTzConfigurationService);
this.cdr = inject(ChangeDetectorRef);
this.cleanup = effect(() => {
this.service.timeZone;
this.cdr.markForCheck();
});
}
transform(date, tz) {
if (!isValidDate(date))
return new Date(0);
tz = tz ?? this.service.timeZone;
return fromZonedTime(date, tz);
}
ngOnDestroy() {
this.cleanup.destroy();
}
static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.14", ngImport: i0, type: ZonedTimeToUtcPipe, deps: [], target: i0.ɵɵFactoryTarget.Pipe }); }
static { this.ɵpipe = i0.ɵɵngDeclarePipe({ minVersion: "14.0.0", version: "19.2.14", ngImport: i0, type: ZonedTimeToUtcPipe, isStandalone: true, name: "dfnsZonedTimeToUtc", pure: false }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.14", ngImport: i0, type: ZonedTimeToUtcPipe, decorators: [{
type: Pipe,
args: [{
name: 'dfnsZonedTimeToUtc',
standalone: true,
pure: false
}]
}] });
class FormatInTimeZonePipe {
constructor() {
this.service = inject(DateFnsTzConfigurationService);
this.cdr = inject(ChangeDetectorRef);
this.cleanup = effect(() => {
this.service.timeZone;
this.service.format;
this.service.locale;
this.cdr.markForCheck();
});
}
transform(date, tz, fmt) {
if (!isValidDate(date))
return '';
tz = tz ?? this.service.timeZone;
fmt = fmt ?? this.service.format;
return formatInTimeZone(date, tz, fmt, {
locale: this.service.locale
});
}
ngOnDestroy() {
this.cleanup.destroy();
}
static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.14", ngImport: i0, type: FormatInTimeZonePipe, deps: [], target: i0.ɵɵFactoryTarget.Pipe }); }
static { this.ɵpipe = i0.ɵɵngDeclarePipe({ minVersion: "14.0.0", version: "19.2.14", ngImport: i0, type: FormatInTimeZonePipe, isStandalone: true, name: "dfnsFormatInTimeZone", pure: false }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.14", ngImport: i0, type: FormatInTimeZonePipe, decorators: [{
type: Pipe,
args: [{
name: 'dfnsFormatInTimeZone',
standalone: true,
pure: false
}]
}] });
class GetTimeZoneOffsetPipe {
constructor() {
this.service = inject(DateFnsTzConfigurationService);
}
transform(tz, date) {
return getTimezoneOffset(tz ?? this.service.timeZone, date);
}
static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.14", ngImport: i0, type: GetTimeZoneOffsetPipe, deps: [], target: i0.ɵɵFactoryTarget.Pipe }); }
static { this.ɵpipe = i0.ɵɵngDeclarePipe({ minVersion: "14.0.0", version: "19.2.14", ngImport: i0, type: GetTimeZoneOffsetPipe, isStandalone: true, name: "dfnsGetTimeZoneOffset" }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.14", ngImport: i0, type: GetTimeZoneOffsetPipe, decorators: [{
type: Pipe,
args: [{
name: 'dfnsGetTimeZoneOffset',
standalone: true,
pure: true
}]
}] });
class UtcToZonedTimePurePipe {
constructor() {
this.service = inject(DateFnsTzConfigurationService);
}
transform(date, tz) {
if (!isValidDate(date))
return new Date(0);
tz = tz ?? this.service.timeZone;
return toZonedTime(date, tz);
}
static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.14", ngImport: i0, type: UtcToZonedTimePurePipe, deps: [], target: i0.ɵɵFactoryTarget.Pipe }); }
static { this.ɵpipe = i0.ɵɵngDeclarePipe({ minVersion: "14.0.0", version: "19.2.14", ngImport: i0, type: UtcToZonedTimePurePipe, isStandalone: true, name: "utcToZonedTimePure" }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.14", ngImport: i0, type: UtcToZonedTimePurePipe, decorators: [{
type: Pipe,
args: [{
name: 'utcToZonedTimePure',
standalone: true,
pure: true
}]
}] });
class ZonedTimeToUtcPurePipe {
constructor() {
this.service = inject(DateFnsTzConfigurationService);
}
transform(date, tz) {
if (!isValidDate(date))
return new Date(0);
tz = tz ?? this.service.timeZone;
return fromZonedTime(date, tz);
}
static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.14", ngImport: i0, type: ZonedTimeToUtcPurePipe, deps: [], target: i0.ɵɵFactoryTarget.Pipe }); }
static { this.ɵpipe = i0.ɵɵngDeclarePipe({ minVersion: "14.0.0", version: "19.2.14", ngImport: i0, type: ZonedTimeToUtcPurePipe, isStandalone: true, name: "dfnsZonedTimeToUtcPure" }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.14", ngImport: i0, type: ZonedTimeToUtcPurePipe, decorators: [{
type: Pipe,
args: [{
name: 'dfnsZonedTimeToUtcPure',
standalone: true,
pure: true
}]
}] });
class FormatInTimeZonePurePipe {
constructor() {
this.service = inject(DateFnsTzConfigurationService);
}
transform(date, tz, fmt) {
if (!isValidDate(date))
return '';
tz = tz ?? this.service.timeZone;
fmt = fmt ?? this.service.format;
return formatInTimeZone(date, tz, fmt, {
locale: this.service.locale
});
}
static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.14", ngImport: i0, type: FormatInTimeZonePurePipe, deps: [], target: i0.ɵɵFactoryTarget.Pipe }); }
static { this.ɵpipe = i0.ɵɵngDeclarePipe({ minVersion: "14.0.0", version: "19.2.14", ngImport: i0, type: FormatInTimeZonePurePipe, isStandalone: true, name: "dfnsFormatInTimeZonePure" }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.14", ngImport: i0, type: FormatInTimeZonePurePipe, decorators: [{
type: Pipe,
args: [{
name: 'dfnsFormatInTimeZonePure',
standalone: true,
pure: true
}]
}] });
function provideDateFnsTz() {
return makeEnvironmentProviders([
DateFnsTzConfigurationService
]);
}
/**
* Generated bundle index. Do not edit.
*/
export { DateFnsTzConfigurationService, FormatInTimeZonePipe, FormatInTimeZonePurePipe, GetTimeZoneOffsetPipe, MockChangeDetectorRef, UtcToZonedTimePipe, UtcToZonedTimePurePipe, ZonedTimeToUtcPipe, ZonedTimeToUtcPurePipe, isValidDate, provideDateFnsTz };
//# sourceMappingURL=ngx-date-fns-tz.mjs.map