Browse Source

extensible table timezone support

pull/22619/head
erdemcaygor 1 year ago
parent
commit
eb8b74429c
  1. 3
      npm/ng-packs/packages/core/src/lib/core.module.ts
  2. 29
      npm/ng-packs/packages/core/src/lib/pipes/utc-to-local.pipe.ts

3
npm/ng-packs/packages/core/src/lib/core.module.ts

@ -65,7 +65,6 @@ const standaloneDirectives = [
ShortDateTimePipe,
ShortTimePipe,
ShortDatePipe,
UtcToLocalPipe,
...standaloneDirectives,
],
imports: [
@ -74,6 +73,7 @@ const standaloneDirectives = [
ReactiveFormsModule,
RouterModule,
LocalizationModule,
UtcToLocalPipe,
...standaloneDirectives,
],
declarations: [
@ -87,7 +87,6 @@ const standaloneDirectives = [
ShortDateTimePipe,
ShortTimePipe,
ShortDatePipe,
UtcToLocalPipe,
],
providers: [LocalizationPipe, provideHttpClient(withInterceptorsFromDi())],
})

29
npm/ng-packs/packages/core/src/lib/pipes/utc-to-local.pipe.ts

@ -1,28 +1,27 @@
import { Pipe, PipeTransform, Injectable, inject } from '@angular/core';
import { TimezoneService } from '../services';
import { LocalizationService, TimezoneService } from '../services';
@Injectable()
@Pipe({
name: 'utcToLocal',
standalone: false,
name: 'abpUtcToLocal',
})
export class UtcToLocalPipe implements PipeTransform {
protected readonly timezoneService = inject(TimezoneService);
private readonly timezone: string;
constructor() {
this.timezone = this.timezoneService.getTimezone();
}
protected readonly localizationService = inject(LocalizationService);
transform(
value: string | Date | null | undefined,
locale: string = navigator.language,
options?: Intl.DateTimeFormatOptions,
): string {
): string | Date {
if (!value) return '';
try {
const utcDate = new Date(value + 'Z'); // Ensure it's treated as UTC
const dateInput = new Date(value);
if (isNaN(dateInput.getTime())) {
// Invalid date
return '';
}
const formatOptions: Intl.DateTimeFormatOptions = options || {
year: 'numeric',
@ -32,14 +31,14 @@ export class UtcToLocalPipe implements PipeTransform {
minute: '2-digit',
};
const formatter = new Intl.DateTimeFormat(locale, {
const formatter = new Intl.DateTimeFormat('en-US', {
...formatOptions,
timeZone: this.timezone,
timeZone: this.timezoneService.getTimezone(),
});
return formatter.format(utcDate);
return formatter.format(dateInput);
} catch (err) {
return '';
return value;
}
}
}

Loading…
Cancel
Save