Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const index = numarray(raw.index, 36);
const offsets = numarray(raw.offsets, 36);
const untils = numarray(raw.untils, 36).map(n => n === -1 ? n : n * 1000);
for (let i = 0; i < index.length; i += 2) {
const s = index[i];
const e = index[i + 1];
const rec = {
offsets: offsets.slice(s, e),
untils: untils.slice(s, e)
};
this.metazones.push(rec);
}
// mapping of zoneid to metazone records
const zoneids = TZ.zoneIds();
const zoneindex = numarray(raw.zoneindex, 36);
// Mapping of tzdb id back to cldr stable id used for schema lookups
raw.stableids.split('|').forEach((d: string) => {
const p = d.split(':');
const i = Number(p[0]);
this.stableids.set(zoneids[i], p[1]);
});
// Sanity-check, since the zoneindex is based off the canonical
// zoneids array, but could be generated at different times. our test
// cases should ensure they're in sync, but warn of a discrepancy
/* istanbul ignore if */
if (zoneids.length !== zoneindex.length) {
console.log(`Error: time zone ids and zone index are not in sync!`);
}
export const getStableTimeZoneId = (zoneid: string): string => {
init();
// Check if this is already a CLDR stable timezone id.
const isstable = TimeZoneStableIdIndex.get(zoneid) !== -1;
if (!isstable) {
// Resolve the passed-in string to a real tzdb zone id
const realid = TZ.resolveId(zoneid);
if (realid) {
// Map to a CLDR stable id
zoneid = metazones!.getStableId(realid);
}
}
return zoneid;
};
export const zoneInfoFromUTC = (zoneid: string, utc: number): ZoneInfo => {
init();
let tzinfo = TZ.fromUTC(zoneid, utc);
if (tzinfo === undefined) {
tzinfo = TZ.utcZone();
}
// For the purposes of CLDR stable timezone ids, check if the passed-in
// id is an alias to a current/valid tzdb id.
const isstable = TimeZoneStableIdIndex.get(zoneid) !== -1;
// Use the passed-in id as the stable id if it is an alias,
// otherwise lookup the id in the stable map.
const stableid = isstable ? zoneid : metazones!.getStableId(tzinfo.zoneid);
// Use the corrected zone id to lookup the metazone
const metazoneid = metazones!.getMetazone(tzinfo.zoneid, utc);
return {
...tzinfo,
export const zoneInfoFromUTC = (zoneid: string, utc: number): ZoneInfo => {
init();
let tzinfo = TZ.fromUTC(zoneid, utc);
if (tzinfo === undefined) {
tzinfo = TZ.utcZone();
}
// For the purposes of CLDR stable timezone ids, check if the passed-in
// id is an alias to a current/valid tzdb id.
const isstable = TimeZoneStableIdIndex.get(zoneid) !== -1;
// Use the passed-in id as the stable id if it is an alias,
// otherwise lookup the id in the stable map.
const stableid = isstable ? zoneid : metazones!.getStableId(tzinfo.zoneid);
// Use the corrected zone id to lookup the metazone
const metazoneid = metazones!.getMetazone(tzinfo.zoneid, utc);
return {
...tzinfo,
metazoneid: metazoneid || ('' as MetaZoneType),
stableid
const buildStableIdMapping = (data: any): string => {
const tzids = TZ.zoneIds();
const stableids = data.timeZoneIds;
const res: string[] = [];
for (const stableid of stableids) {
const resolved = TZ.resolveId(stableid);
if (resolved === undefined || stableid === resolved) {
continue;
}
const i = tzids.indexOf(resolved);
// const j = stableids.indexOf(stableid);
res.push(`${i}:${stableid}`);
}
return res.join('|');
};
Object.keys(data).forEach((id, mi) => {
// Map the metazone id to the index of the time zone id in the TZ data.
let zi = zoneids.indexOf(id);
if (zi === -1) {
// We have an alias, e.g. Africa/Addis_Ababa, so follow the link to
// get the correct tzdb id.
const zid = TZ.resolveId(id);
if (zid === undefined) {
throw new Error(`${chalk.red('Error')} tzdb / cldr mismatch. zone id failed ${id}`);
}
zi = zoneids.indexOf(zid);
}
zonemap.set(zi, mi);
});
const zones = Object.keys(zoneAlias).filter(alias => {
const zi = zoneids.indexOf(alias);
if (zi !== -1) {
// alias is in list of valid tzdb ids, ignore
return false;
}
// if alias fails to resolve, include in this list
return TZ.resolveId(alias) === undefined;
}).sort().map(k => [k, zoneAlias[k]].join(':')).join('|');
const result: Code[] = [];
let code = HEADER + NOLINT_MAXLINE;
code += `export const languageAliasRaw = '${languages}';\n\n`;
code += `export const scriptAliasRaw = '${scripts}';\n`;
result.push(Code.locale(['autogen.aliases.ts'], code));
code = HEADER + NOLINT_MAXLINE;
code += `export const territoryAliasRaw = '${territories}';\n`;
result.push(Code.languagetag(['autogen.aliases.ts'], code));
// Find time zone aliases that are not already handled in the @phensley/timezone package
const zoneids = TZ.zoneIds();
const zones = Object.keys(zoneAlias).filter(alias => {
const zi = zoneids.indexOf(alias);
if (zi !== -1) {
// alias is in list of valid tzdb ids, ignore
return false;
}
// if alias fails to resolve, include in this list
return TZ.resolveId(alias) === undefined;
}).sort().map(k => [k, zoneAlias[k]].join(':')).join('|');
code = HEADER + NOLINT_MAXLINE;
code += `export const zoneAliasRaw = '${zones}';\n`;
result.push(Code.core(['systems', 'calendars', 'autogen.aliases.ts'], code));
const buildStableIdMapping = (data: any): string => {
const tzids = TZ.zoneIds();
const stableids = data.timeZoneIds;
const res: string[] = [];
for (const stableid of stableids) {
const resolved = TZ.resolveId(stableid);
if (resolved === undefined || stableid === resolved) {
continue;
}
const i = tzids.indexOf(resolved);
// const j = stableids.indexOf(stableid);
res.push(`${i}:${stableid}`);
}
return res.join('|');
};
const buildMetaZones2 = (data: any): Metazones => {
const metazoneIndex = new IdArray();
const offsets: number[] = [];
const untils: number[] = [];
const index: number[] = [];
const zonemap = new Map();
// Array of canonical time zone ids
const zoneids = TZ.zoneIds();
Object.keys(data).forEach((id, mi) => {
// Map the metazone id to the index of the time zone id in the TZ data.
let zi = zoneids.indexOf(id);
if (zi === -1) {
// We have an alias, e.g. Africa/Addis_Ababa, so follow the link to
// get the correct tzdb id.
const zid = TZ.resolveId(id);
if (zid === undefined) {
throw new Error(`${chalk.red('Error')} tzdb / cldr mismatch. zone id failed ${id}`);
}
zi = zoneids.indexOf(zid);
}
zonemap.set(zi, mi);
});