-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Multi-timezone support in Angular #22619
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
af9cffe
timezone service added
erdemcaygor b4db4b3
timezone interceptor service added
erdemcaygor 8f2bb48
utc-to-local pipe added
erdemcaygor 7a56fec
timezone service refactoring
erdemcaygor eb8b744
extensible table timezone support
erdemcaygor 1b4f4fe
refactoring
erdemcaygor 8633edb
refactoring
erdemcaygor e7b2111
refactoring
erdemcaygor 4d57345
abp-extensible-table props validation according to timezone
erdemcaygor 1d4e693
remove logs
erdemcaygor ab404f6
refactoring
erdemcaygor eda18d6
refactoring
erdemcaygor f191379
unused functions removed
erdemcaygor 14bb8a6
Add Luxon library and create TimeService for centralized date handling
erdemcaygor 935ad1c
timezone http interceptor added to provideAbpCore
erdemcaygor 265fd4d
Merge branch 'rel-9.2' into feature/#22507
erdemcaygor ccae5fc
timezone http interceptor added to provideAbpCore
erdemcaygor 5ab765b
luxon peer dependency added to core module
erdemcaygor 923655a
luxon dependency added to core module
erdemcaygor deb7b61
Update package.json
masum-ulu b4656e6
Update core.module.ts
masum-ulu 7f26390
Update core.module.ts
masum-ulu 944836a
Merge branch 'rel-9.2' into feature/#22507
oykuermann File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ | |
"angular-oauth2-oidc", | ||
"just-compare", | ||
"just-clone", | ||
"ts-toolbelt" | ||
"ts-toolbelt", | ||
"luxon" | ||
] | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from './api.interceptor'; | ||
export * from './timezone.interceptor'; |
26 changes: 26 additions & 0 deletions
26
npm/ng-packs/packages/core/src/lib/interceptors/timezone.interceptor.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { inject, Injectable } from '@angular/core'; | ||
import { HttpEvent, HttpHandler, HttpInterceptor, HttpRequest } from '@angular/common/http'; | ||
import { TimezoneService } from '../services'; | ||
import { Observable } from 'rxjs'; | ||
|
||
@Injectable({ | ||
providedIn: 'root', | ||
}) | ||
export class TimezoneInterceptor implements HttpInterceptor { | ||
protected readonly timezoneService = inject(TimezoneService); | ||
|
||
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> { | ||
if (!this.timezoneService.isUtcClockEnabled) { | ||
return next.handle(req); | ||
} | ||
const timezone = this.timezoneService.timezone; | ||
if (timezone) { | ||
req = req.clone({ | ||
setHeaders: { | ||
__timezone: timezone, | ||
}, | ||
}); | ||
} | ||
return next.handle(req); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
npm/ng-packs/packages/core/src/lib/pipes/utc-to-local.pipe.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { Pipe, PipeTransform, Injectable, inject, LOCALE_ID } from '@angular/core'; | ||
import { ConfigStateService, LocalizationService, TimeService, TimezoneService } from '../services'; | ||
import { getShortDateFormat, getShortDateShortTimeFormat, getShortTimeFormat } from '../utils'; | ||
|
||
@Injectable() | ||
@Pipe({ | ||
name: 'abpUtcToLocal', | ||
}) | ||
export class UtcToLocalPipe implements PipeTransform { | ||
protected readonly timezoneService = inject(TimezoneService); | ||
protected readonly timeService = inject(TimeService); | ||
protected readonly configState = inject(ConfigStateService); | ||
protected readonly localizationService = inject(LocalizationService); | ||
protected readonly locale = inject(LOCALE_ID); | ||
|
||
transform( | ||
value: string | Date | null | undefined, | ||
type: 'date' | 'datetime' | 'time', | ||
): string | Date { | ||
if (!value) return ''; | ||
|
||
const date = new Date(value); | ||
if (isNaN(date.getTime())) return ''; | ||
|
||
const format = this.getFormat(type); | ||
|
||
try { | ||
if (this.timezoneService.isUtcClockEnabled) { | ||
const timeZone = this.timezoneService.timezone; | ||
return this.timeService.formatDateWithStandardOffset(date, format, timeZone); | ||
} else { | ||
return this.timeService.formatWithoutTimeZone(date, format); | ||
} | ||
} catch (err) { | ||
return value; | ||
} | ||
} | ||
|
||
private getFormat(propType: 'date' | 'datetime' | 'time'): string { | ||
switch (propType) { | ||
case 'date': | ||
return getShortDateFormat(this.configState); | ||
case 'time': | ||
return getShortTimeFormat(this.configState); | ||
case 'datetime': | ||
default: | ||
return getShortDateShortTimeFormat(this.configState); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
npm/ng-packs/packages/core/src/lib/services/time.service.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
import { inject, Injectable, LOCALE_ID } from '@angular/core'; | ||
import { DateTime } from 'luxon'; | ||
|
||
@Injectable({ | ||
providedIn: 'root', | ||
}) | ||
export class TimeService { | ||
private locale = inject(LOCALE_ID); | ||
|
||
/** | ||
* Returns the current date and time in the specified timezone. | ||
* | ||
* @param zone - An IANA timezone name (e.g., 'Europe/Istanbul', 'UTC'); defaults to the system's local timezone. | ||
* @returns A Luxon DateTime instance representing the current time in the given timezone. | ||
*/ | ||
now(zone = 'local'): DateTime { | ||
return DateTime.now().setZone(zone); | ||
} | ||
|
||
/** | ||
* Converts the input date to the specified timezone, applying any timezone and daylight saving time (DST) adjustments. | ||
* | ||
* This method: | ||
* 1. Parses the input value into a Luxon DateTime object. | ||
* 2. Applies the specified IANA timezone, including any DST shifts based on the given date. | ||
* | ||
* @param value - The ISO string or Date object to convert. | ||
* @param zone - An IANA timezone name (e.g., 'America/New_York'). | ||
* @returns A Luxon DateTime instance adjusted to the specified timezone and DST rules. | ||
*/ | ||
toZone(value: string | Date, zone: string): DateTime { | ||
return DateTime.fromISO(value instanceof Date ? value.toISOString() : value, { | ||
zone, | ||
}); | ||
} | ||
|
||
/** | ||
* Formats the input date by applying timezone and daylight saving time (DST) adjustments. | ||
* | ||
* This method: | ||
* 1. Converts the input date to the specified timezone. | ||
* 2. Formats the result using the given format and locale, reflecting any timezone or DST shifts. | ||
* | ||
* @param value - The ISO string or Date object to format. | ||
* @param format - The format string (default: 'ff'). | ||
* @param zone - Optional IANA timezone name (e.g., 'America/New_York'); defaults to the system's local timezone. | ||
* @returns A formatted date string adjusted for the given timezone and DST rules. | ||
*/ | ||
format(value: string | Date, format = 'ff', zone = 'local'): string { | ||
return this.toZone(value, zone).setLocale(this.locale).toFormat(format); | ||
} | ||
|
||
/** | ||
* Formats a date using the standard time offset (ignoring daylight saving time) for the specified timezone. | ||
* | ||
* This method: | ||
* 1. Converts the input date to UTC. | ||
* 2. Calculates the standard UTC offset for the given timezone (based on January 1st to avoid DST). | ||
* 3. Applies the standard offset manually to the UTC time. | ||
* 4. Formats the result using the specified format and locale, without applying additional timezone shifts. | ||
* | ||
* @param value - The ISO string or Date object to format. | ||
* @param format - The Luxon format string (default: 'ff'). | ||
* @param zone - Optional IANA timezone name (e.g., 'America/New_York'); if omitted, system local timezone is used. | ||
* @returns A formatted date string adjusted by standard time (non-DST). | ||
*/ | ||
formatDateWithStandardOffset(value: string | Date, format = 'ff', zone?: string): string { | ||
const utcDate = | ||
typeof value === 'string' | ||
? DateTime.fromISO(value, { zone: 'UTC' }) | ||
: DateTime.fromJSDate(value, { zone: 'UTC' }); | ||
|
||
if (!utcDate.isValid) return ''; | ||
|
||
const targetZone = zone ?? DateTime.local().zoneName; | ||
|
||
const januaryDate = DateTime.fromObject( | ||
{ year: utcDate.year, month: 1, day: 1 }, | ||
{ zone: targetZone }, | ||
); | ||
const standardOffset = januaryDate.offset; | ||
const dateWithStandardOffset = utcDate.plus({ minutes: standardOffset }); | ||
|
||
return dateWithStandardOffset.setZone('UTC').setLocale(this.locale).toFormat(format); | ||
} | ||
|
||
/** | ||
* Formats the input date using its original clock time, without converting based on timezone or DST | ||
* | ||
* This method: | ||
* 1. Converts the input date to ISO string. | ||
* 2. Calculates the date time in UTC, keeping the local time. | ||
* 3. Formats the result using the specified format and locale, without shifting timezones. | ||
* | ||
* @param value - The ISO string or Date object to format. | ||
* @param format - The format string (default: 'ff'). | ||
* @returns A formatted date string without applying timezone. | ||
*/ | ||
formatWithoutTimeZone(value: string | Date, format = 'ff'): string { | ||
const isoString = value instanceof Date ? value.toISOString() : value; | ||
|
||
const dateTime = DateTime.fromISO(isoString) | ||
.setZone('utc', { keepLocalTime: true }) | ||
.setLocale(this.locale); | ||
return dateTime.toFormat(format); | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.