Skip to content

Commit

Permalink
Added some JS doc for time zones (#1499)
Browse files Browse the repository at this point in the history
Signed-off-by: Peter Tandler <[email protected]>
  • Loading branch information
ptandler authored Mar 9, 2024
1 parent 080e813 commit 895a749
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/impl/locale.js
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ export default class Locale {

static create(locale, numberingSystem, outputCalendar, weekSettings, defaultToEN = false) {
const specifiedLocale = locale || Settings.defaultLocale;
// the system locale is useful for human readable strings but annoying for parsing/formatting known formats
// the system locale is useful for human-readable strings but annoying for parsing/formatting known formats
const localeR = specifiedLocale || (defaultToEN ? "en-US" : systemLocale());
const numberingSystemR = numberingSystem || Settings.defaultNumberingSystem;
const outputCalendarR = outputCalendar || Settings.defaultOutputCalendar;
Expand Down
7 changes: 7 additions & 0 deletions src/impl/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,13 @@ export function normalizeObject(obj, normalizer) {
return normalized;
}

/**
* Returns the offset's value as a string
* @param {number} ts - Epoch milliseconds for which to get the offset
* @param {string} format - What style of offset to return.
* Accepts 'narrow', 'short', or 'techie'. Returning '+6', '+06:00', or '+0600' respectively
* @return {string}
*/
export function formatOffset(offset, format) {
const hours = Math.trunc(Math.abs(offset / 60)),
minutes = Math.trunc(Math.abs(offset % 60)),
Expand Down
6 changes: 6 additions & 0 deletions src/zone.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ export default class Zone {
throw new ZoneIsAbstractError();
}

/**
* The IANA name of this zone.
* Defaults to `name` if not overwritten by a subclass.
* @abstract
* @type {string}
*/
get ianaName() {
return this.name;
}
Expand Down
60 changes: 51 additions & 9 deletions src/zones/IANAZone.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export default class IANAZone extends Zone {
* @param {string} s - The string to check validity on
* @example IANAZone.isValidSpecifier("America/New_York") //=> true
* @example IANAZone.isValidSpecifier("Sport~~blorp") //=> false
* @deprecated This method returns false for some valid IANA names. Use isValidZone instead.
* @deprecated For backward compatibility, this forwards to isValidZone, better use `isValidZone()` directly instead.
* @return {boolean}
*/
static isValidSpecifier(s) {
Expand Down Expand Up @@ -118,32 +118,65 @@ export default class IANAZone extends Zone {
this.valid = IANAZone.isValidZone(name);
}

/** @override **/
/**
* The type of zone. `iana` for all instances of `IANAZone`.
* @override
* @type {string}
*/
get type() {
return "iana";
}

/** @override **/
/**
* The name of this zone (i.e. the IANA zone name).
* @override
* @type {string}
*/
get name() {
return this.zoneName;
}

/** @override **/
/**
* Returns whether the offset is known to be fixed for the whole year:
* Always returns false for all IANA zones.
* @override
* @type {boolean}
*/
get isUniversal() {
return false;
}

/** @override **/
/**
* Returns the offset's common name (such as EST) at the specified timestamp
* @override
* @param {number} ts - Epoch milliseconds for which to get the name
* @param {Object} opts - Options to affect the format
* @param {string} opts.format - What style of offset to return. Accepts 'long' or 'short'.
* @param {string} opts.locale - What locale to return the offset name in.
* @return {string}
*/
offsetName(ts, { format, locale }) {
return parseZoneInfo(ts, format, locale, this.name);
}

/** @override **/
/**
* Returns the offset's value as a string
* @override
* @param {number} ts - Epoch milliseconds for which to get the offset
* @param {string} format - What style of offset to return.
* Accepts 'narrow', 'short', or 'techie'. Returning '+6', '+06:00', or '+0600' respectively
* @return {string}
*/
formatOffset(ts, format) {
return formatOffset(this.offset(ts), format);
}

/** @override **/
/**
* Return the offset in minutes for this zone at the specified timestamp.
* @override
* @param {number} ts - Epoch milliseconds for which to compute the offset
* @return {number}
*/
offset(ts) {
const date = new Date(ts);

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

/** @override **/
/**
* Return whether this Zone is equal to another zone
* @override
* @param {Zone} otherZone - the zone to compare
* @return {boolean}
*/
equals(otherZone) {
return otherZone.type === "iana" && otherZone.name === this.name;
}

/** @override **/
/**
* Return whether this Zone is valid.
* @override
* @type {boolean}
*/
get isValid() {
return this.valid;
}
Expand Down
64 changes: 56 additions & 8 deletions src/zones/fixedOffsetZone.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,31 @@ export default class FixedOffsetZone extends Zone {
this.fixed = offset;
}

/** @override **/
/**
* The type of zone. `fixed` for all instances of `FixedOffsetZone`.
* @override
* @type {string}
*/
get type() {
return "fixed";
}

/** @override **/
/**
* The name of this zone.
* All fixed zones' names always start with "UTC" (plus optional offset)
* @override
* @type {string}
*/
get name() {
return this.fixed === 0 ? "UTC" : `UTC${formatOffset(this.fixed, "narrow")}`;
}

/**
* The IANA name of this zone, i.e. `Etc/UTC` or `Etc/GMT+/-nn`
*
* @override
* @type {string}
*/
get ianaName() {
if (this.fixed === 0) {
return "Etc/UTC";
Expand All @@ -70,32 +85,65 @@ export default class FixedOffsetZone extends Zone {
}
}

/** @override **/
/**
* Returns the offset's common name at the specified timestamp.
*
* For fixed offset zones this equals to the zone name.
* @override
*/
offsetName() {
return this.name;
}

/** @override **/
/**
* Returns the offset's value as a string
* @override
* @param {number} ts - Epoch milliseconds for which to get the offset
* @param {string} format - What style of offset to return.
* Accepts 'narrow', 'short', or 'techie'. Returning '+6', '+06:00', or '+0600' respectively
* @return {string}
*/
formatOffset(ts, format) {
return formatOffset(this.fixed, format);
}

/** @override **/
/**
* Returns whether the offset is known to be fixed for the whole year:
* Always returns true for all fixed offset zones.
* @override
* @type {boolean}
*/
get isUniversal() {
return true;
}

/** @override **/
/**
* Return the offset in minutes for this zone at the specified timestamp.
*
* For fixed offset zones, this is constant and does not depend on a timestamp.
* @override
* @return {number}
*/
offset() {
return this.fixed;
}

/** @override **/
/**
* Return whether this Zone is equal to another zone (i.e. also fixed and same offset)
* @override
* @param {Zone} otherZone - the zone to compare
* @return {boolean}
*/
equals(otherZone) {
return otherZone.type === "fixed" && otherZone.fixed === this.fixed;
}

/** @override **/
/**
* Return whether this Zone is valid:
* All fixed offset zones are valid.
* @override
* @type {boolean}
*/
get isValid() {
return true;
}
Expand Down

0 comments on commit 895a749

Please sign in to comment.