UNPKG

@progress/kendo-date-math

Version:

Kendo UI typescript package exporting functions for Date manipulations

58 lines (57 loc) 2.39 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.abbrTimezone = void 0; var zone_and_rule_1 = require("./zone-and-rule"); var pad_number_1 = require("./pad-number"); // tslint:disable:max-line-length /** * A function which returns the abbreviated name of the timezone. You can specify an optional date for returning the timezone name at a different point in time. The corresponding UTC date is used for locating the relevant rule. Timezone names change both historically and when they reflect the Daylight Savings Time rules. * * @param timezone - The timezone name. For example, `America/Chicago`, `Europe/Sofia`. * @param date - A date for which to locate the zone rule. By default, the current time is used. * * @return - The abbreviated name of the timezone at the specified date or, if not set, returns now. * * @example * ```ts-no-run * import { abbrTimezone } from '@progress/kendo-date-math'; * import '@progress/kendo-date-math/tz/Europe/Sofia'; * * const dstDate = new Date('2018-04-01T00:00:00Z'); * console.log(abbrTimezone('Europe/Sofia', dstDate)); // EEST * * const date = new Date('2018-01-01T00:00:00Z'); * console.log(abbrTimezone('Europe/Sofia', date)); // EET * ``` */ // tslint:enable:max-line-length var abbrTimezone = function (timezone, date) { if (date === void 0) { date = new Date(); } if (timezone === "Etc/UTC") { return 'UTC'; } if (timezone === "Etc/GMT") { return 'GMT'; } if (timezone === '') { return ''; } var _a = (0, zone_and_rule_1.zoneAndRule)(timezone, date), zone = _a.zone, rule = _a.rule; var base = zone[2]; if (base.indexOf("/") >= 0) { return base.split("/")[rule && +rule[6] ? 1 : 0]; } else if (base.indexOf("%s") >= 0) { return base.replace("%s", (!rule || rule[7] === "-") ? '' : rule[7]); } else if (base.indexOf("%z") >= 0) { var hours = -1 * Math.trunc(zone[0] / 60); var hoursPart = (0, pad_number_1.padNumber)(hours, 2); var signPart = hours > 0 ? '+' : ''; var minutes = Math.abs(Math.trunc(zone[0] % 60)); var minutesPart = minutes === 0 ? '' : (0, pad_number_1.padNumber)(minutes, 2); return base.replace("%z", "".concat(signPart).concat(hoursPart).concat(minutesPart)); } return base; }; exports.abbrTimezone = abbrTimezone;