Skip to content

Commit 895a749

Browse files
authored
Added some JS doc for time zones (#1499)
Signed-off-by: Peter Tandler <[email protected]>
1 parent 080e813 commit 895a749

File tree

5 files changed

+121
-18
lines changed

5 files changed

+121
-18
lines changed

src/impl/locale.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ export default class Locale {
340340

341341
static create(locale, numberingSystem, outputCalendar, weekSettings, defaultToEN = false) {
342342
const specifiedLocale = locale || Settings.defaultLocale;
343-
// the system locale is useful for human readable strings but annoying for parsing/formatting known formats
343+
// the system locale is useful for human-readable strings but annoying for parsing/formatting known formats
344344
const localeR = specifiedLocale || (defaultToEN ? "en-US" : systemLocale());
345345
const numberingSystemR = numberingSystem || Settings.defaultNumberingSystem;
346346
const outputCalendarR = outputCalendar || Settings.defaultOutputCalendar;

src/impl/util.js

+7
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,13 @@ export function normalizeObject(obj, normalizer) {
287287
return normalized;
288288
}
289289

290+
/**
291+
* Returns the offset's value as a string
292+
* @param {number} ts - Epoch milliseconds for which to get the offset
293+
* @param {string} format - What style of offset to return.
294+
* Accepts 'narrow', 'short', or 'techie'. Returning '+6', '+06:00', or '+0600' respectively
295+
* @return {string}
296+
*/
290297
export function formatOffset(offset, format) {
291298
const hours = Math.trunc(Math.abs(offset / 60)),
292299
minutes = Math.trunc(Math.abs(offset % 60)),

src/zone.js

+6
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ export default class Zone {
2222
throw new ZoneIsAbstractError();
2323
}
2424

25+
/**
26+
* The IANA name of this zone.
27+
* Defaults to `name` if not overwritten by a subclass.
28+
* @abstract
29+
* @type {string}
30+
*/
2531
get ianaName() {
2632
return this.name;
2733
}

src/zones/IANAZone.js

+51-9
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ export default class IANAZone extends Zone {
8383
* @param {string} s - The string to check validity on
8484
* @example IANAZone.isValidSpecifier("America/New_York") //=> true
8585
* @example IANAZone.isValidSpecifier("Sport~~blorp") //=> false
86-
* @deprecated This method returns false for some valid IANA names. Use isValidZone instead.
86+
* @deprecated For backward compatibility, this forwards to isValidZone, better use `isValidZone()` directly instead.
8787
* @return {boolean}
8888
*/
8989
static isValidSpecifier(s) {
@@ -118,32 +118,65 @@ export default class IANAZone extends Zone {
118118
this.valid = IANAZone.isValidZone(name);
119119
}
120120

121-
/** @override **/
121+
/**
122+
* The type of zone. `iana` for all instances of `IANAZone`.
123+
* @override
124+
* @type {string}
125+
*/
122126
get type() {
123127
return "iana";
124128
}
125129

126-
/** @override **/
130+
/**
131+
* The name of this zone (i.e. the IANA zone name).
132+
* @override
133+
* @type {string}
134+
*/
127135
get name() {
128136
return this.zoneName;
129137
}
130138

131-
/** @override **/
139+
/**
140+
* Returns whether the offset is known to be fixed for the whole year:
141+
* Always returns false for all IANA zones.
142+
* @override
143+
* @type {boolean}
144+
*/
132145
get isUniversal() {
133146
return false;
134147
}
135148

136-
/** @override **/
149+
/**
150+
* Returns the offset's common name (such as EST) at the specified timestamp
151+
* @override
152+
* @param {number} ts - Epoch milliseconds for which to get the name
153+
* @param {Object} opts - Options to affect the format
154+
* @param {string} opts.format - What style of offset to return. Accepts 'long' or 'short'.
155+
* @param {string} opts.locale - What locale to return the offset name in.
156+
* @return {string}
157+
*/
137158
offsetName(ts, { format, locale }) {
138159
return parseZoneInfo(ts, format, locale, this.name);
139160
}
140161

141-
/** @override **/
162+
/**
163+
* Returns the offset's value as a string
164+
* @override
165+
* @param {number} ts - Epoch milliseconds for which to get the offset
166+
* @param {string} format - What style of offset to return.
167+
* Accepts 'narrow', 'short', or 'techie'. Returning '+6', '+06:00', or '+0600' respectively
168+
* @return {string}
169+
*/
142170
formatOffset(ts, format) {
143171
return formatOffset(this.offset(ts), format);
144172
}
145173

146-
/** @override **/
174+
/**
175+
* Return the offset in minutes for this zone at the specified timestamp.
176+
* @override
177+
* @param {number} ts - Epoch milliseconds for which to compute the offset
178+
* @return {number}
179+
*/
147180
offset(ts) {
148181
const date = new Date(ts);
149182

@@ -177,12 +210,21 @@ export default class IANAZone extends Zone {
177210
return (asUTC - asTS) / (60 * 1000);
178211
}
179212

180-
/** @override **/
213+
/**
214+
* Return whether this Zone is equal to another zone
215+
* @override
216+
* @param {Zone} otherZone - the zone to compare
217+
* @return {boolean}
218+
*/
181219
equals(otherZone) {
182220
return otherZone.type === "iana" && otherZone.name === this.name;
183221
}
184222

185-
/** @override **/
223+
/**
224+
* Return whether this Zone is valid.
225+
* @override
226+
* @type {boolean}
227+
*/
186228
get isValid() {
187229
return this.valid;
188230
}

src/zones/fixedOffsetZone.js

+56-8
Original file line numberDiff line numberDiff line change
@@ -52,16 +52,31 @@ export default class FixedOffsetZone extends Zone {
5252
this.fixed = offset;
5353
}
5454

55-
/** @override **/
55+
/**
56+
* The type of zone. `fixed` for all instances of `FixedOffsetZone`.
57+
* @override
58+
* @type {string}
59+
*/
5660
get type() {
5761
return "fixed";
5862
}
5963

60-
/** @override **/
64+
/**
65+
* The name of this zone.
66+
* All fixed zones' names always start with "UTC" (plus optional offset)
67+
* @override
68+
* @type {string}
69+
*/
6170
get name() {
6271
return this.fixed === 0 ? "UTC" : `UTC${formatOffset(this.fixed, "narrow")}`;
6372
}
6473

74+
/**
75+
* The IANA name of this zone, i.e. `Etc/UTC` or `Etc/GMT+/-nn`
76+
*
77+
* @override
78+
* @type {string}
79+
*/
6580
get ianaName() {
6681
if (this.fixed === 0) {
6782
return "Etc/UTC";
@@ -70,32 +85,65 @@ export default class FixedOffsetZone extends Zone {
7085
}
7186
}
7287

73-
/** @override **/
88+
/**
89+
* Returns the offset's common name at the specified timestamp.
90+
*
91+
* For fixed offset zones this equals to the zone name.
92+
* @override
93+
*/
7494
offsetName() {
7595
return this.name;
7696
}
7797

78-
/** @override **/
98+
/**
99+
* Returns the offset's value as a string
100+
* @override
101+
* @param {number} ts - Epoch milliseconds for which to get the offset
102+
* @param {string} format - What style of offset to return.
103+
* Accepts 'narrow', 'short', or 'techie'. Returning '+6', '+06:00', or '+0600' respectively
104+
* @return {string}
105+
*/
79106
formatOffset(ts, format) {
80107
return formatOffset(this.fixed, format);
81108
}
82109

83-
/** @override **/
110+
/**
111+
* Returns whether the offset is known to be fixed for the whole year:
112+
* Always returns true for all fixed offset zones.
113+
* @override
114+
* @type {boolean}
115+
*/
84116
get isUniversal() {
85117
return true;
86118
}
87119

88-
/** @override **/
120+
/**
121+
* Return the offset in minutes for this zone at the specified timestamp.
122+
*
123+
* For fixed offset zones, this is constant and does not depend on a timestamp.
124+
* @override
125+
* @return {number}
126+
*/
89127
offset() {
90128
return this.fixed;
91129
}
92130

93-
/** @override **/
131+
/**
132+
* Return whether this Zone is equal to another zone (i.e. also fixed and same offset)
133+
* @override
134+
* @param {Zone} otherZone - the zone to compare
135+
* @return {boolean}
136+
*/
94137
equals(otherZone) {
95138
return otherZone.type === "fixed" && otherZone.fixed === this.fixed;
96139
}
97140

98-
/** @override **/
141+
/**
142+
* Return whether this Zone is valid:
143+
* All fixed offset zones are valid.
144+
* @override
145+
* @type {boolean}
146+
*/
99147
get isValid() {
100148
return true;
101149
}

0 commit comments

Comments
 (0)