Skip to content

Commit c3fe151

Browse files
committed
Refactor: Separate TimeZone type definition into zone.ts
Move TimeZone interface and helper functions from timezone.ts to a new zone.ts file to improve code organization and prepare for adding consolidated timezone imports feature. This change does not affect the external API or functionality.
1 parent fd6d116 commit c3fe151

15 files changed

Lines changed: 133 additions & 41 deletions

File tree

src/addDays.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { toParts, fromParts } from './datetime.ts';
2-
import { isTimeZone, isUTC, createTimezoneDate } from './timezone.ts';
3-
import type { TimeZone } from './timezone.ts';
2+
import { isTimeZone, isUTC, createTimezoneDate } from './zone.ts';
3+
import type { TimeZone } from './zone.ts';
44

55
/**
66
* Adds the specified number of days to a Date object.

src/addMonths.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { toParts, fromParts } from './datetime.ts';
2-
import { isTimeZone, isUTC, createTimezoneDate } from './timezone.ts';
3-
import type { TimeZone } from './timezone.ts';
2+
import { isTimeZone, isUTC, createTimezoneDate } from './zone.ts';
3+
import type { TimeZone } from './zone.ts';
44

55
/**
66
* Adds the specified number of months to a Date object.

src/addYears.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { addMonths } from './addMonths.ts';
2-
import type { TimeZone } from './timezone.ts';
2+
import type { TimeZone } from './zone.ts';
33

44
/**
55
* Adds the specified number of years to a Date object.

src/datetime.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { getDateTimeFormat } from './dtf.ts';
2+
import { isUTC } from './zone.ts';
23

34
export interface DateTimeParts {
45
weekday: number;
@@ -13,7 +14,7 @@ export interface DateTimeParts {
1314
}
1415

1516
export const toParts = (dateObj: Date, zoneName: string): DateTimeParts => {
16-
if (zoneName.toUpperCase() === 'UTC') {
17+
if (isUTC(zoneName)) {
1718
return {
1819
weekday: dateObj.getUTCDay(),
1920
year: dateObj.getUTCFullYear(),
@@ -102,11 +103,11 @@ export interface DateLike {
102103
*/
103104
getMilliseconds(): number;
104105
/**
105-
* Returns the day of the week (0-6; where 0 is Sunday).
106+
* Returns the day of the week (0-6, where 0 is Sunday).
106107
*/
107108
getDay(): number;
108109
/**
109-
* Returns the time value in milliseconds since the Unix epoch (January 1; 1970).
110+
* Returns the time value in milliseconds since the Unix epoch (January 1, 1970).
110111
*/
111112
getTime(): number;
112113
/**

src/format.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { DateTime } from './datetime.ts';
33
import { formatter as defaultFormatter } from './formatter.ts';
44
import en from './locales/en.ts';
55
import latn from './numerals/latn.ts';
6-
import { isTimeZone, isUTC } from './timezone.ts';
6+
import { isTimeZone } from './zone.ts';
77
import type { CompiledObject } from './compile.ts';
88
import type { FormatterOptions } from './formatter.ts';
99

@@ -18,15 +18,14 @@ const comment = /^\[(.*)\]$/;
1818
*/
1919
export function format(dateObj: Date, arg: string | CompiledObject, options?: FormatterOptions) {
2020
const pattern = (typeof arg === 'string' ? compile(arg) : arg).slice(1);
21-
const timeZone = isTimeZone(options?.timeZone) || isUTC(options?.timeZone) ? options.timeZone : undefined;
22-
const zoneName = typeof timeZone === 'string' ? timeZone : timeZone?.zone_name ?? '';
21+
const zoneName = isTimeZone(options?.timeZone) ? options.timeZone.zone_name : options?.timeZone;
2322
const dateTime = zoneName ? new DateTime(dateObj, zoneName) : dateObj;
2423
const formatterOptions = {
2524
hour12: options?.hour12 ?? 'h12',
2625
hour24: options?.hour24 ?? 'h23',
2726
numeral: options?.numeral ?? latn,
2827
calendar: options?.calendar ?? 'gregory',
29-
timeZone,
28+
timeZone: options?.timeZone,
3029
locale: options?.locale ?? en
3130
};
3231
const formatters = [...options?.plugins ?? [], defaultFormatter];

src/formatter.ts

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { CompiledObject } from './compile.ts';
22
import type { DateLike } from './datetime.ts';
33
import type { Locale } from './locale.ts';
44
import type { Numeral } from './numeral.ts';
5-
import type { TimeZone } from './timezone.ts';
5+
import type { TimeZone } from './zone.ts';
66

77
export interface FormatterPluginOptions {
88
/**
@@ -31,12 +31,12 @@ export interface FormatterPluginOptions {
3131
*/
3232
calendar: 'buddhist' | 'gregory';
3333

34-
/**
35-
* The time zone to use for formatting dates and times.
36-
* This can be a specific time zone object or 'UTC' to use Coordinated Universal Time.
37-
* If not specified, it defaults to undefined, which means the local time zone will be used.
38-
*/
39-
timeZone: TimeZone | 'UTC' | undefined;
34+
/**
35+
* The time zone to use for formatting dates and times.
36+
* This can be a specific time zone object, an IANA time zone name, or 'UTC' to use Coordinated Universal Time.
37+
* If not specified, it defaults to undefined, which means the local time zone will be used.
38+
*/
39+
timeZone: TimeZone | string | undefined;
4040

4141
/**
4242
* The locale to use for formatting dates and times.
@@ -72,12 +72,12 @@ class DefaultFormatter extends FormatterPlugin {
7272

7373
MMMM (d: DateLike, options: FormatterPluginOptions, compiledObj: CompiledObject) {
7474
const list = options.locale.getMonthList({ style: 'long', compiledObj });
75-
return list[d.getMonth()] || '';
75+
return list[d.getMonth()] ?? '';
7676
}
7777

7878
MMM (d: DateLike, options: FormatterPluginOptions, compiledObj: CompiledObject) {
7979
const list = options.locale.getMonthList({ style: 'short', compiledObj });
80-
return list[d.getMonth()] || '';
80+
return list[d.getMonth()] ?? '';
8181
}
8282

8383
MM (d: DateLike) {
@@ -106,22 +106,22 @@ class DefaultFormatter extends FormatterPlugin {
106106

107107
AA (d: DateLike, options: FormatterPluginOptions, compiledObj: CompiledObject) {
108108
const list = options.locale.getMeridiemList({ style: 'long', compiledObj, case: 'uppercase' });
109-
return list[+(d.getHours() > 11)] || '';
109+
return list[+(d.getHours() > 11)] ?? '';
110110
}
111111

112112
A (d: DateLike, options: FormatterPluginOptions, compiledObj: CompiledObject) {
113113
const list = options.locale.getMeridiemList({ style: 'short', compiledObj, case: 'uppercase' });
114-
return list[+(d.getHours() > 11)] || '';
114+
return list[+(d.getHours() > 11)] ?? '';
115115
}
116116

117117
aa (d: DateLike, options: FormatterPluginOptions, compiledObj: CompiledObject) {
118118
const list = options.locale.getMeridiemList({ style: 'long', compiledObj, case: 'lowercase' });
119-
return list[+(d.getHours() > 11)] || '';
119+
return list[+(d.getHours() > 11)] ?? '';
120120
}
121121

122122
a (d: DateLike, options: FormatterPluginOptions, compiledObj: CompiledObject) {
123123
const list = options.locale.getMeridiemList({ style: 'short', compiledObj, case: 'lowercase' });
124-
return list[+(d.getHours() > 11)] || '';
124+
return list[+(d.getHours() > 11)] ?? '';
125125
}
126126

127127
hh (d: DateLike, options: FormatterPluginOptions) {
@@ -162,17 +162,17 @@ class DefaultFormatter extends FormatterPlugin {
162162

163163
dddd (d: DateLike, options: FormatterPluginOptions, compiledObj: CompiledObject) {
164164
const list = options.locale.getDayOfWeekList({ style: 'long', compiledObj });
165-
return list[d.getDay()] || '';
165+
return list[d.getDay()] ?? '';
166166
}
167167

168168
ddd (d: DateLike, options: FormatterPluginOptions, compiledObj: CompiledObject) {
169169
const list = options.locale.getDayOfWeekList({ style: 'short', compiledObj });
170-
return list[d.getDay()] || '';
170+
return list[d.getDay()] ?? '';
171171
}
172172

173173
dd (d: DateLike, options: FormatterPluginOptions, compiledObj: CompiledObject) {
174174
const list = options.locale.getDayOfWeekList({ style: 'narrow', compiledObj });
175-
return list[d.getDay()] || '';
175+
return list[d.getDay()] ?? '';
176176
}
177177

178178
Z (d: DateLike) {

src/parse.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { isTimeZone, isUTC, createTimezoneDate } from './timezone.ts';
1+
import { isTimeZone, isUTC, createTimezoneDate } from './zone.ts';
22
import { validatePreparseResult } from './isValid.ts';
33
import { preparse } from './preparse.ts';
44
import type { CompiledObject } from './compile.ts';

src/parser.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { CompiledObject } from './compile.ts';
22
import type { Locale } from './locale.ts';
33
import type { Numeral } from './numeral.ts';
4-
import type { TimeZone } from './timezone.ts';
4+
import type { TimeZone } from './zone.ts';
55

66
type ParserToken = 'Y' | 'M' | 'D' | 'H' | 'A' | 'h' | 'm' | 's' | 'S' | 'Z';
77

src/plugin.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ export type { CompiledObject } from './compile.ts';
66
export type { DateLike } from './datetime.ts';
77
export type { Locale } from './locale.ts';
88
export type { Numeral } from './numeral.ts';
9-
export type { TimeZone } from './timezone.ts';
9+
export type { TimeZone } from './zone.ts';

src/plugins/zonename.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import timeZoneNames from '@/zonenames.ts';
22
import { FormatterPlugin } from '@/plugin.ts';
3+
import { isTimeZone } from '@/zone.ts';
34
import type { FormatterPluginOptions, DateLike } from '@/plugin.ts';
45

56
const getLongTimezoneName = (time: number, zoneName?: string) => {
@@ -16,12 +17,12 @@ const getShortTimezoneName = (time: number, zoneName?: string) => {
1617

1718
class Formatter extends FormatterPlugin {
1819
z (d: DateLike, options: FormatterPluginOptions) {
19-
const zoneName = options.timeZone === 'UTC' ? 'UTC' : options.timeZone?.zone_name;
20+
const zoneName = isTimeZone(options.timeZone) ? options.timeZone.zone_name : options.timeZone;
2021
return getShortTimezoneName(d.getTime(), zoneName);
2122
}
2223

2324
zz (d: DateLike, options: FormatterPluginOptions) {
24-
const zoneName = options.timeZone === 'UTC' ? 'UTC' : options.timeZone?.zone_name;
25+
const zoneName = isTimeZone(options.timeZone) ? options.timeZone.zone_name : options.timeZone;
2526
return getLongTimezoneName(d.getTime(), zoneName);
2627
}
2728
}

0 commit comments

Comments
 (0)