Skip to content

Commit

Permalink
feat: timezone format
Browse files Browse the repository at this point in the history
  • Loading branch information
badosz0 committed Aug 20, 2024
1 parent 43f11ca commit 8d17550
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "holy-time",
"version": "5.1.0",
"version": "5.2.0",
"description": "Utility functions",
"repository": "Yet another (type-safe) date time library",
"main": "dist/index.js",
Expand Down
2 changes: 1 addition & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export const TimeUnits = {
MILLISECOND: 1,
} as const;

export const FORMAT_REGEX = /\[(?<escaped>[^\]]+)]|Y{4}|Y{2}|M{1,4}|D{1,4}|d{1,4}|H{1,2}|h{1,2}|m{1,2}|s{1,2}|a|A/g;
export const FORMAT_REGEX = /\[(?<escaped>[^\]]+)]|Y{4}|Y{2}|M{1,4}|D{1,4}|d{1,4}|H{1,2}|h{1,2}|m{1,2}|s{1,2}|a|A|O{1,2}|TZ/g;
export const MONTH_NAMES = [ 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December' ];
export const DAY_NAMES = [ 'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday' ];

Expand Down
28 changes: 27 additions & 1 deletion src/time.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,10 @@ export class HolyTime {
}

public static format(time: TimeResolvable, format: string | ((time: HolyTime) => string), timeZone?: TimeZone): string {
const date = HolyTime.adjustToTimeZone(HolyTime.resolveDate(time), timeZone);
const resolvedDate = HolyTime.resolveDate(time);
const utcDate = HolyTime.adjustToTimeZone(resolvedDate, 'UTC');
const date = HolyTime.adjustToTimeZone(resolvedDate, timeZone);
const timeZoneOffset = Math.round(this.between(date, utcDate).in('minutes')) * (this.isAfter(date, utcDate) ? -1 : +1);

if (typeof format === 'function') {
return format(new HolyTime(date));
Expand All @@ -237,11 +240,34 @@ export class HolyTime {
mm: date.getMinutes().toString().padStart(2, '0'),
s: date.getSeconds().toString(),
ss: date.getSeconds().toString().padStart(2, '0'),
O: `GMT${this.formatTimeZoneOffset(timeZoneOffset, false)}`,
OO: `GMT${this.formatTimeZoneOffset(timeZoneOffset, true)}`,
TZ: TIMEZONE_MAP[timeZone as keyof typeof TIMEZONE_MAP] ?? timeZone ?? this.getTimeZone(),
};

return format.replace(FORMAT_REGEX, (match, group) => group ?? values[match] ?? '?');
}

private static formatTimeZoneOffset(offset: number, long: boolean): string {
const sign = offset > 0 ? '-' : '+';

const hours = long
? this.formatWithLeadingZeros(Math.trunc(Math.abs(offset) / 60), 2)
: Math.trunc(Math.abs(offset) / 60);
const minutes = Math.abs(offset) % 60;

return minutes === 0 && !long
? `${sign}${hours}`
: `${sign}${hours}:${this.formatWithLeadingZeros(minutes, 2)}`;
}

private static formatWithLeadingZeros(number: number, length: number): string {
const sign = number < 0 ? '-' : '';
const output = Math.abs(number).toString().padStart(length, '0');

return `${sign}${output}`;
}

public format(format: string, timeZone?: TimeZone): string {
return HolyTime.format(this, format, timeZone);
}
Expand Down

0 comments on commit 8d17550

Please sign in to comment.