Skip to content

Commit 01e407b

Browse files
committed
feat(date): add timezone handling and formatting utilities to DateBuilder
- Implement timezone methods in DateBuilder, including formatInTimeZone, toTimeZone, getTimezoneOffset, isDST, and getTimezoneAbbreviation. - Enhance date.utils with timezone-aware date formatting presets and functions. - Add tests for timezone conversions, DST handling, and date formatting in various timezones. - Ensure correct handling of date rollovers and daylight saving time transitions.
1 parent bc755b3 commit 01e407b

4 files changed

Lines changed: 1211 additions & 111 deletions

File tree

src/date/date.builder.ts

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,12 @@ import {
1515
getDateFromDuration,
1616
DateFormatOptions,
1717
formatDate,
18-
formatRelativeTime
18+
formatRelativeTime,
19+
formatDateInTimeZone,
20+
toTimeZone,
21+
getTimezoneOffset,
22+
isDST,
23+
getTimezoneAbbreviation
1924
} from './date.utils';
2025

2126
/**
@@ -705,4 +710,47 @@ export class DateBuilder {
705710
toString(): string {
706711
return this.date.toString();
707712
}
713+
714+
// ========== Timezone Methods ==========
715+
716+
/**
717+
* Format the date in a specific timezone.
718+
* @param timeZone - IANA timezone identifier (e.g., 'America/New_York')
719+
* @param format - Format pattern (default: 'yyyy-MM-dd HH:mm:ss')
720+
*/
721+
formatInTimeZone(timeZone: string, format: string = 'yyyy-MM-dd HH:mm:ss'): string {
722+
return formatDateInTimeZone(this.date, timeZone, format);
723+
}
724+
725+
/**
726+
* Get the wall-clock components in a specific timezone.
727+
* @param timeZone - IANA timezone identifier
728+
*/
729+
toTimeZone(timeZone: string) {
730+
return toTimeZone(this.date, timeZone);
731+
}
732+
733+
/**
734+
* Get the UTC offset in minutes for a specific timezone at this date's instant.
735+
* @param timeZone - IANA timezone identifier
736+
*/
737+
getTimezoneOffset(timeZone: string): number {
738+
return getTimezoneOffset(timeZone, this.date);
739+
}
740+
741+
/**
742+
* Check if the specified timezone is observing DST at this date's instant.
743+
* @param timeZone - IANA timezone identifier
744+
*/
745+
isDST(timeZone: string): boolean {
746+
return isDST(timeZone, this.date);
747+
}
748+
749+
/**
750+
* Get the timezone abbreviation (e.g., 'EST', 'EDT') for a specific timezone.
751+
* @param timeZone - IANA timezone identifier
752+
*/
753+
getTimezoneAbbreviation(timeZone: string): string {
754+
return getTimezoneAbbreviation(timeZone, this.date);
755+
}
708756
}

0 commit comments

Comments
 (0)