Skip to content

Commit

Permalink
Comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Phil Chen committed Jun 1, 2018
1 parent 43c5c94 commit 511c087
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions packages/labs/src/components/timezone-picker/timezoneMetadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,26 @@ export function getTimezoneMetadata(timezone: string, date: Date, useManualCalc:
return calculateMetadata(timezone, zone, timestamp);
}

function getValidAbbreviation(abbreviation: string) {
return isValidAbbreviation(abbreviation) ? abbreviation : undefined;
/**
* Ignore abbreviations that are simply offsets, i.e. "+14" instead of "PST"
* @param abbreviation
*/
function getNonOffsetAbbreviation(abbreviation: string) {
return isNonOffsetAbbreviation(abbreviation) ? abbreviation : undefined;
}

function isValidAbbreviation(abbreviation: string) {
function isNonOffsetAbbreviation(abbreviation: string) {
return abbreviation != null && abbreviation.length > 0 && abbreviation[0] !== "-" && abbreviation[0] !== "+";
}

/**
* Use moment-timezone to parse the timestamp and provide timezone metadata
*/
function getMetadataFromMoment(timezone: string, zone: moment.MomentZone, timestamp: number) {
const zonedDate = moment.tz(timestamp, timezone);
const offset = zonedDate.utcOffset();
const offsetAsString = zonedDate.format("Z");
const abbreviation = getValidAbbreviation(zonedDate.zoneAbbr());
const abbreviation = getNonOffsetAbbreviation(zonedDate.zoneAbbr());
return {
abbreviation,
offset,
Expand All @@ -43,10 +50,14 @@ function getMetadataFromMoment(timezone: string, zone: moment.MomentZone, timest
};
}

/**
* Manually determine timezone metadata by skipping the timestamp parsing and following
* http://momentjs.com/timezone/docs/#/data-formats/unpacked-format/
*/
function getMetadataFromMomentManual(timezone: string, zone: moment.MomentZone, timestamp: number) {
const { abbrs, offsets, population, untils } = zone;
const index = findOffsetIndex(timestamp, untils);
const abbreviation = getValidAbbreviation(abbrs[index]);
const abbreviation = getNonOffsetAbbreviation(abbrs[index]);
const offset = offsets[index] * -1;
const offsetAsString = getOffsetAsString(offset);
return {
Expand Down

1 comment on commit 511c087

@blueprint-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comments

Preview: documentation | landing | table

Please sign in to comment.